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 :
- 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
- 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
- if there is a failure here, then there will be an attempt to obtain dimensions from environment variables (see below)
- 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