xterm/vt102 sequences to fill the terminal window, then exit on keypress, clearing whatever was on screen?

StackOverflow https://stackoverflow.com/questions/10499536

  •  06-06-2021
  •  | 
  •  

Question

I'm trying to wrap my head around VT102 sequences and how such libraries as ncurses work.

To give an example, when I open Emacs, it fills the terminal window. When I hit C-x C-c, the program exits and my terminal window looks exactly as it looked before I launched emacs (i.e. the line above my cursor shows the emacs command in my zsh history, not whatever emacs had rendered).

As a sort of "hello world", I've written a little ruby script that opens /dev/tty, then writes \x1B[48;597m\n (blank lines, with a background color) for as many rows as there are in my terminal. The program then sleeps for 5 seconds, leaving me happily staring at a bright orange screen momentarily. Then it exits, leaving me at my zsh prompt, with 53 lines of bright orange up above it.

Two questions:

  1. Is writing blank lines with a background color the correct way to fill the terminal window with color?
  2. How do I completely clear the screen by sending some VT102 sequence to /dev/tty?

I am able to send \x1B[2J, but this does the same thing as when I execute the clear command, and actual just shifts all the content up the terminal window, until my cursor is at the very top of a blank window, rather than leaving my cursor wherever it would naturally be if my program had not written a load of blank lines to the screen.

PS: Not sure what tags to use here that anybody is actually likely to be subscribed to.

Was it helpful?

Solution

Is writing blank lines with a background color the correct way to fill the terminal window with color?

No. The correct way to do this (on a terminal that supports BCE*) is to set a background color, then erase the screen using an ED sequence (e.g, ^[ [ 2 J).

*: most modern terminals do

How do I completely clear the screen by sending some VT102 sequence to /dev/tty?

Using ED. See above.

If what you actually want to do is switch to the alternate screen (like emacs is doing in your example), the alternate screen is controlled by private mode 1049. You can switch to it using ^[ [ ? 1 0 4 9 h, and switch back using ^[ [ ? 1 0 4 9 l. Note, however, that this is an xterm extension; it's not entirely standard, and wasn't supported by any DEC hardware terminals (e.g, the VT102).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top