add CLI loading animation, closes #4
This commit is contained in:
parent
8521692dc8
commit
3ab468205f
@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "dopt-basics"
|
name = "dopt-basics"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
description = "basic cross-project tools for Python-based d-opt projects"
|
description = "basic cross-project tools for Python-based d-opt projects"
|
||||||
authors = [
|
authors = [
|
||||||
{name = "Florian Förster", email = "f.foerster@d-opt.com"},
|
{name = "Florian Förster", email = "f.foerster@d-opt.com"},
|
||||||
@ -69,7 +69,7 @@ directory = "reports/coverage"
|
|||||||
|
|
||||||
|
|
||||||
[tool.bumpversion]
|
[tool.bumpversion]
|
||||||
current_version = "0.2.0"
|
current_version = "0.2.1"
|
||||||
parse = """(?x)
|
parse = """(?x)
|
||||||
(?P<major>0|[1-9]\\d*)\\.
|
(?P<major>0|[1-9]\\d*)\\.
|
||||||
(?P<minor>0|[1-9]\\d*)\\.
|
(?P<minor>0|[1-9]\\d*)\\.
|
||||||
|
|||||||
68
src/dopt_basics/cli.py
Normal file
68
src/dopt_basics/cli.py
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import shutil
|
||||||
|
import time
|
||||||
|
import typing as t
|
||||||
|
from collections.abc import Callable
|
||||||
|
from functools import wraps
|
||||||
|
from itertools import cycle
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
P = t.ParamSpec("P")
|
||||||
|
T = t.TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
# based on: https://stackoverflow.com/questions/22029562/python-how-to-make-simple-animated-loading-while-process-is-running
|
||||||
|
class LoadingAnimation:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
loading_txt: str,
|
||||||
|
ending_text: str,
|
||||||
|
timeout: float = 0.1,
|
||||||
|
) -> None:
|
||||||
|
self.loading_txt = loading_txt
|
||||||
|
self.ending_text = ending_text
|
||||||
|
self.timeout = timeout
|
||||||
|
self.done: bool = False
|
||||||
|
self.steps: list[str] = ["⢿", "⣻", "⣽", "⣾", "⣷", "⣯", "⣟", "⡿"]
|
||||||
|
|
||||||
|
self._thread: Thread = Thread(target=self._animation, daemon=True)
|
||||||
|
|
||||||
|
def __enter__(self) -> None:
|
||||||
|
self.start()
|
||||||
|
|
||||||
|
def __exit__(
|
||||||
|
self,
|
||||||
|
exc_type,
|
||||||
|
exc_value,
|
||||||
|
tb,
|
||||||
|
) -> None:
|
||||||
|
self.stop()
|
||||||
|
|
||||||
|
def _animation(self) -> None:
|
||||||
|
for c in cycle(self.steps):
|
||||||
|
if self.done:
|
||||||
|
break
|
||||||
|
print(f"\r{c} {self.loading_txt}", flush=True, end="")
|
||||||
|
time.sleep(self.timeout)
|
||||||
|
|
||||||
|
def start(self) -> None:
|
||||||
|
self._thread.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.done = True
|
||||||
|
cols = shutil.get_terminal_size((80, 20)).columns
|
||||||
|
print("\r" + " " * cols, end="", flush=True)
|
||||||
|
print(f"\r{self.ending_text}", flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
def default_loading(func: Callable[P, T]) -> Callable[P, T]:
|
||||||
|
@wraps(func)
|
||||||
|
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
|
||||||
|
loading_txt: str = "Performing operation..."
|
||||||
|
ending_text: str = "Operation successful"
|
||||||
|
|
||||||
|
with LoadingAnimation(loading_txt, ending_text):
|
||||||
|
res = func(*args, **kwargs)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
return wrapper
|
||||||
Loading…
x
Reference in New Issue
Block a user