linuxescapingansi-escapevt100

VT100 ANSI escape sequences: getting screen size, conditional ANSI


  1. When I resizing on terminal, it keeps full screen. I guess, there's someway that can find out what screen size the terminal is. How can I do that in VT100?

  2. With , when I list folder, it shows folder in blue color. (or let's say different color) But, if you save output into a text file ( ls > out.txt ), you don't see any ANSI code but plain text. However, if you try ( vi > out.txt ), you will see ANSI code. How does know that?

Thank you


Solution

  • Programs (such as vi) which automatically adjust to screen resizing are responding to the SIGWINCH signal, and using a system call to obtain the system's information about the screen-size. See for example Get width/height of a terminal window in c++?. By the way, though widely implemented, it does not appear to be documented in POSIX signal.h.

    Without taking SIGWINCH into account, a program could ask the terminal about its screensize. The resize program does this, by sending the terminal control sequences to

    The behavior of ls and vi (and other programs) regarding ANSI control sequences which would be embedded in their output depends upon the design of the program. They likely detect the redirection of their output to a file using the isatty function, and do something different depending on whether the output is to a terminal, or to a file.