/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

Add tests, introduce explicit default values, always respect COLUMNS.

* bzrlib/tests/test_osutils.py:
(TestTerminalWidth): Test the known cases.

* bzrlib/osutils.py:
(terminal_width): Add default values and always respect the
COLUMNS envrionment variable. Catch TIOCGWINSZ related errors more
broadly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1294
1294
    normalized_filename = _inaccessible_normalized_filename
1295
1295
 
1296
1296
 
 
1297
default_tty_width = 80
 
1298
"""The default terminal width for ttys."""
 
1299
default_non_tty_width = 256
 
1300
"""The default terminal width for non-ttys."""
1297
1301
def terminal_width():
1298
1302
    """Return estimated terminal width."""
 
1303
 
 
1304
    # If the env var is set, take it, user is always right
 
1305
    try:
 
1306
        return int(os.environ['COLUMNS'])
 
1307
    except (KeyError, ValueError):
 
1308
        pass
 
1309
 
1299
1310
    isatty = getattr(sys.stdout, 'isatty', None)
1300
1311
    if  isatty is None or not isatty():
1301
 
        # If it's not a tty, the width makes no sense. We just use a value bug
1302
 
        # enough to avoid truncations. When the output is redirected, the
1303
 
        # pagers can then handle that themselves. A cleaner implementation
1304
 
        # would be to fix the callers to not try to format at all in these
1305
 
        # circumstances.
1306
 
        return 65536
 
1312
        # If it's not a tty, there is no way to acquire a value
 
1313
        # automatically. Yet, bzrlib expect a reasonable value here since it's
 
1314
        # used for both truncating lines or filling them. As such we need to
 
1315
        # use a reasonable default.  When the output is redirected, the pagers
 
1316
        # can then handle that themselves (or set COLUMNS if needed). A cleaner
 
1317
        # implementation would be to fix the callers to not try to format at
 
1318
        # all in these circumstances, but not formmatting when filling lines
 
1319
        # does not always make sense.
 
1320
        return default_non_tty_width
1307
1321
 
1308
1322
    if sys.platform == 'win32':
1309
 
        return win32utils.get_console_size()[0]
1310
 
    width = 0
 
1323
        return win32utils.get_console_size(default_tty_width)[0]
 
1324
 
1311
1325
    try:
1312
1326
        import struct, fcntl, termios
1313
1327
        s = struct.pack('HHHH', 0, 0, 0, 0)
1314
1328
        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1315
1329
        width = struct.unpack('HHHH', x)[1]
1316
 
    except IOError:
1317
 
        pass
1318
 
    if width <= 0:
1319
 
        try:
1320
 
            width = int(os.environ['COLUMNS'])
1321
 
        except:
1322
 
            pass
1323
 
    if width <= 0:
1324
 
        width = 80
 
1330
    except (IOError, AttributeError):
 
1331
        width = -1
 
1332
 
 
1333
    if width <= 0:
 
1334
        width = default_tty_width
1325
1335
 
1326
1336
    return width
1327
1337