Auf der Suche nach einer Möglichkeit, die Terminalgröße ohne `curses`, `stty` oder abenteuerlichem Auslesen von `env`-Variablen zu bekommen und das vielleicht auch noch in einer Console ohne X bin ich zwar sehr bald auf `ioctl` gestoßen, konnte das aber mangels C Kenntnis nicht umsetzen.snafu hat geschrieben:... die Terminalbreite selbst erkennen ...
BlackJack hat mir dann geholfen, herausgekommen ist das:
Code: Alles auswählen
from ctypes import c_ushort, sizeof, Structure
from termios import TIOCGWINSZ
from fcntl import ioctl
import sys
class WinSize(Structure):
'''To get the terminal size information via the UNIX systemcall
'ioctl()'. This is needed cause the control sequence '\033[18t'
only work proper in xterm-like terminals but not without running
X-Server.
The 'WinSize.from_file()'-method returns the result of 'ioctl()'
and is wrapped by 'window_size()'. Use this function instead.
Thanks to 'BlackJack' from 'python-forum.de' for this
peace of code...! '''
_fields_ = [
('rows', c_ushort),
('columns', c_ushort),
('x_pixels', c_ushort), # Unused.
('y_pixels', c_ushort), # Unused.
]
@classmethod
def from_file(cls, tty_file):
result = ioctl(tty_file, TIOCGWINSZ, '\0' * sizeof(cls))
return cls.from_buffer_copy(result)
def window_size():
'''window_size() -> (rows, columns) '''
size = WinSize.from_file(sys.stdout)
return size.rows, size.columns
mutetella