Skip to content

Commit cf697a8

Browse files
authored
Merge pull request #220 from cinderblockgames/main
Managing addition of space at end of text
2 parents f7971b6 + 51ba2d1 commit cf697a8

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

adafruit_display_text/scrolling_label.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def __init__(
6565
self.animate_time = animate_time
6666
self._current_index = current_index
6767
self._last_animate_time = -1
68-
self.max_characters = max_characters
68+
self._max_characters = max_characters
6969

70-
if text and text[-1] != " ":
70+
if text and text[-1] != " " and len(text) > max_characters:
7171
text = f"{text} "
7272
self._full_text = text
7373

74-
self.update()
74+
self.update(True)
7575

7676
def update(self, force: bool = False) -> None:
7777
"""Attempt to update the display. If ``animate_time`` has elapsed since
@@ -137,7 +137,7 @@ def full_text(self) -> str:
137137

138138
@full_text.setter
139139
def full_text(self, new_text: str) -> None:
140-
if new_text and new_text[-1] != " ":
140+
if new_text and new_text[-1] != " " and len(new_text) > self.max_characters:
141141
new_text = f"{new_text} "
142142
if new_text != self._full_text:
143143
self._full_text = new_text
@@ -156,3 +156,22 @@ def text(self):
156156
@text.setter
157157
def text(self, new_text):
158158
self.full_text = new_text
159+
160+
@property
161+
def max_characters(self):
162+
"""The maximum number of characters to display on screen.
163+
164+
:return int: The maximum character length of this label.
165+
"""
166+
return self._max_characters
167+
168+
@max_characters.setter
169+
def max_characters(self, new_max_characters):
170+
"""Recalculate the full text based on the new max characters.
171+
172+
This is necessary to correctly handle the potential space at the end of
173+
the text.
174+
"""
175+
if new_max_characters != self._max_characters:
176+
self._max_characters = new_max_characters
177+
self.full_text = self.full_text

0 commit comments

Comments
 (0)