From 0060a9eb7d0d130b7073f363840688983968f192 Mon Sep 17 00:00:00 2001 From: Haskely Date: Thu, 30 Nov 2023 21:22:24 +0800 Subject: [PATCH 1/2] Add 'completed' parameter to 'track' function for resumable progress This commit introduces a new 'completed' parameter to the 'track' function in order to support resumable progress. Now users can specify the starting point for progress, making it easier to implement progress resumption. --- rich/progress.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rich/progress.py b/rich/progress.py index 8810aeac4..90a5e1b2b 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -104,6 +104,7 @@ def track( sequence: Union[Sequence[ProgressType], Iterable[ProgressType]], description: str = "Working...", total: Optional[float] = None, + completed: int = 0, auto_refresh: bool = True, console: Optional[Console] = None, transient: bool = False, @@ -123,6 +124,7 @@ def track( sequence (Iterable[ProgressType]): A sequence (must support "len") you wish to iterate over. description (str, optional): Description of task show next to progress bar. Defaults to "Working". total: (float, optional): Total number of steps. Default is len(sequence). + completed (int, optional): Number of steps completed so far. Defaults to 0. auto_refresh (bool, optional): Automatic refresh, disable to force a refresh after each iteration. Default is True. transient: (bool, optional): Clear the progress on exit. Defaults to False. console (Console, optional): Console to write to. Default creates internal Console instance. @@ -166,7 +168,7 @@ def track( with progress: yield from progress.track( - sequence, total=total, description=description, update_period=update_period + sequence, total=total, completed=completed, description=description, update_period=update_period ) @@ -1180,6 +1182,7 @@ def track( self, sequence: Union[Iterable[ProgressType], Sequence[ProgressType]], total: Optional[float] = None, + completed: int = 0, task_id: Optional[TaskID] = None, description: str = "Working...", update_period: float = 0.1, @@ -1189,6 +1192,7 @@ def track( Args: sequence (Sequence[ProgressType]): A sequence of values you want to iterate over and track progress. total: (float, optional): Total number of steps. Default is len(sequence). + completed (int, optional): Number of steps completed so far. Defaults to 0. task_id: (TaskID): Task to track. Default is new task. description: (str, optional): Description of task, if new task is created. update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1. @@ -1200,7 +1204,7 @@ def track( total = float(length_hint(sequence)) or None if task_id is None: - task_id = self.add_task(description, total=total) + task_id = self.add_task(description, total=total, completed=completed) else: self.update(task_id, total=total) From d0a996204ab987fdceaaba35cdeae6a21da2c9fd Mon Sep 17 00:00:00 2001 From: Haskely Date: Thu, 30 Nov 2023 21:30:21 +0800 Subject: [PATCH 2/2] Update progress.py --- rich/progress.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rich/progress.py b/rich/progress.py index 90a5e1b2b..63fd8d4ad 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -1206,7 +1206,7 @@ def track( if task_id is None: task_id = self.add_task(description, total=total, completed=completed) else: - self.update(task_id, total=total) + self.update(task_id, total=total, completed=completed) if self.live.auto_refresh: with _TrackThread(self, task_id, update_period) as track_thread: