Bugfix in terminal_size()

This commit is contained in:
Todd Gamblin 2014-11-23 17:53:21 -06:00
parent d2fe038caf
commit 287b04e50a
2 changed files with 15 additions and 12 deletions

View File

@ -25,6 +25,9 @@
import sys import sys
import os import os
import textwrap import textwrap
import fcntl
import termios
import struct
from StringIO import StringIO from StringIO import StringIO
from llnl.util.tty.color import * from llnl.util.tty.color import *
@ -155,7 +158,7 @@ def hline(label=None, **kwargs):
color = kwargs.get('color', '') color = kwargs.get('color', '')
max_width = kwargs.get('max_width', 64) max_width = kwargs.get('max_width', 64)
cols, rows = terminal_size() rows, cols = terminal_size()
if not cols: if not cols:
cols = max_width cols = max_width
else: else:
@ -178,22 +181,22 @@ def hline(label=None, **kwargs):
def terminal_size(): def terminal_size():
"""Gets the dimensions of the console: cols, rows.""" """Gets the dimensions of the console: (rows, cols)."""
def ioctl_GWINSZ(fd): def ioctl_GWINSZ(fd):
try: try:
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')) rc = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except: except:
return return
return cr return rc
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) rc = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr: if not rc:
try: try:
fd = os.open(os.ctermid(), os.O_RDONLY) fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd) rc = ioctl_GWINSZ(fd)
os.close(fd) os.close(fd)
except: except:
pass pass
if not cr: if not rc:
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80)) rc = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
return int(cr[1]), int(cr[0]) return int(rc[0]), int(rc[1])

View File

@ -121,7 +121,7 @@ def colify(elts, **options):
console_cols = options.get("cols", None) console_cols = options.get("cols", None)
if not console_cols: if not console_cols:
console_cols, console_rows = terminal_size() console_rows, console_cols = terminal_size()
elif type(console_cols) != int: elif type(console_cols) != int:
raise ValueError("Number of columns must be an int") raise ValueError("Number of columns must be an int")
console_cols = max(1, console_cols - indent) console_cols = max(1, console_cols - indent)
@ -167,7 +167,7 @@ def colified(elts, **options):
if __name__ == "__main__": if __name__ == "__main__":
import optparse import optparse
cols, rows = terminal_size() rows, cols = terminal_size()
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-u", "--uniform", action="store_true", default=False, parser.add_option("-u", "--uniform", action="store_true", default=False,
help="Use uniformly sized columns instead of variable-size.") help="Use uniformly sized columns instead of variable-size.")