theonlygusti theonlygusti - 2 months ago 9
C# Question

Terminal app that uses the entire terminal window

I've seen terminal apps, such as vim, that when used fill the whole screen and then disappear entirely when they are quit, i.e. they do not simply print into the current terminal "space":

Vim, when opened, takes up the whole terminal window, and when closed prints nothing else in the terminal.

I was wondering how in C# I can accomplish this.

Answer

The program uses the alternate screen feature of the terminal (originally xterm, but others).

vim is using the terminal description, which includes escape sequences for starting and ending the use of cursor-addressing. Mostly (for xterm and imitators) that switches to/from the alternate screen, saving/restoring the cursor position in the normal screen. Not all terminal descriptions use the feature.

A history of the feature is given in the xterm FAQ Why doesn't the screen clear when running vi?. Originally this was (in terminfo format)

smcup=\E7\E[?47h, rmcup=\E[2J\E[?47l\E8,

(where \E is the ASCII escape character \033), and the modern version is

smcup=\E7\E[?1049h, rmcup=\E[?1049l

Referring to MSDN, this probably will work for \E7\E[?47h:

System.Console.Write("\u001b7\u001b[?47h")

The \E becomes \u001b. Similarly, for \E[2J\E[?47l\E8, you could write

System.Console.Write("\u001b[2J\u001b[?47l\u001b8")

Further reading: