The dstat program displays the header of the table, and then line by line the actual values ​​once a certain time interval:

----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system-- usr sys idl wai hiq siq| read writ| recv send| in out | int csw 0 1 98 0 0 1| 0 0 |7884B 1058B| 0 0 | 334 266 0 1 99 0 0 0| 0 0 |8565B 1660B| 0 0 | 324 276 0 1 99 0 0 0| 0 0 |6996B 590B| 0 0 | 325 240 0 0 99 0 0 1| 0 0 |6718B 992B| 0 0 | 348 236 1 1 98 0 0 0| 0 0 |7116B 964B| 0 0 | 408 276 0 1 99 0 0 0| 0 0 |6500B 468B| 0 0 | 343 221 0 0 99 0 0 1| 0 0 |6136B 830B| 0 0 | 318 217 

Interestingly, when the table head goes up, beyond the console window, a new one immediately appears. It looks as if dstat knows the height of the window and at the right moment sends two lines with a header again.

I tried to change the height after running dstat. The program adjusts and sends the header again at the right time.

How does this happen? Is some code executed on the client side? Does the console provide window sizes using any protocol?

    1 answer 1

    if we talk about this particular program written in the python language, then at the beginning of its execution it “probes the ground” in the initterm () function :

    1. first, an attempt is made to perform an ioctl- TIOCGWINSZ request . if it passes successfully, then on subsequent calls to the gettermsize () function, it will receive the terminal dimensions by ioctl -query
    2. on failure, an attempt is made to access the curses library . if successful, it will subsequently be asked for information about the size of the terminal
    3. if there is a failure here, then there will be an attempt to obtain dimensions from environment variables (see below)
    4. in the worst case, constants 25 and 80

    now about "in general".

    for example, right in an interactive shell session (checked in bash and zsh ), you can access the COLUMNS and LINES environment COLUMNS (they are set by the shell and dynamically change when the terminal is resized):

     $ echo $LINES $COLUMNS 

    inside the program / script, in addition to the low-level tools mentioned above, you can use the programs stty or tput . By the way, when the terminal is resized, the terminal emulator usually sends a SIGWINCH signal to the process started in it. This is usually a shell process that, in turn, sends the signal to the running child processes.

    illustrating an example script (should work, in theory, in any posix-compatible shell):

     /bin/echo -e "начальные значения:\nstty size: $(stty size); \ tput lines: $(tput lines); tput cols: $(tput cols)" trap '/bin/echo -e "получен сигнал WINCH!\nstty size: $(stty size); \ tput lines: $(tput lines); tput cols: $(tput cols)"' WINCH while :; do sleep 3 /bin/echo -e "текущие значения:\nstty size: $(stty size); \ tput lines: $(tput lines); tput cols: $(tput cols)" done 

    after launch, you can see how the window's signal was processed when the terminal emulator resized (after the completion of the next external program call - sleep ), and then the current values ​​were output:

     начальные значения: stty size: 28 81; tput lines: 28; tput cols: 81 текущие значения: stty size: 28 81; tput lines: 28; tput cols: 81 получен сигнал WINCH! stty size: 30 81; tput lines: 30; tput cols: 81 текущие значения: stty size: 30 81; tput lines: 30; tput cols: 81 ^C