|
| 1 | +import functools |
| 2 | + |
1 | 3 | from rich.progress import Progress as RichProgress
|
2 | 4 | from rich.progress import SpinnerColumn
|
3 | 5 | from rich.progress import TextColumn
|
4 | 6 |
|
5 | 7 |
|
6 | 8 | class Progress:
|
7 |
| - _instance = RichProgress(SpinnerColumn(), TextColumn("[white]{task.description}{task.fields[status]}")) |
| 9 | + _running = False |
| 10 | + _instance = RichProgress(SpinnerColumn(finished_text="*"), |
| 11 | + TextColumn("[white]{task.description}{task.fields[status]}")) |
8 | 12 |
|
9 |
| - def __init__(self, description, done="Process completed"): |
10 |
| - self.task = self._instance.add_task(description, status=" ... [yellow]processing") |
11 |
| - self.done = done |
| 13 | + def __init__(self, description, done_description): |
| 14 | + if self._running: |
| 15 | + self.task = self._instance.add_task(description, status=" ... [yellow]processing") |
| 16 | + self.done_description = done_description |
12 | 17 |
|
13 | 18 | def update(self, description, status=" ... [yellow]processing"):
|
14 |
| - self._instance.update(self.task, description=description, status=status, refresh=True) |
| 19 | + if self._running: |
| 20 | + self._instance.update(self.task, description=description, status=status, refresh=True) |
15 | 21 |
|
16 | 22 | @classmethod
|
17 | 23 | def start(cls):
|
| 24 | + cls._running = True |
18 | 25 | cls._instance.start()
|
19 | 26 |
|
20 | 27 | @classmethod
|
21 | 28 | def stop(cls):
|
| 29 | + cls._running = False |
22 | 30 | cls._instance.stop()
|
23 | 31 |
|
24 | 32 | def __enter__(self):
|
25 | 33 | return self
|
26 | 34 |
|
27 |
| - def __exit__(self, *_): |
28 |
| - self.update(self.done, status=" ... [green]done") |
| 35 | + def __exit__(self, exc_type, exc_val, _): |
| 36 | + # Set finished time to 0 to hide the spinner |
| 37 | + self._instance.tasks[self.task].finished_time = 0 |
| 38 | + if exc_type is not None: |
| 39 | + return self.update(exc_val, status=" ... [red]failure") |
| 40 | + self.update(self.done_description, status=" ... [green]success") |
0 commit comments