From a8df4a5f9d8bff9881ee0a2009e7f5fb37d5b2b5 Mon Sep 17 00:00:00 2001 From: Pavle Portic Date: Fri, 15 May 2020 20:31:00 +0200 Subject: [PATCH] Handle n+1 characters in the scrolling decorator --- .gitignore | 3 +++ bumblebee_status/core/decorators.py | 9 +++++++-- tests/core/test_decorators.py | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4996eaa..2bf1ac8 100644 --- a/.gitignore +++ b/.gitignore @@ -94,3 +94,6 @@ ENV/ # Visual studio project files .vscode/ + +# mypy cache +.mypy_cache diff --git a/bumblebee_status/core/decorators.py b/bumblebee_status/core/decorators.py index 5b8619e..b117f7b 100644 --- a/bumblebee_status/core/decorators.py +++ b/bumblebee_status/core/decorators.py @@ -1,6 +1,10 @@ import difflib +import logging + import util.format +log = logging.getLogger(__name__) + def never(init): def call_init(obj, *args, **kwargs): @@ -49,9 +53,10 @@ def scrollable(func): direction = widget.get("scrolling.direction", "right") if direction == "left": - scroll_speed = -scroll_speed - if start + scroll_speed <= 0: # bounce back + if start - scroll_speed < 0: # bounce back widget.set("scrolling.direction", "right") + else: + scroll_speed = -scroll_speed next_start = start + scroll_speed if next_start + width > len(text): diff --git a/tests/core/test_decorators.py b/tests/core/test_decorators.py index 8b30338..4ad1410 100644 --- a/tests/core/test_decorators.py +++ b/tests/core/test_decorators.py @@ -81,5 +81,14 @@ class config(unittest.TestCase): self.module.text = "this is a different song (0:12)" self.assertEqual(self.module.text[0:10], self.module.get(self.widget)) + def test_n_plus_one(self): + self.module.text = "10 letters" + self.module.set("scrolling.width", 9) + self.assertEqual(self.module.text[0:9], self.module.get(self.widget)) + self.assertEqual(self.module.text[1:10], self.module.get(self.widget)) + self.assertEqual(self.module.text[0:9], self.module.get(self.widget)) + self.assertEqual(self.module.text[1:10], self.module.get(self.widget)) + self.assertEqual(self.module.text[0:9], self.module.get(self.widget)) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4