Skip to content

Commit e8072ad

Browse files
committed
GH-49: Implement error handling
1 parent 44c1101 commit e8072ad

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/thumbnails/progress.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1+
import functools
2+
13
from rich.progress import Progress as RichProgress
24
from rich.progress import SpinnerColumn
35
from rich.progress import TextColumn
46

57

68
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]}"))
812

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
1217

1318
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)
1521

1622
@classmethod
1723
def start(cls):
24+
cls._running = True
1825
cls._instance.start()
1926

2027
@classmethod
2128
def stop(cls):
29+
cls._running = False
2230
cls._instance.stop()
2331

2432
def __enter__(self):
2533
return self
2634

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

Comments
 (0)