From 526560ea54eed31af311df0f16f5314e860a7e80 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Thu, 14 May 2020 20:35:09 +0200 Subject: [PATCH] [core/decorators] use difflib to make scrolling reset detection fuzzy when scrolling text that is subject to *slight* changes (e.g. a song that contains the current position within the song), allow for slight variations in the displayed text. fixes #629 --- bumblebee_status/core/decorators.py | 6 +++++- tests/core/test_decorators.py | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/bumblebee_status/core/decorators.py b/bumblebee_status/core/decorators.py index 8c87308..5b8619e 100644 --- a/bumblebee_status/core/decorators.py +++ b/bumblebee_status/core/decorators.py @@ -1,3 +1,4 @@ +import difflib import util.format @@ -28,7 +29,10 @@ def scrollable(func): if not text: return text - if text != widget.get("__content__", text): + if ( + difflib.SequenceMatcher(a=text, b=widget.get("__content__", text)).ratio() + < 0.9 + ): widget.set("scrolling.start", 0) widget.set("scrolling.direction", "right") widget.set("__content__", text) diff --git a/tests/core/test_decorators.py b/tests/core/test_decorators.py index 9ad7986..8b30338 100644 --- a/tests/core/test_decorators.py +++ b/tests/core/test_decorators.py @@ -70,5 +70,16 @@ class config(unittest.TestCase): self.module.text = "wxyz" self.assertEqual("wx", self.module.get(self.widget)) + def test_minimum_changed_data(self): + self.module.text = "this is a sample song (0:00)" + self.module.set("scrolling.width", 10) + self.assertEqual(self.module.text[0:10], self.module.get(self.widget)) + self.module.text = "this is a sample song (0:01)" + self.assertEqual(self.module.text[1:11], self.module.get(self.widget)) + self.module.text = "this is a sample song (0:12)" + self.assertEqual(self.module.text[2:12], self.module.get(self.widget)) + self.module.text = "this is a different song (0:12)" + self.assertEqual(self.module.text[0:10], self.module.get(self.widget)) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4