Skip to content

Commit e2c9f3e

Browse files
committed
Added accent substring APIs. Updated example to show more than one accent/color and removing.
1 parent ff6d63b commit e2c9f3e

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

adafruit_display_text/accent_label.py

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

examples/display_text_accent_label_simpletest.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@
1717

1818
main_group = displayio.Group()
1919

20-
accent_palette = displayio.Palette(4)
20+
accent_palette = displayio.Palette(6)
2121
accent_palette[2] = 0x000000
2222
accent_palette[3] = 0xDDDD00
23+
accent_palette[4] = 0xFFFFFF
24+
accent_palette[5] = 0x652F8F
2325

24-
quote_lbl = AccentLabel(terminalio.FONT, color_palette=accent_palette, text="", color=0xAAAAAA)
25-
quote_lbl.anchor_point = (0, 0)
26-
quote_lbl.anchored_position = (4, 4)
27-
main_group.append(quote_lbl)
26+
accent_lbl = AccentLabel(terminalio.FONT, color_palette=accent_palette, text="", color=0xAAAAAA)
27+
accent_lbl.anchor_point = (0, 0)
28+
accent_lbl.anchored_position = (4, 4)
29+
main_group.append(accent_lbl)
2830
display.root_group = main_group
2931

3032
text = "CircuitPython is amazing!"
31-
start_index = text.find("amazing!")
32-
end_index = start_index + len("amazing!")
33+
accent_lbl.text = text
3334

34-
quote_lbl.text = text
35-
quote_lbl.add_accent_range(start_index, end_index, 2, 3)
35+
time.sleep(1)
36+
accent_lbl.add_accent_to_substring("CircuitPython", 4, 5)
37+
time.sleep(2)
38+
accent_lbl.remove_accent_from_substring("CircuitPython")
39+
accent_lbl.add_accent_to_substring("amazing!", 2, 3)
3640

3741
while True:
3842
time.sleep(1)

0 commit comments

Comments
 (0)