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 condensed time column #1992

Merged
merged 3 commits into from Feb 27, 2022
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
24 changes: 24 additions & 0 deletions rich/progress.py
Expand Up @@ -357,6 +357,30 @@ def render(self, task: "Task") -> Text:
return Text(str(remaining_delta), style="progress.remaining")


class CondensedTimeColumn(ProgressColumn):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming isn't clear. What is condensed time? How is it different from TimeRemaining or TimeElapsed? I know the answers, but I don't think it would be obvious to a user.

I do like the functionality. But if we add it I think it should be rolled in to the TimeRemainingColumn class so that the defaults continue to work as before, but with options for the new features.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't love Condensed, but I chose that thinking of "a shorter time column with more information". Maybe Compact would be clearer.

However, I'm happy to roll it into TimeRemainingColumn. How about:

class TimeRemainingColumn(ProgressColumn):
    """Renders estimated time remaining.

    Args:
        compact (bool, optional): Render MM:SS when time remaining is less than an hour. Defaults to False.
        elapsed_when_finished (bool, optional): Render time elapsed when the task is finished. Defaults to False.
    """

Or, should compact (or another name) encompass both behaviors? Or ???

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

"""Renders estimated time remaining, or elapsed time when the task is finished."""

# Only refresh twice a second to prevent jitter
max_refresh = 0.5

def render(self, task: "Task") -> Text:
"""Show time."""
style = "progress.elapsed" if task.finished else "progress.remaining"
task_time = task.finished_time if task.finished else task.time_remaining
if task_time is None:
return Text("--:--", style=style)

# Based on https://github.com/tqdm/tqdm/blob/master/tqdm/std.py
minutes, seconds = divmod(int(task_time), 60)
hours, minutes = divmod(minutes, 60)
if hours:
formatted = f"{hours:d}:{minutes:02d}:{seconds:02d}"
else:
formatted = f"{minutes:02d}:{seconds:02d}"

return Text(formatted, style=style)


class FileSizeColumn(ProgressColumn):
"""Renders completed filesize."""

Expand Down
23 changes: 23 additions & 0 deletions tests/test_progress.py
Expand Up @@ -2,6 +2,7 @@

import io
from time import sleep
from types import SimpleNamespace

import pytest

Expand All @@ -22,6 +23,7 @@
TextColumn,
TimeElapsedColumn,
TimeRemainingColumn,
CondensedTimeColumn,
track,
_TrackThread,
TaskID,
Expand Down Expand Up @@ -89,6 +91,27 @@ class FakeTask(Task):
assert str(text) == "0:01:00"


@pytest.mark.parametrize("finished", [False, True])
@pytest.mark.parametrize(
"task_time, formatted",
[
(None, "--:--"),
(0, "00:00"),
(59, "00:59"),
(71, "01:11"),
(4210, "1:10:10"),
],
)
def test_condensed_time_column(finished, task_time, formatted):
if finished:
task = SimpleNamespace(finished=finished, finished_time=task_time)
else:
task = SimpleNamespace(finished=finished, time_remaining=task_time)

column = CondensedTimeColumn()
assert str(column.render(task)) == formatted


def test_renderable_column():
column = RenderableColumn("foo")
task = Task(1, "test", 100, 20, _get_time=lambda: 1.0)
Expand Down