Skip to content

Managing addition of space at end of text #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions adafruit_display_text/scrolling_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ def __init__(
self.animate_time = animate_time
self._current_index = current_index
self._last_animate_time = -1
self.max_characters = max_characters
self._max_characters = max_characters

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

self.update()
self.update(True)

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

@full_text.setter
def full_text(self, new_text: str) -> None:
if new_text and new_text[-1] != " ":
if new_text and new_text[-1] != " " and len(new_text) > self.max_characters:
new_text = f"{new_text} "
if new_text != self._full_text:
self._full_text = new_text
Expand All @@ -156,3 +156,22 @@ def text(self):
@text.setter
def text(self, new_text):
self.full_text = new_text

@property
def max_characters(self):
"""The maximum number of characters to display on screen.

:return int: The maximum character length of this label.
"""
return self._max_characters

@max_characters.setter
def max_characters(self, new_max_characters):
"""Recalculate the full text based on the new max characters.

This is necessary to correctly handle the potential space at the end of
the text.
"""
if new_max_characters != self._max_characters:
self._max_characters = new_max_characters
self.full_text = self.full_text