Feature request: Screen re-/storing
See original GitHub issueI’ve been playing around with rich
as of late and I am really enjoying it. 👍
But there is one thing I wish was also in this wonderful chuck of code and that would be the ability to programmatically store and restore the terminal view (Like how less
and other common commandline utilities do). I’ve got a somewhat working version of a decorator that I am using in my current project which I think some other people might find fairly useful. 😉
from contextlib import contextmanager
import subprocess
@contextmanager
def second_screen():
try:
r_code, s_code = (
subprocess.getoutput(
r"infocmp -1 | grep 'rmcup\|smcup' | sed 's/.*=\(.*\),/\1/'"
)
.replace("\\E", "\033")
.split("\n")
)
try:
print(s_code + "\033[0;0H", end="")
yield
finally:
print(r_code, end="")
return
except:
yield
I tried my hand at making it work with multiple different terminals by using infocmp
to findout the proper escape sequence.
I am unsure if this is something that is considered inside the scope of this library, but imho I think it would by a great addition. I think the best way to integrate this into the API would be to add it as a member of the Console
-class to enable the following usage:
from rich.console import Console
console = Console()
with console.second_screen():
console.print("It works!", style="bold green")
EDIT: For me the escape sequences turn out to be:
s_code = '\033[?1049h\033[22;0;0t'
r_code = '\033[?1049l\033[23;0;0t'
So switch to secondary/primary buffer and re-/store icon and window title from/to stack respectively.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top GitHub Comments
Closing this to tidy issues, but keeping a note of it.
I’d probably not bother with legacy windows now that there is the option of the new Windows Terminal. Unless there is a low cost compatibility layer, like colorama.
Let me know what your research turns up!