Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

terminal - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...