How to get terminal size or font size in pixels?
Question:
I’ve seen some posts and answers about how to get the terminal size in numbers of columns and rows. Can I get the terminal size, or equivalently, the size of the font used in the terminal, in pixels?
(I wrote equivalently because terminal width[px] = font width[px]*number of columns. or that is what I mean by terminal width.)
I’m looking for a way that works with python 2 on linux, but I do appreciate answers that works only with python 3. Thanks!
Answers:
Maybe. If your terminal software supports XTerm Control Sequences, then the sequence e[14t
will give you the size width*height in pixels.
Related:
- xtermctl – Put standard xterm/dtterm window control codes in shell parameters for easy use. Note that some terminals do not support all combinations.
So, you already know how to get terminal size (here) in characters.
I’m afraid it is not possible.
TTY is a text terminal and doesn’t have control of where it is running.
So if your console program is executed in the terminal, you can’t know where is it displaying.
However, you can use graphical mode to take control of fonts, display, etc.
But why terminal? You can use GUI for that.
The data structure that stores terminal info in linux is terminfo. This is the structure that any general terminal query would be reading from. It does not contain pixel information, since that is not relevant for the text-only terminals it was designed to specify.
If you’re running the code in an X compatible terminal, it is probably possible with control codes, but that would very likely not be portable.
Another possible approach, with limited support, is checking the ws_xpixel and ws_ypixel values of struct terminfo.
A python snippet to query these values:
import array, fcntl, termios
buf = array.array('H', [0, 0, 0, 0])
fcntl.ioctl(1, termios.TIOCGWINSZ, buf)
print(buf[2], buf[3])
This only works in certain terminal emulators, others always report 0 0
. See e.g. the VTE feature request to set these fields for a support matrix.
tput cols
tells you the number of columns.
tput lines
tells you the number of rows.
so
from subprocess import check_output
cols = int(check_output(['tput', 'cols']))
lines = int(check_output(['tput', 'lines']))
I’ve seen some posts and answers about how to get the terminal size in numbers of columns and rows. Can I get the terminal size, or equivalently, the size of the font used in the terminal, in pixels?
(I wrote equivalently because terminal width[px] = font width[px]*number of columns. or that is what I mean by terminal width.)
I’m looking for a way that works with python 2 on linux, but I do appreciate answers that works only with python 3. Thanks!
Maybe. If your terminal software supports XTerm Control Sequences, then the sequence e[14t
will give you the size width*height in pixels.
Related:
- xtermctl – Put standard xterm/dtterm window control codes in shell parameters for easy use. Note that some terminals do not support all combinations.
So, you already know how to get terminal size (here) in characters.
I’m afraid it is not possible.
TTY is a text terminal and doesn’t have control of where it is running.
So if your console program is executed in the terminal, you can’t know where is it displaying.
However, you can use graphical mode to take control of fonts, display, etc.
But why terminal? You can use GUI for that.
The data structure that stores terminal info in linux is terminfo. This is the structure that any general terminal query would be reading from. It does not contain pixel information, since that is not relevant for the text-only terminals it was designed to specify.
If you’re running the code in an X compatible terminal, it is probably possible with control codes, but that would very likely not be portable.
Another possible approach, with limited support, is checking the ws_xpixel and ws_ypixel values of struct terminfo.
A python snippet to query these values:
import array, fcntl, termios
buf = array.array('H', [0, 0, 0, 0])
fcntl.ioctl(1, termios.TIOCGWINSZ, buf)
print(buf[2], buf[3])
This only works in certain terminal emulators, others always report 0 0
. See e.g. the VTE feature request to set these fields for a support matrix.
tput cols
tells you the number of columns.
tput lines
tells you the number of rows.
so
from subprocess import check_output
cols = int(check_output(['tput', 'cols']))
lines = int(check_output(['tput', 'lines']))