From b0794522a6a8f85d9110db2c1768d1c4f48162d9 Mon Sep 17 00:00:00 2001 From: ptmcg Date: Mon, 31 Jan 2022 08:29:01 -0600 Subject: [PATCH 1/2] Add default_columns classmethod to Progress class --- rich/progress.py | 21 +++++++++++---------- tests/test_progress.py | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/rich/progress.py b/rich/progress.py index 1f670db43..8b9713c7a 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -588,12 +588,7 @@ def __init__( refresh_per_second is None or refresh_per_second > 0 ), "refresh_per_second must be > 0" self._lock = RLock() - self.columns = columns or ( - TextColumn("[progress.description]{task.description}"), - BarColumn(), - TextColumn("[progress.percentage]{task.percentage:>3.0f}%"), - TimeRemainingColumn(), - ) + self.columns = columns or self.default_columns() self.speed_estimate_period = speed_estimate_period self.disable = disable @@ -613,6 +608,15 @@ def __init__( self.print = self.console.print self.log = self.console.log + @classmethod + def default_columns(cls) -> Tuple[ProgressColumn, ...]: + return ( + TextColumn("[progress.description]{task.description}"), + BarColumn(), + TextColumn("[progress.percentage]{task.percentage:>3.0f}%"), + TimeRemainingColumn(), + ) + @property def console(self) -> Console: return self.live.console @@ -1015,10 +1019,7 @@ def remove_task(self, task_id: TaskID) -> None: with Progress( SpinnerColumn(), - TextColumn("[progress.description]{task.description}"), - BarColumn(), - TextColumn("[progress.percentage]{task.percentage:>3.0f}%"), - TimeRemainingColumn(), + *Progress.default_columns(), TimeElapsedColumn(), console=console, transient=True, diff --git a/tests/test_progress.py b/tests/test_progress.py index 2020f91ff..876e5d99b 100644 --- a/tests/test_progress.py +++ b/tests/test_progress.py @@ -334,6 +334,32 @@ def test_columns() -> None: assert result == expected +def test_using_default_columns() -> None: + # can only check types, as the instances do not '==' each other + expected_default_types = [ + TextColumn, + BarColumn, + TextColumn, + TimeRemainingColumn, + ] + + progress = Progress() + assert [type(c) for c in progress.columns] == expected_default_types + + progress = Progress( + SpinnerColumn(), + *Progress.default_columns(), + "Elapsed:", + TimeElapsedColumn(), + ) + assert [type(c) for c in progress.columns] == [ + SpinnerColumn, + *expected_default_types, + str, + TimeElapsedColumn, + ] + + def test_task_create() -> None: task = Task(TaskID(1), "foo", 100, 0, _get_time=lambda: 1) assert task.elapsed is None From 76e234b79d8e8ae2c922c6576856bb5c613ce1e0 Mon Sep 17 00:00:00 2001 From: ptmcg Date: Mon, 31 Jan 2022 13:09:51 -0600 Subject: [PATCH 2/2] Rename default_columns to get_default_columns, and add docstring --- rich/progress.py | 28 +++++++++++++++++++++++++--- tests/test_progress.py | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/rich/progress.py b/rich/progress.py index 8b9713c7a..fe35b6c17 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -588,7 +588,7 @@ def __init__( refresh_per_second is None or refresh_per_second > 0 ), "refresh_per_second must be > 0" self._lock = RLock() - self.columns = columns or self.default_columns() + self.columns = columns or self.get_default_columns() self.speed_estimate_period = speed_estimate_period self.disable = disable @@ -609,7 +609,29 @@ def __init__( self.log = self.console.log @classmethod - def default_columns(cls) -> Tuple[ProgressColumn, ...]: + def get_default_columns(cls) -> Tuple[ProgressColumn, ...]: + """Get the default columns used for a new Progress instance: + - a text column for the description (TextColumn) + - the bar itself (BarColumn) + - a text column showing completion percentage (TextColumn) + - an estimated-time-remaining column (TimeRemainingColumn) + If the Progress instance is created without passing a columns argument, + the default columns defined here will be used. + + You can also create a Progress instance using custom columns before + and/or after the defaults, as in this example: + + progress = Progress( + SpinnerColumn(), + *Progress.default_columns(), + "Elapsed:", + TimeElapsedColumn(), + ) + + This code shows the creation of a Progress display, containing + a spinner to the left, the default columns, and a labeled elapsed + time column. + """ return ( TextColumn("[progress.description]{task.description}"), BarColumn(), @@ -1019,7 +1041,7 @@ def remove_task(self, task_id: TaskID) -> None: with Progress( SpinnerColumn(), - *Progress.default_columns(), + *Progress.get_default_columns(), TimeElapsedColumn(), console=console, transient=True, diff --git a/tests/test_progress.py b/tests/test_progress.py index 876e5d99b..20b9d32ed 100644 --- a/tests/test_progress.py +++ b/tests/test_progress.py @@ -348,7 +348,7 @@ def test_using_default_columns() -> None: progress = Progress( SpinnerColumn(), - *Progress.default_columns(), + *Progress.get_default_columns(), "Elapsed:", TimeElapsedColumn(), )