Skip to content

Commit

Permalink
+ Made progress bar indicator prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
caffeine-addictt committed Dec 10, 2023
1 parent 3761773 commit b52675e
Showing 1 changed file with 61 additions and 14 deletions.
75 changes: 61 additions & 14 deletions src/thread/cli/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import logging
from typing import Union

from rich.progress import Progress, TextColumn, BarColumn, TimeRemainingColumn, TimeElapsedColumn
from rich.live import Live
from rich.panel import Panel
from rich.console import Group
from rich.progress import Progress, TaskID, SpinnerColumn, TextColumn, BarColumn, TimeRemainingColumn, TimeElapsedColumn
from .utils import DebugOption, VerboseOption, QuietOption, verbose_args_processor, kwargs_processor

logger = logging.getLogger('base')
Expand Down Expand Up @@ -148,22 +151,66 @@ def process(
logger.info('Started parallel processes')
typer.echo('Waiting for parallel processes to complete, this may take a while...')

with Progress(
TextColumn('[bold blue]{task.description}', justify = 'right'),
BarColumn(bar_width = None),
'[progress.percentage]{task.percentage:>3.1f}%',

# Progress bar :D
threadCount = len(newProcess._threads)

thread_progress = Progress(
SpinnerColumn(),
TextColumn('{task.description}'),
'•',
TimeRemainingColumn(),
'•',
TimeElapsedColumn()
) as progress:
percentage = 0
job = progress.add_task('Working...', total = 100)

while percentage < 100:
percentage = round(sum(i.progress for i in newProcess._threads) / (len(newProcess._threads) or 8), 2) * 100
progress.update(job, completed = percentage)
BarColumn(bar_width = 80),
TextColumn('{task.percentage:>3.1f}%')
)
overall_progress = Progress(
TimeElapsedColumn(),
BarColumn(bar_width = 110),
TextColumn('{task.description}')
)

workerjobs: list[TaskID] = [
thread_progress.add_task(
f'[bold blue][T {threadNum}]',
total = 100
)
for threadNum in range(threadCount)
]
overalljob = overall_progress.add_task('(0 of ?)', total = 100)


with Live(
Group(
Panel(thread_progress),
overall_progress,
),
refresh_per_second = 10
):
completed = 0
while completed != threadCount:
i = 0
completed = 0
progressAvg = 0

for jobID in workerjobs:
jobProgress = newProcess._threads[i].progress
thread_progress.update(jobID, completed = round(jobProgress * 100, 2))
if jobProgress == 1:
thread_progress.stop_task(jobID)
thread_progress.update(jobID, description = '[bold green]Completed')
completed += 1

progressAvg += jobProgress
i += 1

# Update overall
overall_progress.update(
overalljob,
description = f'[bold {"green" if completed == threadCount else "#AAAAAA"}]({completed} of {threadCount})',
completed = round((progressAvg / threadCount) * 100, 2)
)
time.sleep(0.1)


result = newProcess.get_return_values()

Expand Down

0 comments on commit b52675e

Please sign in to comment.