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 1 commit
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
21 changes: 11 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.default_columns()
self.speed_estimate_period = speed_estimate_period

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

@classmethod
def default_columns(cls) -> Tuple[ProgressColumn, ...]:
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 +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,
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.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