def test_live_screen() -> None: console = create_capture_console(width=20, height=5) console.begin_capture() with Live(Text("foo"), screen=True, console=console, auto_refresh=False) as live: live.refresh() result = console.end_capture() print(repr(result)) expected = "\x1b[?1049h\x1b[H\x1b[?25l\x1b[Hfoo \n \n \n \n \x1b[Hfoo \n \n \n \n \x1b[?25h\x1b[?1049l" assert result == expected
def test_growing_display_transient() -> None: console = create_capture_console() console.begin_capture() with Live(console=console, auto_refresh=False, transient=True) as live: display = "" for step in range(10): display += f"Step {step}\n" live.update(display, refresh=True) output = console.end_capture() assert ( output == "\x1b[?25lStep 0\n\r\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\nStep 7\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\nStep 7\nStep 8\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\nStep 7\nStep 8\nStep 9\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\nStep 7\nStep 8\nStep 9\n\n\x1b[?25h\r\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K" )
def test_live_state() -> None: with Live("") as live: assert live._started live.start() assert live.renderable == "" assert live._started live.stop() assert not live._started assert not live._started
def test_growing_display_file_console() -> None: console = create_capture_console(force_terminal=False) console.begin_capture() with Live(console=console, auto_refresh=False) as live: display = "" for step in range(10): display += f"Step {step}\n" live.update(display, refresh=True) output = console.end_capture() assert ( output == "Step 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\nStep 7\nStep 8\nStep 9\n" )
def test_growing_display_autorefresh() -> None: """Test generating a table but using auto-refresh from threading""" console = create_capture_console() console = create_capture_console(height=5) console.begin_capture() with Live(console=console, auto_refresh=True, vertical_overflow="visible") as live: display = "" for step in range(10): display += f"Step {step}\n" live.update(display) time.sleep(0.2)
def test_growing_display_overflow_crop() -> None: console = create_capture_console(height=5) console.begin_capture() with Live(console=console, auto_refresh=False, vertical_overflow="crop") as live: display = "" for step in range(10): display += f"Step {step}\n" live.update(display, refresh=True) output = console.end_capture() assert ( output == "\x1b[?25lStep 0\n\r\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\n\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\r\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2K\x1b[1A\x1b[2KStep 0\nStep 1\nStep 2\nStep 3\nStep 4\nStep 5\nStep 6\nStep 7\nStep 8\nStep 9\n\n\x1b[?25h" )
progress_table = Table.grid(expand=True) progress_table.add_row( Panel( overall_progress, title="Overall Progress", border_style="green", padding=(2, 2), ), Panel(job_progress, title="[b]Jobs", border_style="red", padding=(1, 2)), ) layout = make_layout() layout["header"].update(Header()) layout["body"].update(make_sponsor_message()) layout["box2"].update(Panel(make_syntax(), border_style="green")) layout["box1"].update(Panel(layout.tree, border_style="red")) layout["footer"].update(progress_table) from mudrich.live import Live from time import sleep with Live(layout, refresh_per_second=10, screen=True): while not overall_progress.finished: sleep(0.1) for job in job_progress.tasks: if not job.finished: job_progress.advance(job.id) completed = sum(task.completed for task in job_progress.tasks) overall_progress.update(overall_task, completed=completed)
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)
from time import sleep from mudrich.columns import Columns from mudrich.panel import Panel from mudrich.live import Live from mudrich.text import Text from mudrich.spinner import Spinner, SPINNERS all_spinners = Columns( [ Spinner(spinner_name, text=Text(repr(spinner_name), style="green")) for spinner_name in sorted(SPINNERS) ], column_first=True, expand=True, ) with Live( Panel(all_spinners, title="Spinners", border_style="blue"), refresh_per_second=20, ) as live: while True: sleep(0.1)
) job1 = job_progress.add_task("[green]Cooking") job2 = job_progress.add_task("[magenta]Baking", total=200) job3 = job_progress.add_task("[cyan]Mixing", total=400) total = sum(task.total for task in job_progress.tasks) overall_progress = Progress() overall_task = overall_progress.add_task("All Jobs", total=int(total)) progress_table = Table.grid() progress_table.add_row( Panel.fit(overall_progress, title="Overall Progress", border_style="green", padding=(2, 2)), Panel.fit(job_progress, title="[b]Jobs", border_style="red", padding=(1, 2)), ) with Live(progress_table, refresh_per_second=10): while not overall_progress.finished: sleep(0.1) for job in job_progress.tasks: if not job.finished: job_progress.advance(job.id) completed = sum(task.completed for task in job_progress.tasks) overall_progress.update(overall_task, completed=completed)
layout["main"].split_row(Layout(name="side"), Layout(name="body", ratio=2)) layout["side"].split(Layout(), Layout()) layout["body"].update( Align.center( Text( """This is a demonstration of rich.Layout\n\nHit Ctrl+C to exit""", justify="center", ), vertical="middle", ) ) class Clock: """Renders the time in the center of the screen.""" def __rich__(self) -> Text: return Text(datetime.now().ctime(), style="bold magenta", justify="center") layout["header"].update(Clock()) with Live(layout, screen=True, redirect_stderr=False) as live: try: while True: sleep(1) except KeyboardInterrupt: pass
BEAT_TIME = 0.04 @contextmanager def beat(length: int = 1) -> None: yield time.sleep(length * BEAT_TIME) table = Table(show_footer=False) table_centered = Align.center(table) console.clear() with Live(table_centered, console=console, screen=False, refresh_per_second=20): with beat(10): table.add_column("Release Date", no_wrap=True) with beat(10): table.add_column("Title", Text.from_markup("[b]Total", justify="right")) with beat(10): table.add_column("Budget", "[u]$412,000,000", no_wrap=True) with beat(10): table.add_column("Opening Weekend", "[u]$577,703,455", no_wrap=True) with beat(10): table.add_column("Box Office", "[u]$4,331,212,357", no_wrap=True)