You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was hoping that with rich I could implement the increasingly prevalent "terminal waterfall" where you see live inset output from a non-trivial command scroll through a small subview in the terminal, and then replaces it with a summary once it's finished. Like showing you the last 10 lines of a compiler's output but when it's done all you see are any errors/warnings.
import subprocess
import time
from rich.console import Console
from rich.live import Live
from rich.text import Text
console = Console()
def run_step(label, cmd):
with Live(Text(f"{label}: {cmd}"), auto_refresh=True, transient=True) as live:
process = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
universal_newlines=True
)
for line in process.stdout:
live.console.print(line.rstrip())
rc = process.poll()
time.sleep(1.0)
console.print(f"{label}: {'OK' if rc == 0 else 'FAIL'}")
run_step("Ping the server", "ping 1.1.1.1")
I get nice colorized output.
I've been trying the various methods people have described for doing a terminal waterfall (a length-constrained scrolling view of the live output from a command that goes away once the command has finished).
What I actually want to achieve is a view where the user sees:
$ initiating-command.py
Ping the Server: ping 1.1.1.1 <spinner>
<output line 3>
<8 more lines>
<output line 13>
rather than having the status at the bottom of the output, and when the command is complete I want to replace the whole thing with a summary line:
$ initiating-command.py
Ping the Server: OK
I can't achieve what I'm trying to do with a table, because there's no way to remove rows or make rows roll-over when you add the Nth.
If I try to do it with a Live, I can get what I want at the cost of continually rebuilding the renderable, but I also lose the colorization/highlighting:
with Live(Panel(Text()), console=console, auto_refresh=False, transient=True) as live:
process = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
universal_newlines=True
)
# Create a deque with a max length of 10
last_lines = collections.deque(maxlen=10)
for line in process.stdout:
last_lines.append(line.rstrip())
live.update(Panel(Text('\n'.join(last_lines))))
live.refresh()
Is there a better approach to achieving this, and what I am doing that is losing the highlighting?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I was hoping that with rich I could implement the increasingly prevalent "terminal waterfall" where you see live inset output from a non-trivial command scroll through a small subview in the terminal, and then replaces it with a summary once it's finished. Like showing you the last 10 lines of a compiler's output but when it's done all you see are any errors/warnings.
I get nice colorized output.
I've been trying the various methods people have described for doing a terminal waterfall (a length-constrained scrolling view of the live output from a command that goes away once the command has finished).
What I actually want to achieve is a view where the user sees:
rather than having the status at the bottom of the output, and when the command is complete I want to replace the whole thing with a summary line:
I can't achieve what I'm trying to do with a table, because there's no way to remove rows or make rows roll-over when you add the Nth.
If I try to do it with a Live, I can get what I want at the cost of continually rebuilding the renderable, but I also lose the colorization/highlighting:
Is there a better approach to achieving this, and what I am doing that is losing the highlighting?
Beta Was this translation helpful? Give feedback.
All reactions