1 # from http://pdos.csail.mit.edu/~cblake/cls/cls.py
5 def _ioctl_GWINSZ(fd
): #### TABULATION FUNCTIONS
6 try: ### Discover terminal width
10 cr
= struct
.unpack('hh', fcntl
.ioctl(fd
, termios
.TIOCGWINSZ
, '1234'))
15 def terminal_size(): ### decide on *some* terminal size
16 """Return (lines, columns)."""
17 cr
= _ioctl_GWINSZ(0) or _ioctl_GWINSZ(1) or _ioctl_GWINSZ(2) # try open fds
18 if not cr
: # ...then ctty
20 fd
= os
.open(os
.ctermid(), os
.O_RDONLY
)
21 cr
= _ioctl_GWINSZ(fd
)
25 if not cr
: # env vars or finally defaults
27 cr
= os
.environ
['LINES'], os
.environ
['COLUMNS']
30 return int(cr
[1]), int(cr
[0]) # reverse rows, cols
32 if __name__
== '__main__':