|
17 | 17 | import requests
|
18 | 18 | import requests_toolbelt
|
19 | 19 | import rich.progress
|
| 20 | +import rich.text |
20 | 21 | import urllib3
|
21 | 22 | from requests import adapters
|
22 | 23 | from requests_toolbelt.utils import user_agent
|
|
36 | 37 | logger = logging.getLogger(__name__)
|
37 | 38 |
|
38 | 39 |
|
| 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 | + |
39 | 64 | class Repository:
|
40 | 65 | def __init__(
|
41 | 66 | self,
|
@@ -154,8 +179,7 @@ def _upload(self, package: package_file.PackageFile) -> requests.Response:
|
154 | 179 | rich.progress.BarColumn(),
|
155 | 180 | rich.progress.DownloadColumn(),
|
156 | 181 | "•",
|
157 |
| - rich.progress.TimeElapsedColumn(), |
158 |
| - rich.progress.TimeRemainingColumn(), |
| 182 | + CondensedTimeColumn(), |
159 | 183 | "•",
|
160 | 184 | rich.progress.TransferSpeedColumn(),
|
161 | 185 | disable=self.disable_progress_bar,
|
|
0 commit comments