diff --git a/src/ansi_string/ansi_string.py b/src/ansi_string/ansi_string.py index 32a728b..094f032 100644 --- a/src/ansi_string/ansi_string.py +++ b/src/ansi_string/ansi_string.py @@ -969,21 +969,15 @@ def apply_formatting( return if end is None: - length = None - else: - length = end - start - if length <= 0: - # Ignore - nothing to apply - return + end = len(self._s) settings = __class__.Settings(setting_or_settings) # Apply settings self._insert_settings(start, True, settings, topmost) - if length is not None: - # Remove settings - self._insert_settings(start + length, False, settings, topmost) + # Remove settings + self._insert_settings(end, False, settings, topmost) def apply_formatting_for_match( self, @@ -1282,8 +1276,12 @@ def __iadd__(self, value): settings_cpy = copy.deepcopy(value._color_settings) __class__._shift_settings_idx(settings_cpy, len(self._s), False) self._s += value._s - for key, value in settings_cpy.values(): - self._color_settings[key] = value + for key, value in settings_cpy.items(): + if key in self._color_settings: + self._color_settings[key][0].extend(value[0]) + self._color_settings[key][1].extend(value[1]) + else: + self._color_settings[key] = value else: raise ValueError(f'value is invalid type: {type(value)}') return self diff --git a/tests/test_ansi_string.py b/tests/test_ansi_string.py index b377851..2063445 100755 --- a/tests/test_ansi_string.py +++ b/tests/test_ansi_string.py @@ -63,6 +63,13 @@ def test_ranges(self): 'across different ranges' ) + def test_format_right_only(self): + s = AnsiString('This has no ANSI formatting') + self.assertEqual( + f'{s:#>90}', + '###############################################################This has no ANSI formatting' + ) + def test_format_right_justify_and_int(self): s = AnsiString('This string will be formatted bold and red, right justify') self.assertEqual( @@ -101,5 +108,21 @@ def test_no_format_and_rgb_functions2(self): '\x1b[38;2;138;43;226;48;2;100;232;170;4;58;2;255;99;71mManually adjust colors of foreground, background, and underline\x1b[m' ) + def test_add(self): + s = AnsiString('bold', 'bold') + AnsiString('red', 'red') + self.assertEqual( + str(s), + '\x1b[1mbold\x1b[0;31mred\x1b[m' + ) + + def test_iadd(self): + s = AnsiString('part bold') + s.apply_formatting(AnsiFormat.BOLD, 0, 3) + s += AnsiString('red', 'red') + self.assertEqual( + str(s), + '\x1b[1mpar\x1b[mt bold\x1b[31mred\x1b[m' + ) + if __name__ == '__main__': unittest.main()