VT100 ANSI escape sequences: Get screen size, conditional ANSI

  • When I resize on the terminal it keeps the entire screen. Probably, there is something that can find out what screen size the terminal has. How can I do this in VT100?

  • C, when I list a folder, it shows the folder in blue. (or let's say differently) But, if you save the output to a text file (ls> out.txt), you don't see ANSI code, but plain text. However, if you try (vi> out.txt), you will see ANSI code. How do you know?



source to share

1 answer

Programs (such as vi

) that automatically adjust the screen size, respond to a signal, SIGWINCH

and use a system call to get system information about the screen size. See For example Get the width / height of a terminal window in C ++? ... By the way, although it's widely implemented, it doesn't seem to be documented in POSIX signal.h


Without taking into account SIGWINCH

, the program can ask the terminal for its screens. The program resize

does this by sending terminal escape sequences to

  • move the cursor to the bottom right corner (actually, to row / column 999/999, which is good enough) and
  • asks the terminal where the cursor is.

The behavior of ls

both vi

(and other programs) with respect to ANSI escape sequences that will be embedded in their output depends on the design of the program. They probably detect the redirection of their output to a file using a function isatty

and do something different depending on whether the output is a terminal or a file.



All Articles