Skip to content

Commit 3bd5bd6

Browse files
committed
- Refactored
- Removed simplify_settings because it caused issues, and it's impossible to fully handle properly
1 parent 1e15cb3 commit 3bd5bd6

File tree

2 files changed

+21
-48
lines changed

2 files changed

+21
-48
lines changed

src/ansi_string/ansi_string.py

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,13 @@ def __eq__(self, value) -> bool:
931931
return value.add == self.add and value.rem == self.rem
932932
return False
933933

934+
def insert_settings(self, apply:bool, settings:'AnsiString.Settings', topmost:bool=True):
935+
lst = self.add if apply else self.rem
936+
if topmost:
937+
lst.append(settings)
938+
else:
939+
lst.insert(0, settings)
940+
934941
def __init__(self, s:str='', *setting_or_settings:Union[List[str], str, List[int], int, List[AnsiFormat], AnsiFormat]):
935942
self._s = s
936943
# Key is the string index to make a color change at
@@ -974,16 +981,6 @@ def base_str(self) -> str:
974981
def copy(self) -> 'AnsiString':
975982
return self[:]
976983

977-
@staticmethod
978-
def _insert_settings_to_dict(settings_dict:Dict[int,'AnsiString.SettingPoint'], idx:int, apply:bool, settings:Settings, topmost:bool=True):
979-
if idx not in settings_dict:
980-
settings_dict[idx] = __class__.SettingPoint()
981-
lst = settings_dict[idx].add if apply else settings_dict[idx].rem
982-
if topmost:
983-
lst.append(settings)
984-
else:
985-
lst.insert(0, settings)
986-
987984
@staticmethod
988985
def _shift_settings_idx(settings_dict:Dict[int,'AnsiString.SettingPoint'], num:int, keep_origin:bool):
989986
'''
@@ -996,7 +993,9 @@ def _shift_settings_idx(settings_dict:Dict[int,'AnsiString.SettingPoint'], num:i
996993
settings_dict[new_key] = settings_dict.pop(key)
997994

998995
def _insert_settings(self, idx:int, apply:bool, settings:Settings, topmost:bool=True):
999-
__class__._insert_settings_to_dict(self._color_settings, idx, apply, settings, topmost)
996+
if idx not in self._color_settings:
997+
self._color_settings[idx] = __class__.SettingPoint()
998+
self._color_settings[idx].insert_settings(apply, settings, topmost)
1000999

10011000
def apply_formatting(
10021001
self,
@@ -1404,40 +1403,6 @@ def istitle(self) -> bool:
14041403
def isupper(self) -> bool:
14051404
return self._s.isupper()
14061405

1407-
def simplify_settings(self):
1408-
'''
1409-
Attempts to simplify ANSI formatting settings by removing redundant parameters. This does nothing to interrogate
1410-
custom formatting strings that were applied using "[" string prefix.
1411-
'''
1412-
previous_settings = [[],[]]
1413-
for idx, settings, current_settings in __class__.SettingsIterator(self._color_settings):
1414-
apply_list_original = list(settings.add)
1415-
1416-
# Remove settings that are redundantly reapplied
1417-
self._color_settings[idx].add = [
1418-
s for s in settings.add if s not in previous_settings
1419-
]
1420-
1421-
# Remove settings that are being applied and removed within the same index
1422-
remove_list = settings.rem
1423-
self._color_settings[idx].add = [
1424-
v for v in settings.add if v not in remove_list
1425-
]
1426-
self._color_settings[idx].rem = [
1427-
v for v in settings.rem if v not in apply_list_original
1428-
]
1429-
1430-
# Save for next loop
1431-
previous_settings = list(current_settings)
1432-
1433-
# Remove now empty indices
1434-
for idx in list(self._color_settings.keys()):
1435-
if (
1436-
not self._color_settings[idx].add
1437-
and not self._color_settings[idx].rem
1438-
):
1439-
del self._color_settings[idx]
1440-
14411406
def __add__(self, value:Union[str,'AnsiString']) -> 'AnsiString':
14421407
cpy = self.copy()
14431408
cpy += value
@@ -1456,7 +1421,6 @@ def __iadd__(self, value:Union[str,'AnsiString']) -> 'AnsiString':
14561421
self._color_settings[key].rem.extend(value.rem)
14571422
else:
14581423
self._color_settings[key] = value
1459-
self.simplify_settings()
14601424
else:
14611425
raise ValueError(f'value is invalid type: {type(value)}')
14621426
return self

tests/test_ansi_string.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,12 @@ def test_iterate(self):
314314
# Recreate the original string by iterating the characters
315315
for c in s:
316316
s2 += c
317-
# Should have created a whole new copy of the original
318-
self.assertEqual(str(s), str(s2))
317+
# This will look the same, even though each character now has formatting
318+
self.assertEqual(
319+
str(s2),
320+
'\x1b[43mo\x1b[0;43mn\x1b[0;43me\x1b[0;43m \x1b[0;4mt\x1b[0;4mw\x1b[0;4mo\x1b[0;4m \x1b[0;1mt\x1b[0;1mh'
321+
'\x1b[0;1mr\x1b[0;1me\x1b[0;1me\x1b[m'
322+
)
319323
self.assertIsNot(s, s2)
320324

321325
def test_apply_string_equal_length(self):
@@ -361,6 +365,11 @@ def test_remove_suffix_not_found(self):
361365
self.assertEqual(str(s), '\x1b[14mblah blah\x1b[m')
362366
self.assertIsNot(s, s2)
363367

368+
def test_cat_edge_case(self):
369+
a = AnsiString('a', 'red')
370+
b = AnsiString('b', 'red')
371+
c = a + b
372+
self.assertEqual(str(c), '\x1b[31ma\x1b[0;31mb\x1b[m')
364373

365374

366375
if __name__ == '__main__':

0 commit comments

Comments
 (0)