~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-12-09 18:07:09 UTC
  • mfrom: (4881.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20091209180709-vahds276v0vklwad
Fix minor issues and make test passing on windows for terminal_width()

Show diffs side-by-side

added added

removed removed

Lines of Context:
1342
1342
    """Return terminal width.
1343
1343
 
1344
1344
    None is returned if the width can't established precisely.
 
1345
 
 
1346
    The rules are:
 
1347
    - if BZR_COLUMNS is set, returns its value
 
1348
    - if there is no controlling terminal, returns None
 
1349
    - if COLUMNS is set, returns its value,
 
1350
 
 
1351
    From there, we need to query the OS to get the size of the controlling
 
1352
    terminal.
 
1353
 
 
1354
    Unices:
 
1355
    - get termios.TIOCGWINSZ
 
1356
    - if an error occurs or a negative value is obtained, returns None
 
1357
 
 
1358
    Windows:
 
1359
    
 
1360
    - win32utils.get_console_size() decides,
 
1361
    - returns None on error (provided default value)
1345
1362
    """
1346
1363
 
1347
1364
    # If BZR_COLUMNS is set, take it, user is always right
1355
1372
        # Don't guess, setting BZR_COLUMNS is the recommended way to override.
1356
1373
        return None
1357
1374
 
1358
 
    if sys.platform == 'win32':
1359
 
        return win32utils.get_console_size(defaultx=None)[0]
1360
 
 
 
1375
    # If COLUMNS is set, take it, the terminal knows better (even inside a
 
1376
    # given terminal, the application can decide to set COLUMNS to a lower
 
1377
    # value (splitted screen) or a bigger value (scroll bars))
 
1378
    try:
 
1379
        return int(os.environ['COLUMNS'])
 
1380
    except (KeyError, ValueError):
 
1381
        pass
 
1382
 
 
1383
    width, height = _terminal_size(None, None)
 
1384
    if width <= 0:
 
1385
        # Consider invalid values as meaning no width
 
1386
        return None
 
1387
 
 
1388
    return width
 
1389
 
 
1390
 
 
1391
def _win32_terminal_size(width, height):
 
1392
    width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
 
1393
    return width, height
 
1394
 
 
1395
 
 
1396
def _ioctl_terminal_size(width, height):
1361
1397
    try:
1362
1398
        import struct, fcntl, termios
1363
1399
        s = struct.pack('HHHH', 0, 0, 0, 0)
1364
1400
        x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1365
 
        width = struct.unpack('HHHH', x)[1]
 
1401
        height, width = struct.unpack('HHHH', x)[0:2]
1366
1402
    except (IOError, AttributeError):
1367
 
        # If COLUMNS is set, take it
1368
 
        try:
1369
 
            return int(os.environ['COLUMNS'])
1370
 
        except (KeyError, ValueError):
1371
 
            return None
1372
 
 
1373
 
    if width <= 0:
1374
 
        # Consider invalid values as meaning no width
1375
 
        return None
1376
 
 
1377
 
    return width
 
1403
        pass
 
1404
    return width, height
 
1405
 
 
1406
_terminal_size = None
 
1407
"""Returns the terminal size as (width, height).
 
1408
 
 
1409
:param width: Default value for width.
 
1410
:param height: Default value for height.
 
1411
 
 
1412
This is defined specifically for each OS and query the size of the controlling
 
1413
terminal. If any error occurs, the provided default values should be returned.
 
1414
"""
 
1415
if sys.platform == 'win32':
 
1416
    _terminal_size = _win32_terminal_size
 
1417
else:
 
1418
    _terminal_size = _ioctl_terminal_size
1378
1419
 
1379
1420
 
1380
1421
def supports_executable():