Skip to content

Commit 7fed68d

Browse files
committed
Add custom time column
1 parent 530ec12 commit 7fed68d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

tests/test_repository.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ def dictfunc():
233233
default_repo.upload(package)
234234

235235

236+
@pytest.mark.parametrize("finished", [False, True])
237+
@pytest.mark.parametrize(
238+
"task_time, formatted",
239+
[
240+
(None, "--:--"),
241+
(0, "00:00"),
242+
(59, "00:59"),
243+
(71, "01:11"),
244+
(4210, "1:10:10"),
245+
],
246+
)
247+
def test_time_column_renders_condensed_time(finished, task_time, formatted):
248+
if finished:
249+
task = pretend.stub(finished=finished, finished_time=task_time)
250+
else:
251+
task = pretend.stub(finished=finished, time_remaining=task_time)
252+
253+
column = repository.CondensedTimeColumn()
254+
assert str(column.render(task)) == formatted
255+
256+
236257
def test_upload_retry(tmpdir, default_repo, capsys):
237258
"""Print retry messages when the upload response indicates a server error."""
238259
default_repo.disable_progress_bar = True

twine/repository.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import requests
1818
import requests_toolbelt
1919
import rich.progress
20+
import rich.text
2021
import urllib3
2122
from requests import adapters
2223
from requests_toolbelt.utils import user_agent
@@ -36,6 +37,30 @@
3637
logger = logging.getLogger(__name__)
3738

3839

40+
class CondensedTimeColumn(rich.progress.ProgressColumn):
41+
"""Renders estimated time remaining, or elapsed time when the task is finished."""
42+
43+
# Only refresh twice a second to prevent jitter
44+
max_refresh = 0.5
45+
46+
def render(self, task: rich.progress.Task) -> rich.text.Text:
47+
"""Show time."""
48+
style = "progress.elapsed" if task.finished else "progress.remaining"
49+
task_time = task.finished_time if task.finished else task.time_remaining
50+
if task_time is None:
51+
return rich.text.Text("--:--", style=style)
52+
53+
# Based on https://github.com/tqdm/tqdm/blob/master/tqdm/std.py
54+
minutes, seconds = divmod(int(task_time), 60)
55+
hours, minutes = divmod(minutes, 60)
56+
if hours:
57+
formatted = f"{hours:d}:{minutes:02d}:{seconds:02d}"
58+
else:
59+
formatted = f"{minutes:02d}:{seconds:02d}"
60+
61+
return rich.text.Text(formatted, style=style)
62+
63+
3964
class Repository:
4065
def __init__(
4166
self,
@@ -154,8 +179,7 @@ def _upload(self, package: package_file.PackageFile) -> requests.Response:
154179
rich.progress.BarColumn(),
155180
rich.progress.DownloadColumn(),
156181
"•",
157-
rich.progress.TimeElapsedColumn(),
158-
rich.progress.TimeRemainingColumn(),
182+
CondensedTimeColumn(),
159183
"•",
160184
rich.progress.TransferSpeedColumn(),
161185
disable=self.disable_progress_bar,

0 commit comments

Comments
 (0)