简体   繁体   English

Python Curses没有清除屏幕

[英]Python Curses without clearing screen

I would like to use Curses under Python without clearing the screen. 我想在Python下使用Curses而不清除屏幕。 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. 似乎Python init函数总是清除屏幕。 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. 我还记得几年前看到一个非Python应用程序做了我喜欢的事情,所以我知道这是可能的,至少在C Curses程序中是这样。

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. 我不会说“它不可能完成”,但是我会说库存,开箱即用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. 根本问题是curses库在初始化时无法访问终端的当前状态,特别是当前正在显示的字符和字形。

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. 在PC上的旧时代,屏幕是内存映射的,因此当程序运行时,它可以访问现有的屏幕状态,以便捕获并稍后恢复它。

For a generic smart terminal, that's not necessarily the case. 对于通用智能终端,情况不一定如此。 On Linux, or the Mac, the terminal type is some kind of "xterm". 在Linux或Mac上,终端类型是某种“xterm”。 On a windows console terminal, it's a ANSI style terminal (mind xterm is also a kind of ANSI terminal). 在Windows控制台终端上,它是ANSI样式终端(mind xterm也是一种ANSI终端)。 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. 终端类型是termcap / terminfo库使用的代码,curses依赖于该代码来了解如何移动光标,删除字符和行,设置颜色或反向视频等。

All of curses interaction with the screen is through the printing of ESCape sequences, rather than manipulating memory. 所有与屏幕交互的curses都是通过打印ESCape序列,而不是操纵内存。 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. 如果查看XTerm转义序列列表,您将看到没有任何内容可以将屏幕内容报告回主机程序。 However, there is an alternate frame buffer. 但是,有一个备用帧缓冲区。 An example of this is, perhaps, vim . 这方面的一个例子可能是vim When you edit a file with vim , vim takes over the entire screen. 使用vim编辑文件时, vim将占据整个屏幕。 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. vim正在切换到备用屏幕缓冲区,并在那里执行所有操作,然后在退出时恢复主屏幕缓冲区。 But this is a simple switching exercise, vim does not "know", nor has access to, the contents of the original screen buffer. 但这是一个简单的切换练习, vim不会“知道”,也无法访问原始屏幕缓冲区的内容。

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. 如果您使用Linux控制台(可以使用FKeys切换屏幕)或GNU Screen之类的实用程序,则会有所不同。 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. 这些依赖于不同的概念(Linux控制台的设备驱动程序和GNU Screen的伪终端),整个程序自己维护每个屏幕的状态。 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. 如果是,那是通过一些专有方法而不是Curses。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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