Skip to content

Commit

Permalink
Add ProgressBar class
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Jan 6, 2024
1 parent 1de34be commit 0800ebb
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions plextraktsync/rich/RichProgressBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from functools import cached_property


class RichProgressBar:
def __init__(self, iterable, total, options=None, desc=""):
self.iter = iterable
self.options = options or {}
self.desc = desc
self.total = total

def __iter__(self):
p = self.progress
task_id = p.add_task(self.desc, total=self.total)

i = 0
for it in self.iter:
yield it
i += 1
p.update(task_id, completed=i)

def __enter__(self):
self.progress.__enter__()
return self

def __exit__(self, *exc):
self.progress.__exit__(*exc)

@cached_property
def progress(self):
from rich.progress import Progress

return Progress(**self.options)

0 comments on commit 0800ebb

Please sign in to comment.