Permalink
Browse files

more robust version of terminal size detection

  • Loading branch information...
1 parent d802924 commit dbdcb918d051d71c913e63ed6bea605088b457ed @poelzi poelzi committed Mar 23, 2011
Showing with 24 additions and 9 deletions.
  1. +24 −9 ulatency
View
33 ulatency
@@ -93,16 +93,31 @@ style = drug(**dict(
on_cyan = "\033[46m",
on_white = "\033[47m"))
+
def terminal_size():
- import fcntl, termios, struct
- try:
- h, w, hp, wp = struct.unpack('HHHH',
- fcntl.ioctl(0, termios.TIOCGWINSZ,
- struct.pack('HHHH', 0, 0, 0, 0)))
- except Exception, e:
- w = 80
- h = 40
- return w or 80, h
+ def ioctl_GWINSZ(fd):
+ try:
+ import fcntl, termios, struct, os
+ cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
+ '1234'))
+ except:
+ return None
+ return cr
+ cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
+ if not cr:
+ try:
+ fd = os.open(os.ctermid(), os.O_RDONLY)
+ cr = ioctl_GWINSZ(fd)
+ os.close(fd)
+ except:
+ pass
+ if not cr:
+ try:
+ cr = (os.environment['LINES'], os.environment['COLUMNS'])
+ except:
+ cr = (25, 80)
+ return int(cr[1]), int(cr[0])
+
TERM_WIDTH = terminal_size()[0]

0 comments on commit dbdcb91

Please sign in to comment.