Remove useless parens
[pysize.git] / pysize / ui / ascii / terminal_size.py
blob16bfa5a68a448eaa5d0edfd09908aa513bcfa4b4
1 # from http://pdos.csail.mit.edu/~cblake/cls/cls.py
3 import os
5 def _ioctl_GWINSZ(fd): #### TABULATION FUNCTIONS
6 try: ### Discover terminal width
7 import fcntl
8 import termios
9 import struct
10 cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
11 except:
12 return
13 return cr
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
19 try:
20 fd = os.open(os.ctermid(), os.O_RDONLY)
21 cr = _ioctl_GWINSZ(fd)
22 os.close(fd)
23 except:
24 pass
25 if not cr: # env vars or finally defaults
26 try:
27 cr = os.environ['LINES'], os.environ['COLUMNS']
28 except:
29 cr = 25, 80
30 return int(cr[1]), int(cr[0]) # reverse rows, cols
32 if __name__ == '__main__':
33 print terminal_size()