From a81caf7f653c39005a398959278afd17c7936d4d Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 16 Mar 2024 14:45:20 +0100 Subject: [PATCH] spinner: add a debug mode with extra logging Spinners run very quickly and generate a lot of events. If we forget to stop() a spinner, it will continue running forever until the event loop is closed. We now add an optional debug parameter that will cause spinner events to be logged. It is very useful to find out if we forgot to stop spinners. Signed-off-by: Olivier Gayot --- subiquitycore/ui/spinner.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/subiquitycore/ui/spinner.py b/subiquitycore/ui/spinner.py index 5a16065d..b36c5685 100644 --- a/subiquitycore/ui/spinner.py +++ b/subiquitycore/ui/spinner.py @@ -14,6 +14,7 @@ # along with this program. If not, see . import asyncio +import logging from urwid import Text @@ -41,8 +42,18 @@ styles = { } +log = logging.getLogger("subiquitycore.ui.spinner") + + class Spinner(Text): - def __init__(self, style="spin", align="center"): + """An animation used when loading elements. + Use the .start() method to start the animation. You must ensure that you + call .stop() otherwise the Spinner will continue firing events until the + event loop is closed (even if the spinner is no longer visible on the + screen).""" + + def __init__(self, style="spin", align="center", *, debug=False): + self.debug = debug self.spin_index = 0 self.spin_text = styles[style]["texts"] self.rate = styles[style]["rate"] @@ -50,6 +61,8 @@ class Spinner(Text): self._spin_task = None def spin(self): + if self.debug: + log.debug("spinning spinner %s", id(self)) self.spin_index = (self.spin_index + 1) % len(self.spin_text) self.set_text(self.spin_text[self.spin_index]) @@ -59,10 +72,14 @@ class Spinner(Text): await asyncio.sleep(self.rate) def start(self): + if self.debug: + log.debug("starting spinner %s", id(self)) self.stop() self._spin_task = asyncio.create_task(self._spin()) def stop(self): + if self.debug: + log.debug("stopping spinner %s", id(self)) self.set_text("") if self._spin_task is not None: self._spin_task.cancel()