Skip to content

Commit e79ce9d

Browse files
Managing addition of space at end of text
Wrapping max_characters in properties and checking for length of text before adding a space to full_text.
1 parent f7971b6 commit e79ce9d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

adafruit_display_text/scrolling_label.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ 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

7070
if text and text[-1] != " ":
7171
text = f"{text} "
@@ -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,21 @@ 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+
This is necessary to correctly handle the potential space at the end of
172+
the text.
173+
"""
174+
if new_max_characters != self._max_characters:
175+
self._max_characters = new_max_characters
176+
self.full_text = self.full_text

0 commit comments

Comments
 (0)