mirror of
https://github.com/Cian-H/I-Form_Server_Node_Deployer.git
synced 2025-12-23 14:42:02 +00:00
Improved CLI
This commit is contained in:
36
cli.py
Normal file
36
cli.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from functools import wraps
|
||||
import inspect
|
||||
from typing import Callable
|
||||
|
||||
from rich.progress import Progress, SpinnerColumn, TextColumn
|
||||
|
||||
import config
|
||||
from utils import Singleton
|
||||
|
||||
|
||||
class SingletonProgress(Progress, metaclass=Singleton):
|
||||
pass
|
||||
|
||||
|
||||
def cli_spinner(*spinner_args, **spinner_kwargs) -> Callable:
|
||||
def decorator(f: Callable) -> Callable:
|
||||
# Indent the spinner to match its nesting level
|
||||
indent = len(inspect.stack()) - 1
|
||||
spinner_kwargs["indent"] = f"├{"─"*indent}► "
|
||||
|
||||
@wraps(f)
|
||||
def wrapped(*func_args, **func_kwargs):
|
||||
if not config.CLI:
|
||||
return f(*func_args, **func_kwargs)
|
||||
with SingletonProgress(
|
||||
SpinnerColumn(),
|
||||
TextColumn("{task.fields[indent]}[progress.description]{task.description}"),
|
||||
transient=True,
|
||||
expand=True,
|
||||
) as progress:
|
||||
task_id = progress.add_task(*spinner_args, **spinner_kwargs)
|
||||
out = f(*func_args, **func_kwargs)
|
||||
progress.stop_task(task_id)
|
||||
return out
|
||||
return wrapped
|
||||
return decorator
|
||||
Reference in New Issue
Block a user