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 os
import textwrap
import fcntl
import termios
import struct
from StringIO import StringIO
from llnl.util.tty.color import *
@ -155,7 +158,7 @@ def hline(label=None, **kwargs):
color = kwargs.get('color', '')
max_width = kwargs.get('max_width', 64)
cols, rows = terminal_size()
rows, cols = terminal_size()
if not cols:
cols = max_width
else:
@ -178,22 +181,22 @@ def hline(label=None, **kwargs):
def terminal_size():
"""Gets the dimensions of the console: cols, rows."""
"""Gets the dimensions of the console: (rows, cols)."""
def ioctl_GWINSZ(fd):
try:
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
rc = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
return rc
rc = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not rc:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
rc = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
if not rc:
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)
if not console_cols:
console_cols, console_rows = terminal_size()
console_rows, console_cols = terminal_size()
elif type(console_cols) != int:
raise ValueError("Number of columns must be an int")
console_cols = max(1, console_cols - indent)
@ -167,7 +167,7 @@ def colified(elts, **options):
if __name__ == "__main__":
import optparse
cols, rows = terminal_size()
rows, cols = terminal_size()
parser = optparse.OptionParser()
parser.add_option("-u", "--uniform", action="store_true", default=False,
help="Use uniformly sized columns instead of variable-size.")