How curses preserves screen contents?
Asked Answered
A

2

5

When you start the (n)curses program, it will remove the scroll bar from your terminal emulator, clear the screen, the scroll log (history) will also disappear. When you exit the program, the screen contents reappears, the scroll bar returns, and the scroll buffer returns.

How ncurses does it? I have studied all ANSI escape codes, I cannot see anything that can do such things. What is the technology behind this?

Abaca answered 28/8, 2016 at 6:15 Comment(0)
A
5

The technology is called The Alternate Screen Buffer. Terminal emulator provides two modes: primary and alternate. Alternate screen buffer has no scroll back, when you switch between these buffers the contents in them is preserved. To switch between them use smcup and rmcup ANSI Escape Sequences. In binary form they are:

$ tput smcup|hexdump -C 
00000000  1b 5b 3f 31 30 34 39 68                           |.[?1049h|

$ tput rmcup|hexdump -C 
00000000  1b 5b 3f 31 30 34 39 6c                           |.[?1049l|
Abaca answered 28/8, 2016 at 9:48 Comment(0)
G
7

This is discussed at length in the xterm FAQ Why doesn't the screen clear when running vi?.

ncurses as such doesn't do it. This is done by the terminal, given the escape sequences provided in the terminal description.

The behavior is from the alternate screen buffer, which was introduced with xterm in X10R4 (the end of 1986). xterm maintains two buffers:

  • the normal screen buffer (what you see in the VT100 window) including the scrollback area, and
  • the alternate screen buffer (which does not include the scrollback area)

xterm (and programs which use this feature, such as rxvt) recognizes escape sequences for switching between the two buffers. Since conventional termcap had no feature for this, the developers used the termcap initialization sequences ti and te (which correspond to terminfo smcup and rmcup). Technically the names refer to cursor-addressing mode, but no one cares (much).

The original xterm's alternate screen feature could be suppressed by removing at runtime the termcap ti and te capabilities (from the TERMCAP environment variable), but that approach could not work with terminfo, which uses files. Since 1997, xterm has implemented a newer set of escape sequences which can be ignored by xterm. A few other programs allow the feature to be suppressed, but as a rule, those do not.

Galeiform answered 28/8, 2016 at 10:54 Comment(0)
A
5

The technology is called The Alternate Screen Buffer. Terminal emulator provides two modes: primary and alternate. Alternate screen buffer has no scroll back, when you switch between these buffers the contents in them is preserved. To switch between them use smcup and rmcup ANSI Escape Sequences. In binary form they are:

$ tput smcup|hexdump -C 
00000000  1b 5b 3f 31 30 34 39 68                           |.[?1049h|

$ tput rmcup|hexdump -C 
00000000  1b 5b 3f 31 30 34 39 6c                           |.[?1049l|
Abaca answered 28/8, 2016 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.