@@ -931,6 +931,13 @@ def __eq__(self, value) -> bool:
931
931
return value .add == self .add and value .rem == self .rem
932
932
return False
933
933
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
+
934
941
def __init__ (self , s :str = '' , * setting_or_settings :Union [List [str ], str , List [int ], int , List [AnsiFormat ], AnsiFormat ]):
935
942
self ._s = s
936
943
# Key is the string index to make a color change at
@@ -974,16 +981,6 @@ def base_str(self) -> str:
974
981
def copy (self ) -> 'AnsiString' :
975
982
return self [:]
976
983
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
-
987
984
@staticmethod
988
985
def _shift_settings_idx (settings_dict :Dict [int ,'AnsiString.SettingPoint' ], num :int , keep_origin :bool ):
989
986
'''
@@ -996,7 +993,9 @@ def _shift_settings_idx(settings_dict:Dict[int,'AnsiString.SettingPoint'], num:i
996
993
settings_dict [new_key ] = settings_dict .pop (key )
997
994
998
995
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 )
1000
999
1001
1000
def apply_formatting (
1002
1001
self ,
@@ -1404,40 +1403,6 @@ def istitle(self) -> bool:
1404
1403
def isupper (self ) -> bool :
1405
1404
return self ._s .isupper ()
1406
1405
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
-
1441
1406
def __add__ (self , value :Union [str ,'AnsiString' ]) -> 'AnsiString' :
1442
1407
cpy = self .copy ()
1443
1408
cpy += value
@@ -1456,7 +1421,6 @@ def __iadd__(self, value:Union[str,'AnsiString']) -> 'AnsiString':
1456
1421
self ._color_settings [key ].rem .extend (value .rem )
1457
1422
else :
1458
1423
self ._color_settings [key ] = value
1459
- self .simplify_settings ()
1460
1424
else :
1461
1425
raise ValueError (f'value is invalid type: { type (value )} ' )
1462
1426
return self
0 commit comments