简体   繁体   English

Python:使用Vim之类的诅咒还原终端内容

[英]Python: Restore Terminal Contents with Curses Like Vim

Right now, after my Python program that uses curses exits, I'm left with a blank screen. 现在,在退出使用curses Python程序之后,我的屏幕空白。 Anything that I'd done prior to running my program is no longer visible. 我在运行程序之前所做的任何事情都不再可见。

How can I make it so that after my curses program exits, the prior contents is visible, IE, the way that less , man , and vim do on *nix. 我如何做到这一点,以便在我的curses程序退出后,可以看到先前的内容,即IE,即lessmanvim在* nix上执行的方式。

Extra details: I'm using the Windows console, so *nix terminal commands won't work. 额外的详细信息:我正在使用Windows控制台,因此* nix终端命令将不起作用。 For anyone wondering how I managed to get curses working on Windows, see my answer over here . 大家很奇怪,我怎么设法curses在Windows上运行,请参阅我的答案在这里

My question is exactly the same as this one , except mine is specific to Python (not C) and Windows (not *nix). 我的问题与这一问题完全相同,除了我的问题特定于Python(非C)和Windows(非* nix)。

PDCurses attempts to save the screen contents when it starts, and to restore it it you have set the PDC_RESTORE_SCREEN environment variable (for Windows, of course). PDCurses尝试在屏幕启动时尝试保存屏幕内容,并将其还原,您已经设置了PDC_RESTORE_SCREEN环境变量(当然,对于Windows)。

By attempts , I mean that this may fail. 通过尝试 ,我的意思是这可能会失败。 Windows(using the [ReadConsoleOutput][1] function) has a 64KB buffer used for this purpose, and large screensizes (especially counting the scrollback) may be too large. Windows(使用[ReadConsoleOutput][1]函数)具有用于此目的的64KB缓冲区,并且较大的屏幕尺寸(尤其是计算回滚)可能太大。 If it cannot save the whole buffer, it tries to save/restore only the part that you see (the "window"). 如果无法保存整个缓冲区,它将尝试仅保存/恢复您看到的部分(“窗口”)。 Even that could be too large, of course. 当然,即使那样也可能太大。

In a quick check of ncurses on the other hand, I do not see this problem (so the problem lies in PDCurses). 另一方面,在快速检查ncurses时,我没有看到此问题(因此问题出在PDCurses中)。

From more reading (eg, this question ), I see that you are likely using UniCurses for Python , which would be more interesting if it were not for the fact that its author chose to license it in a way that precludes its inclusion in any python, ncurses (or PDCurses) release. 通过更多的阅读(例如, 这个问题 ),我发现您可能正在使用UniCurses for Python ,如果不是因为它的作者选择以不将其包含在任何python中的方式进行许可的事实,这将更加有趣。 ,ncurses(或PDCurses)版本。

curses.endwin() should do the trick. curses.endwin()应该可以解决问题。

Here's the documentation explaining it. 这是解释它文档。

Also, here's where I've personally used this function . 另外,这是我亲自使用此功能的地方

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

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