Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 305
Mayavimmer Avatar asked Apr 09 '14 09:04

Mayavimmer


People also ask

How do you clear the screen curse in Python?

To clear characters until the end of the line, use clrtoeol(), To clear characters until the end of the window, use clrtobot().

Does Python curses work on Windows?

The Windows version of Python doesn't include the curses module. A ported version called UniCurses is available.

Are curses still used?

BSD curses is no longer maintained, having been replaced by ncurses, which is an open-source implementation of the AT&T interface. If you're using an open-source Unix such as Linux or FreeBSD, your system almost certainly uses ncurses.

Is curses standard in Python?

The curses package comes with the Python standard library. In Linux and Mac, the curses dependencies should already be installed so there is no extra steps needed. On Windows, you need to install one special Python package, windows-curses available on PyPI to add support.


1 Answers

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.

like image 153
Will Hartung Avatar answered Oct 14 '22 09:10

Will Hartung