VT100 ANSI escape sequences: getting screen size, conditional ANSI

青春壹個敷衍的年華 提交于 2019-12-12 10:55:24

问题


  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


回答1:


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

  • move the cursor to the lower-right corner (actually, to row/column 999/999, which is good enough), and
  • asking the terminal where the cursor really is.

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.



来源:https://stackoverflow.com/questions/31619962/vt100-ansi-escape-sequences-getting-screen-size-conditional-ansi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!