diff --git a/rich/progress.py b/rich/progress.py index 1e086ef1..6e9e9fbd 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -716,9 +716,16 @@ class TaskProgressColumn(TextColumn): table_column=table_column, ) - def render_speed(self, task: "Task") -> Text: - """Show data transfer speed.""" - speed = task.finished_speed or task.speed + @classmethod + def render_speed(cls, speed: float | None) -> Text: + """Render the speed in iterations per second. + + Args: + task (Task): A Task object. + + Returns: + Text: Text object containing the task speed. + """ if speed is None: return Text("", style="progress.percentage") unit, suffix = filesize.pick_unit_and_suffix( @@ -731,7 +738,7 @@ class TaskProgressColumn(TextColumn): def render(self, task: "Task") -> Text: if task.total is None and self.show_speed: - return self.render_speed(task) + return self.render_speed(task.finished_speed or task.speed) text_format = ( self.text_format_no_percentage if task.total is None else self.text_format ) diff --git a/tests/test_progress.py b/tests/test_progress.py index 995467db..8a90ad80 100644 --- a/tests/test_progress.py +++ b/tests/test_progress.py @@ -647,6 +647,20 @@ def test_wrap_file_task_total() -> None: os.remove(filename) +def test_task_progress_column_speed(): + speed_text = TaskProgressColumn.render_speed(None) + assert speed_text.plain == "" + + speed_text = TaskProgressColumn.render_speed(5) + assert speed_text.plain == "5.0 it/s" + + speed_text = TaskProgressColumn.render_speed(5000) + assert speed_text.plain == "5.0×10³ it/s" + + speed_text = TaskProgressColumn.render_speed(8888888) + assert speed_text.plain == "8.9×10⁶ it/s" + + if __name__ == "__main__": _render = render_progress() print(_render)