Skip to content

Commit

Permalink
Refactor RichProgressBar to use __next__
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed Jan 6, 2024
1 parent 7b3c896 commit 75451da
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions plextraktsync/rich/RichProgressBar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ def __init__(self, iterable, total, options=None, desc=""):
self.options = options or {}
self.desc = desc
self.total = total
self.i = 0

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

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

def __enter__(self):
self.progress.__enter__()
Expand All @@ -25,6 +24,14 @@ def __enter__(self):
def __exit__(self, *exc):
self.progress.__exit__(*exc)

def update(self):
self.i += 1
self.progress.update(self.task_id, completed=self.i)

@cached_property
def task_id(self):
return self.progress.add_task(self.desc, total=self.total)

@cached_property
def progress(self):
from tqdm.rich import FractionColumn, RateColumn
Expand Down

0 comments on commit 75451da

Please sign in to comment.