0

Right now, after my Python program that uses curses exits, I'm left with a blank screen. 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.

Extra details: I'm using the Windows console, so *nix terminal commands won't work. For anyone wondering how I managed to get curses working on Windows, see my answer over here.

My question is exactly the same as this one, except mine is specific to Python (not C) and Windows (not *nix).

Community
  • 1
  • 1
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • Does `endwin` not work on windows? – mjgpy3 Feb 19 '15 at 21:02
  • @mjgpy3: I hadn't tested my code outside of Windows yet when I asked. I just did, and I found that it already functions perfectly in *nix. My problem appears to be specific to Windows. – ArtOfWarfare Feb 19 '15 at 23:29

2 Answers2

1

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).

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. 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).

From more reading (e.g., 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.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • I initially was going to say this didn't work, but it actually kind of did. After my program exists curses, I can scroll up and see all the stuff from before it. But it's as if running curses also inserted a few hundred blank lines. How can I keep it from doing that and restore the screen without all that extra blank space? – ArtOfWarfare Mar 03 '15 at 17:04
  • Checking (with lynx, in a console window), I see something like that: it is successfully restoring the visible part of the console buffer, but is blanking the bulk of it--except that a few hundred lines previously, there is some untouched text from a previous run of lynx. That is with a console buffer which is larger than 64KB, and the behavior is consistent with my caveat above. If I recall correctly, that's 2 bytes per cell, giving a maxium restorable size of 80x400 (versus the 9999 rows I used). But limiting the screen also is wrong - seems to be bug... – Thomas Dickey Mar 04 '15 at 09:52
  • I'll give you a +1 for this for now. If you figure out and share how to do it without all those blank lines being inserted I'll make it accepted. – ArtOfWarfare Mar 04 '15 at 14:26
0

curses.endwin() should do the trick.

Here's the documentation explaining it.

Also, here's where I've personally used this function.

mjgpy3
  • 8,597
  • 5
  • 30
  • 51
  • My code ends with (replaced line breaks with ; for readability in comment): `curses.nocbreak(); screen.keypad(0); curses.echo(); curses.endwin()`. After executing that, all that I have on my screen in a brand new Python prompt - nothing is visible before it if I scroll up (the buffer works though - I can hit the up arrow and see anything I had run previously.) – ArtOfWarfare Feb 19 '15 at 23:22
  • I hadn't tested my code outside of Windows yet when I asked. I just did, and I found that it already functions perfectly in *nix. My problem appears to be specific to Windows. – ArtOfWarfare Feb 19 '15 at 23:30