Skip to content

node_deployer.cli

SingletonProgress

Bases: Progress

A singleton progress bar

Source code in src/node_deployer/cli.py
11
12
13
class SingletonProgress(Progress, metaclass=Singleton):
    """A singleton progress bar"""
    pass

cli_spinner(*spinner_args, **spinner_kwargs)

A decorator that adds a spinner to the CLI while the decorated function is running

Parameters:

Name Type Description Default
*spinner_args

The arguments to pass to the rich spinner object

()
**spinner_kwargs

The keyword arguments to pass to the rich spinner object

{}

Returns:

Name Type Description
Callable Callable

The decorated function

Source code in src/node_deployer/cli.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def cli_spinner(*spinner_args, **spinner_kwargs) -> Callable:
    """A decorator that adds a spinner to the CLI while the decorated function is running

    Args:
        *spinner_args: The arguments to pass to the rich spinner object
        **spinner_kwargs: The keyword arguments to pass to the rich spinner object

    Returns:
        Callable: The decorated function
    """
    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

Last update: November 7, 2023
Created: November 7, 2023