mirror of
https://github.com/THIS-IS-NOT-A-BACKUP/zspotify.git
synced 2024-11-29 19:24:34 +01:00
added loader class for long running tasks
This commit is contained in:
parent
9a48746b79
commit
401688a458
@ -280,3 +280,84 @@ def fmt_seconds(secs: float) -> str:
|
|||||||
return f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
|
return f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
|
||||||
else:
|
else:
|
||||||
return f'{h}'.zfill(2) + ':' + f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
|
return f'{h}'.zfill(2) + ':' + f'{m}'.zfill(2) + ':' + f'{s}'.zfill(2)
|
||||||
|
|
||||||
|
|
||||||
|
# load symbol from:
|
||||||
|
# https://stackoverflow.com/questions/22029562/python-how-to-make-simple-animated-loading-while-process-is-running
|
||||||
|
|
||||||
|
# imports
|
||||||
|
from itertools import cycle
|
||||||
|
from shutil import get_terminal_size
|
||||||
|
from threading import Thread
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
class Loader:
|
||||||
|
"""Busy symbol.
|
||||||
|
|
||||||
|
Can be called inside a context:
|
||||||
|
|
||||||
|
with Loader("This take some Time..."):
|
||||||
|
# do something
|
||||||
|
pass
|
||||||
|
"""
|
||||||
|
def __init__(self, desc="Loading...", end='', timeout=0.3, mode='std2'):
|
||||||
|
"""
|
||||||
|
A loader-like context manager
|
||||||
|
|
||||||
|
Args:
|
||||||
|
desc (str, optional): The loader's description. Defaults to "Loading...".
|
||||||
|
end (str, optional): Final print. Defaults to "".
|
||||||
|
timeout (float, optional): Sleep time between prints. Defaults to 0.1.
|
||||||
|
"""
|
||||||
|
self.desc = desc
|
||||||
|
self.end = end
|
||||||
|
self.timeout = timeout
|
||||||
|
|
||||||
|
self._thread = Thread(target=self._animate, daemon=True)
|
||||||
|
if mode == 'std1':
|
||||||
|
self.steps = ["⢿", "⣻", "⣽", "⣾", "⣷", "⣯", "⣟", "⡿"]
|
||||||
|
elif mode == 'std2':
|
||||||
|
self.steps = ["◜","◝","◞","◟"]
|
||||||
|
elif mode == 'std3':
|
||||||
|
self.steps = ["😐 ","😐 ","😮 ","😮 ","😦 ","😦 ","😧 ","😧 ","🤯 ","💥 ","✨ ","\u3000 ","\u3000 ","\u3000 "]
|
||||||
|
elif mode == 'prog':
|
||||||
|
self.steps = ["[∙∙∙]","[●∙∙]","[∙●∙]","[∙∙●]","[∙∙∙]"]
|
||||||
|
|
||||||
|
self.done = False
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self._thread.start()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def _animate(self):
|
||||||
|
for c in cycle(self.steps):
|
||||||
|
if self.done:
|
||||||
|
break
|
||||||
|
print(f"\r\t☐ {c} {self.desc} ", flush=True, end="")
|
||||||
|
sleep(self.timeout)
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.done = True
|
||||||
|
cols = get_terminal_size((80, 20)).columns
|
||||||
|
print("\r" + " " * cols, end="", flush=True)
|
||||||
|
|
||||||
|
if self.end != "":
|
||||||
|
print(f"\r{self.end}", flush=True)
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, tb):
|
||||||
|
# handle exceptions with those variables ^
|
||||||
|
self.stop()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with Loader("Loading with context manager..."):
|
||||||
|
for i in range(10):
|
||||||
|
sleep(0.25)
|
||||||
|
|
||||||
|
loader = Loader("Loading with object...", "That was fast!", 0.05).start()
|
||||||
|
for i in range(10):
|
||||||
|
sleep(0.25)
|
||||||
|
loader.stop()
|
||||||
|
Loading…
Reference in New Issue
Block a user