Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How curses preserves screen contents?

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?

like image 210
exebook Avatar asked Feb 06 '23 06:02

exebook


1 Answers

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.

like image 115
Thomas Dickey Avatar answered Feb 28 '23 03:02

Thomas Dickey