简体   繁体   中英

Python Curses without clearing screen

I would like to use Curses under Python without clearing the screen. The reason is that I would like my app to pop up a simple small menu over the existing screen and soon exit. It is acceptable, though not preferred, to leave the ugly pieces of the pop up menu on the screen upon exit. The idea is to use it for quick practical sysadmin apps and scripts where aesthetics is not important.

It seems that the Python init functions always clear the screen. I also remember seeing one non Python app do what I like a couple years ago, so I know it is possible, at least in a C Curses program.

I'm not going to say "It can't be done", but I will say "It can't be done" with stock, out of the box Curses/NCurses.

The fundamental problem is that the curses library, when it is initialized, has no access to the current state of the terminal, notably what characters and glyphs are currently being displayed.

In the old days on a PC, the screen was memory mapped, so when a program ran it had access to the existing screen state in order to capture and perhaps restore it later.

For a generic smart terminal, that's not necessarily the case. On Linux, or the Mac, the terminal type is some kind of "xterm". On a windows console terminal, it's a ANSI style terminal (mind xterm is also a kind of ANSI terminal). Terminal type is the code used by the termcap/terminfo library that curses relies upon to know how to move the cursor, delete characters and lines, set color or reverse video, etc.

All of curses interaction with the screen is through the printing of ESCape sequences, rather than manipulating memory. It doesn't work with a framebuffer.

If you look at a list of XTerm escape sequences , you'll see there's nothing to report the contents of the screen back to the host program. However, there is an alternate frame buffer. An example of this is, perhaps, vim . When you edit a file with vim , vim takes over the entire screen. But when you exit, your original screen is restored. vim is switching to the alternate screen buffer, and does all of its operations there, and then restores the primary screen buffer on exit. But this is a simple switching exercise, vim does not "know", nor has access to, the contents of the original screen buffer.

If you use things like the Linux console (where you can switch screens using the FKeys), or a utility like GNU Screen, these are different. These rely on different concepts (device driver for the Linux console, and pseudo-terminals for GNU Screen), and the overall program maintains the state of each screen themselves. But this information isn't available to a generic program, that I know of. If it is, it's through some proprietary method and not Curses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM