@@ -67,21 +67,66 @@ def add_accent_range(self, start, end, foreground_color, background_color):
6767 self ._accent_ranges .append ((start , end , foreground_color , background_color ))
6868 self ._reset_text (text = str (self ._text ))
6969
70- def remove_accent_range (self , start , end , foreground_color , background_color ):
70+ def remove_accent_range (self , start ):
7171 """
72- Remove the accent for the specified range and colors .
72+ Remove the accent that starts at the specified index within the text .
7373
7474 :param start: The start index of the range of accented text, inclusive.
75- :param end: The end index of the range of accented text, exclusive.
76- :param foreground_color: The color index within ``color_palette`` used for
77- the accent foreground color.
78- :param background_color: The color index within ``color_palette`` used for
79- the accent background color.
8075 :return: None
8176 """
82- self ._accent_ranges .remove ((start , end , foreground_color , background_color ))
77+ for accent_range in reversed (self ._accent_ranges ):
78+ if accent_range [0 ] == start :
79+ self ._accent_ranges .remove (accent_range )
8380 self ._reset_text (text = str (self ._text ))
8481
82+ def add_accent_to_substring (self , substring , foreground_color , background_color , start = 0 ):
83+ """
84+ Add accent to the first occurrence of ``substring`` found in the labels text,
85+ starting from ``start``.
86+
87+ :param substring: the substring to accent within the text.
88+ :param foreground_color: The color index within ``color_palette`` to use for
89+ the accent foreground color.
90+ :param background_color: The color index within ``color_palette`` to use for
91+ the accent background color.
92+ :param start: The index within text to start searching for the substring.
93+ Defaults is 0 to search the whole text.
94+ :return: True if the substring was found, False otherwise.
95+ """
96+
97+ index = self ._text .find (substring , start )
98+ if index != - 1 :
99+ self .add_accent_range (index , index + len (substring ), foreground_color , background_color )
100+ return True
101+ else :
102+ return False
103+
104+ def remove_accent_from_substring (self , substring , start = 0 ):
105+ """
106+ Remove the accent for the first instance of the specified ``substring``
107+ starting at ``start``.
108+
109+ :param substring: the substring to accent within the text.
110+ :param start: The index within text to start searching for the substring.
111+ Defaults is 0 to search the whole text.
112+ :return: True if the substring was found, False otherwise.
113+ """
114+
115+ index = self ._text .find (substring , start )
116+ if index != - 1 :
117+ self .remove_accent_range (index )
118+ return True
119+ else :
120+ return False
121+
122+ @property
123+ def accent_ranges (self ):
124+ """
125+ The list of ranges that are accented.
126+ :return: List of Tuples containing (start, end, foreground_color, background_color).
127+ """
128+ return self ._accent_ranges
129+
85130 def clear_accent_ranges (self ):
86131 """
87132 Remove all accents from the text. All text will return to default
0 commit comments