From 150e713dde77c8e40c9f5b302347fb05c140fda1 Mon Sep 17 00:00:00 2001 From: Shubham Patel <165564832+shubham0x13@users.noreply.github.com> Date: Sun, 3 Nov 2024 20:51:54 +0530 Subject: [PATCH 1/2] Allow empty text and update text instantly --- adafruit_display_text/scrolling_label.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_display_text/scrolling_label.py b/adafruit_display_text/scrolling_label.py index 02d4ef6..476e27c 100644 --- a/adafruit_display_text/scrolling_label.py +++ b/adafruit_display_text/scrolling_label.py @@ -66,7 +66,7 @@ def __init__( self._last_animate_time = -1 self.max_characters = max_characters - if text[-1] != " ": + if text and text[-1] != " ": text = "{} ".format(text) self._full_text = text @@ -123,10 +123,10 @@ def current_index(self) -> int: @current_index.setter def current_index(self, new_index: int) -> None: - if new_index < len(self.full_text): - self._current_index = new_index - else: + if self.full_text: self._current_index = new_index % len(self.full_text) + else: + self._current_index = 0 @property def full_text(self) -> str: @@ -139,11 +139,11 @@ def full_text(self) -> str: @full_text.setter def full_text(self, new_text: str) -> None: - if new_text[-1] != " ": + if new_text and new_text[-1] != " ": new_text = "{} ".format(new_text) self._full_text = new_text self.current_index = 0 - self.update() + self.update(True) @property def text(self): From f237d6cf0c8fb9e8f96e8f5665d00049d6ffe3c4 Mon Sep 17 00:00:00 2001 From: Shubham Patel <165564832+shubham0x13@users.noreply.github.com> Date: Tue, 5 Nov 2024 19:07:37 +0530 Subject: [PATCH 2/2] Optimize `full_text` setter to skip redundant updates --- adafruit_display_text/scrolling_label.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_display_text/scrolling_label.py b/adafruit_display_text/scrolling_label.py index 476e27c..2a2b6e5 100644 --- a/adafruit_display_text/scrolling_label.py +++ b/adafruit_display_text/scrolling_label.py @@ -141,9 +141,10 @@ def full_text(self) -> str: def full_text(self, new_text: str) -> None: if new_text and new_text[-1] != " ": new_text = "{} ".format(new_text) - self._full_text = new_text - self.current_index = 0 - self.update(True) + if new_text != self._full_text: + self._full_text = new_text + self.current_index = 0 + self.update(True) @property def text(self):