71
72
from bzrlib import symbol_versioning
75
# Cross platform wall-clock time functionality with decent resolution.
76
# On Linux ``time.clock`` returns only CPU time. On Windows, ``time.time()``
77
# only has a resolution of ~15ms. Note that ``time.clock()`` is not
78
# synchronized with ``time.time()``, this is only meant to be used to find
79
# delta times by subtracting from another call to this function.
80
timer_func = time.time
81
if sys.platform == 'win32':
82
timer_func = time.clock
74
84
# On win32, O_BINARY is used to indicate the file should
75
85
# be opened in binary mode, rather than text mode.
76
86
# On other platforms, O_BINARY doesn't exist, because
1342
1352
"""Return terminal width.
1344
1354
None is returned if the width can't established precisely.
1357
- if BZR_COLUMNS is set, returns its value
1358
- if there is no controlling terminal, returns None
1359
- if COLUMNS is set, returns its value,
1361
From there, we need to query the OS to get the size of the controlling
1365
- get termios.TIOCGWINSZ
1366
- if an error occurs or a negative value is obtained, returns None
1370
- win32utils.get_console_size() decides,
1371
- returns None on error (provided default value)
1347
1374
# If BZR_COLUMNS is set, take it, user is always right
1355
1382
# Don't guess, setting BZR_COLUMNS is the recommended way to override.
1358
if sys.platform == 'win32':
1359
return win32utils.get_console_size(defaultx=None)[0]
1385
# If COLUMNS is set, take it, the terminal knows better (even inside a
1386
# given terminal, the application can decide to set COLUMNS to a lower
1387
# value (splitted screen) or a bigger value (scroll bars))
1389
return int(os.environ['COLUMNS'])
1390
except (KeyError, ValueError):
1393
width, height = _terminal_size(None, None)
1395
# Consider invalid values as meaning no width
1401
def _win32_terminal_size(width, height):
1402
width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
1403
return width, height
1406
def _ioctl_terminal_size(width, height):
1362
1408
import struct, fcntl, termios
1363
1409
s = struct.pack('HHHH', 0, 0, 0, 0)
1364
1410
x = fcntl.ioctl(1, termios.TIOCGWINSZ, s)
1365
width = struct.unpack('HHHH', x)[1]
1411
height, width = struct.unpack('HHHH', x)[0:2]
1366
1412
except (IOError, AttributeError):
1367
# If COLUMNS is set, take it
1369
return int(os.environ['COLUMNS'])
1370
except (KeyError, ValueError):
1374
# Consider invalid values as meaning no width
1414
return width, height
1416
_terminal_size = None
1417
"""Returns the terminal size as (width, height).
1419
:param width: Default value for width.
1420
:param height: Default value for height.
1422
This is defined specifically for each OS and query the size of the controlling
1423
terminal. If any error occurs, the provided default values should be returned.
1425
if sys.platform == 'win32':
1426
_terminal_size = _win32_terminal_size
1428
_terminal_size = _ioctl_terminal_size
1431
def _terminal_size_changed(signum, frame):
1432
"""Set COLUMNS upon receiving a SIGnal for WINdow size CHange."""
1433
width, height = _terminal_size(None, None)
1434
if width is not None:
1435
os.environ['COLUMNS'] = str(width)
1437
if sys.platform == 'win32':
1438
# Martin (gz) mentioned WINDOW_BUFFER_SIZE_RECORD from ReadConsoleInput but
1439
# I've no idea how to plug that in the current design -- vila 20091216
1442
signal.signal(signal.SIGWINCH, _terminal_size_changed)
1380
1445
def supports_executable():