Skip to content

Commit

Permalink
Merge pull request #1894 from ptmcg/ptmcg/add_default_columns_classme…
Browse files Browse the repository at this point in the history
…thod

Add default_columns classmethod to Progress class
  • Loading branch information
willmcgugan committed Feb 4, 2022
2 parents 94273ee + 76e234b commit b595887
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
43 changes: 33 additions & 10 deletions rich/progress.py
Expand Up @@ -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.get_default_columns()
self.speed_estimate_period = speed_estimate_period

self.disable = disable
Expand All @@ -613,6 +608,37 @@ def __init__(
self.print = self.console.print
self.log = self.console.log

@classmethod
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(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeRemainingColumn(),
)

@property
def console(self) -> Console:
return self.live.console
Expand Down Expand Up @@ -1015,10 +1041,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.get_default_columns(),
TimeElapsedColumn(),
console=console,
transient=True,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_progress.py
Expand Up @@ -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.get_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
Expand Down

0 comments on commit b595887

Please sign in to comment.