import time
from rich.console import Group
from rich.panel import Panel
from rich.live import Live
from rich.progress import (
BarColumn,
Progress,
SpinnerColumn,
TextColumn,
TimeElapsedColumn,
)
def run_steps(name, step_times, app_steps_task_id):
for idx, step_time in enumerate(step_times):
action = step_actions[idx]
step_task_id = step_progress.add_task("", action=action, name=name)
for _ in range(step_time):
time.sleep(0.5)
step_progress.update(step_task_id, advance=1)
step_progress.stop_task(step_task_id)
step_progress.update(step_task_id, visible=False)
app_steps_progress.update(app_steps_task_id, advance=1)
current_app_progress = Progress(
TimeElapsedColumn(),
TextColumn("{task.description}"),
)
step_progress = Progress(
TextColumn(" "),
TimeElapsedColumn(),
TextColumn("[bold purple]{task.fields[action]}"),
SpinnerColumn("simpleDots"),
)
app_steps_progress = Progress(
TextColumn(
"[bold blue]Progress for app {task.fields[name]}: {task.percentage:.0f}%"
),
BarColumn(),
TextColumn("({task.completed} of {task.total} steps done)"),
)
overall_progress = Progress(
TimeElapsedColumn(), BarColumn(), TextColumn("{task.description}")
)
progress_group = Group(
Panel(Group(current_app_progress, step_progress, app_steps_progress)),
overall_progress,
)
step_actions = ("downloading", "configuring", "building", "installing")
apps = [
("one", (2, 1, 4, 2)),
("two", (1, 3, 8, 4)),
("three", (2, 1, 3, 2)),
]
overall_task_id = overall_progress.add_task("", total=len(apps))
with Live(progress_group):
for idx, (name, step_times) in enumerate(apps):
top_descr = "[bold #AAAAAA](%d out of %d apps installed)" % (idx, len(apps))
overall_progress.update(overall_task_id, description=top_descr)
current_task_id = current_app_progress.add_task("Installing app %s" % name)
app_steps_task_id = app_steps_progress.add_task(
"", total=len(step_times), name=name
)
run_steps(name, step_times, app_steps_task_id)
app_steps_progress.update(app_steps_task_id, visible=False)
current_app_progress.stop_task(current_task_id)
current_app_progress.update(
current_task_id, description="[bold green]App %s installed!" % name
)
overall_progress.update(overall_task_id, advance=1)
overall_progress.update(
overall_task_id, description="[bold green]%s apps installed, done!" % len(apps)
)