diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-14 20:18:28 +0000 |
commit | f8363b456f1ab31ee56abad579b215af195093d5 (patch) | |
tree | b1500c675c2e0a55fb75721a854e1510acf7c862 /examples/top_lite_simulator.py | |
parent | Initial commit. (diff) | |
download | rich-f8363b456f1ab31ee56abad579b215af195093d5.tar.xz rich-f8363b456f1ab31ee56abad579b215af195093d5.zip |
Adding upstream version 9.11.0.upstream/9.11.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/top_lite_simulator.py')
-rw-r--r-- | examples/top_lite_simulator.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/examples/top_lite_simulator.py b/examples/top_lite_simulator.py new file mode 100644 index 0000000..e4a10f5 --- /dev/null +++ b/examples/top_lite_simulator.py @@ -0,0 +1,81 @@ +"""Lite simulation of the top linux command.""" + +import datetime +import random +import time +from dataclasses import dataclass + +from rich import box +from rich.console import Console +from rich.live import Live +from rich.table import Table +from typing_extensions import Literal + + +@dataclass +class Process: + pid: int + command: str + cpu_percent: float + memory: int + start_time: datetime.datetime + thread_count: int + state: Literal["running", "sleeping"] + + @property + def memory_str(self) -> str: + if self.memory > 1e6: + return f"{int(self.memory/1e6)}M" + if self.memory > 1e3: + return f"{int(self.memory/1e3)}K" + return str(self.memory) + + @property + def time_str(self) -> str: + return str(datetime.datetime.now() - self.start_time) + + +def generate_process(pid: int) -> Process: + return Process( + pid=pid, + command=f"Process {pid}", + cpu_percent=random.random() * 20, + memory=random.randint(10, 200) ** 3, + start_time=datetime.datetime.now() + - datetime.timedelta(seconds=random.randint(0, 500) ** 2), + thread_count=random.randint(1, 32), + state="running" if random.randint(0, 10) < 8 else "sleeping", + ) + + +def create_process_table(height: int) -> Table: + + processes = sorted( + [generate_process(pid) for pid in range(height)], + key=lambda p: p.cpu_percent, + reverse=True, + ) + table = Table( + "PID", "Command", "CPU %", "Memory", "Time", "Thread #", "State", box=box.SIMPLE + ) + + for process in processes: + table.add_row( + str(process.pid), + process.command, + f"{process.cpu_percent:.1f}", + process.memory_str, + process.time_str, + str(process.thread_count), + process.state, + ) + + return table + + +console = Console() + +with Live(console=console, screen=True, auto_refresh=False) as live: + while True: + live.update(create_process_table(console.size.height - 4), refresh=True) + time.sleep(1) |