Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default_columns classmethod to Progress class #1894

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 (
ptmcg marked this conversation as resolved.
Show resolved Hide resolved
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