mirror of https://github.com/Textualize/rich.git
Add flag to render in binary/decimal suffix in DownloadColumn
Add a test for rendering binary suffix in test_progress.py
This commit is contained in:
parent
d17b7f5110
commit
630ed8bd7f
|
@ -48,7 +48,6 @@ TaskID = NewType("TaskID", int)
|
||||||
|
|
||||||
ProgressType = TypeVar("ProgressType")
|
ProgressType = TypeVar("ProgressType")
|
||||||
|
|
||||||
|
|
||||||
GetTimeCallable = Callable[[], float]
|
GetTimeCallable = Callable[[], float]
|
||||||
|
|
||||||
|
|
||||||
|
@ -292,15 +291,30 @@ class TotalFileSizeColumn(ProgressColumn):
|
||||||
|
|
||||||
|
|
||||||
class DownloadColumn(ProgressColumn):
|
class DownloadColumn(ProgressColumn):
|
||||||
"""Renders file size downloaded and total, e.g. '0.5/2.3 GB'."""
|
"""Renders file size downloaded and total, e.g. '0.5/2.3 GB'.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
decimal_suffix (bool, optional): Flag to renser filesize in desired format, disable to render in binary. Defaults to True.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, decimal_suffix: bool = True) -> None:
|
||||||
|
self.decimal_ssuffix = decimal_suffix
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
def render(self, task: "Task") -> Text:
|
def render(self, task: "Task") -> Text:
|
||||||
"""Calculate common unit for completed and total."""
|
"""Calculate common unit for completed and total."""
|
||||||
completed = int(task.completed)
|
completed = int(task.completed)
|
||||||
total = int(task.total)
|
total = int(task.total)
|
||||||
|
if self.decimal_ssuffix:
|
||||||
unit, suffix = filesize.pick_unit_and_suffix(
|
unit, suffix = filesize.pick_unit_and_suffix(
|
||||||
total, ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], 1000
|
total, ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], 1000
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
unit, suffix = filesize.pick_unit_and_suffix(
|
||||||
|
total,
|
||||||
|
["bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"],
|
||||||
|
1024,
|
||||||
|
)
|
||||||
completed_ratio = completed / unit
|
completed_ratio = completed / unit
|
||||||
total_ratio = total / unit
|
total_ratio = total / unit
|
||||||
precision = 0 if unit == 1 else 1
|
precision = 0 if unit == 1 else 1
|
||||||
|
|
|
@ -88,6 +88,15 @@ def test_download_progress_uses_decimal_units() -> None:
|
||||||
assert rendered_progress == expected
|
assert rendered_progress == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_download_progress_uses_binary_units() -> None:
|
||||||
|
|
||||||
|
column = DownloadColumn(decimal_suffix=False)
|
||||||
|
test_task = Task(1, "test", 1024, 512, _get_time=lambda: 1.0)
|
||||||
|
rendered_progress = str(column.render(test_task))
|
||||||
|
expected = "0.5/1.0 KiB"
|
||||||
|
assert rendered_progress == expected
|
||||||
|
|
||||||
|
|
||||||
def test_task_ids():
|
def test_task_ids():
|
||||||
progress = make_progress()
|
progress = make_progress()
|
||||||
assert progress.task_ids == [0, 1, 2, 4]
|
assert progress.task_ids == [0, 1, 2, 4]
|
||||||
|
|
Loading…
Reference in New Issue