Skip to content

Commit

Permalink
Fixed __iadd__
Browse files Browse the repository at this point in the history
  • Loading branch information
Tails86 committed Apr 20, 2024
1 parent 5d61718 commit d9c74b6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/ansi_string/ansi_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/test_ansi_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()

0 comments on commit d9c74b6

Please sign in to comment.