15

I want to get the size of the terminal. I am using this functionality:

import sys, struct, fcntl, termios

s = struct.pack('HHHH', 0, 0, 0, 0)
t = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, s)
print(struct.unpack('HHHH', t))

But what on earth is termios.TIOCGWINSZ?

William
  • 2,917
  • 5
  • 30
  • 47
  • Unfortunately the python `termios` module documentation (https://docs.python.org/3/library/termios.html#module-termios) doesn't give a list of the `ioctl` `request` constants that it brings in... but you can go look at the source code: https://github.com/python/cpython/blob/3a5b0d8988491d9408b22bceea6fd70b91345724/Modules/termios.c#L319 and for more information refer to your particular OS manual pages or, for more standard ones, the standard: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/termios.h.html – rakslice Jun 06 '18 at 01:55
  • Related: https://stackoverflow.com/questions/566746/how-to-get-linux-console-window-width-in-python . `os.get_terminal_size()` – Albert Aug 19 '22 at 19:43

1 Answers1

27

It is a magic constant determined by the system you are running on resp. by the terminal driver.

In combination with ioctl(), it serves to tell exectly what you want, in your case call IOCtl to Get the Window Size. Thus the name TIOCGWINSZ, IOCtl to Get the WINdow SiZe.

This bit of documentation might help you clear things up.

Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
glglgl
  • 89,107
  • 13
  • 149
  • 217