1

Several *NIX commands, such as screen, man, vim and others, create a temporary canvas/screen/overlay in a shell environment. When such programs execute, they cover or hide whatever content was displayed in the terminal before — almost like a "full screen" mode, within the terminal window. When they terminate, however, they reveal or restore whatever had been on the terminal before.

In the example below, I create some filler text on the screen, then invoke man bash. The man page opens up and covers all other characters on the terminal display. When I close the man page, the characters that had been covered up are again shown.

Before

terminal screen capture before running <code>man bash</code>

While an example full-screen program is running

Screencap of the man page for Bash

After

Screencap of the terminal after running <code>man bash</code>

I would expect that programs writing to stdout/stderr could accomplish the first step (replacing the content of the terminal with program-specific content), but then it would produce a ton of text that I could scroll through, and therefore couldn't do the second step: restoring the contents of the terminal. That means that somehow either the program memorizes the previous contents of the screen and re-outputs them (I doubt it?), or it creates some sort of sub-window within a terminal and something else keeps track of the previous contents of the terminal.

My Question

How can I accomplish that behavior in my own program and/or script?

Perhaps I should use curses/ncurses, tput, termcap/terminfo, or ANSI escape sequences?

Update:

This revised question is essentially the same as https://unix.stackexchange.com/questions/27941/show-output-on-another-screen-and-return-to-normal-when-done. (I hadn't found it when I had written this question despite lots of searching.) The difference is that my question is more general (any language) whereas that question is specific to Bash. The answers to both questions are essentially the same. If it's too similar to a question on another site, feel free to close it here for that reason.

oguz ismail
  • 1
  • 16
  • 47
  • 69
jvriesem
  • 1,859
  • 3
  • 18
  • 40
  • 1
    @Rob — They are all terminal applications. There's no window manager involved. – Quentin Jun 03 '20 at 18:45
  • @Quentin it looks like a Mac. I'm betting his terminal is running on that desktop – Rob Jun 03 '20 at 18:47
  • 1
    @Quentin is correct. This behavior is the same regardless of OS (macOS, Linux), terminal (Terminal.app, iTerm2.app, Putty, Konsole, xTerm, others), or even shell (bash, zsh, csh, tcsh, ksh, and even a toy shell I wrote). – jvriesem Jun 03 '20 at 18:52
  • 2
    The source for Man is here. Feel free to just go see what it does: https://git.savannah.gnu.org/cgit/man-db.git – user2199860 Jun 03 '20 at 18:52
  • @Rob — Probably has something to do with your terminal or its configuration; not the windowing environment or lack thereof. – Quentin Jun 03 '20 at 18:59
  • 2
    Go into your favorite terminal. Use TERM=xterm-256color see overlay. Change to vt100, no overlay.... – user2199860 Jun 03 '20 at 19:05
  • @Rob — https://imgur.com/TzyO5bA — There. That's a terminal on a linux VM with no X environment running. – Quentin Jun 03 '20 at 19:07
  • Perhaps this discussion should be moved into a chat (I lack privileges to do so). – jvriesem Jun 03 '20 at 19:10
  • @user2199860: I don't know the details of that (though I could look it up soon), but setting the `TERM` environment variable definitely enables/disables the behavior I'm describing. That supports the conclusion @Quentin and @Rob are arriving at: that it's dependent on settings. – jvriesem Jun 03 '20 at 19:13
  • @Quentin That is not what I took as an overlay. I thought he was meaning another window on top. – Rob Jun 03 '20 at 19:23
  • @Rob Sorry for the confusion! Is it more clear now? (Feel free to edit my question text if you'd like to clarify it.) – jvriesem Jun 03 '20 at 19:24
  • @jvriesem It didn't help that I was reading and doing all this on a phone. – Rob Jun 03 '20 at 19:24
  • I'm curious why there were a bunch of downvotes and votes to close. It says it needs to be more focused, but it asks a limited, well-defined question: "how do programs do X?" – jvriesem Jun 04 '20 at 00:40

1 Answers1

6

How do these programs accomplish that behavior?

ANSI escape sequences. Try running this script:

#/bin/bash -
tput smcup
echo 'Hello world!'
sleep 3
tput rmcup

Using infocmp, you can see underlying sequences that create this overlaying effect, e.g:

$ infocmp -1 | grep 'rmcup\|smcup'
        rmcup=\E[?1049l\E[23;0;0t,
        smcup=\E[?1049h\E[22;0;0t,

is this behavior shell-dependent or system-dependent?

None, it depends on whether the terminal emulator supports save/restore operations.

oguz ismail
  • 1
  • 16
  • 47
  • 69