1342
1342
"""Return terminal width.
1344
1344
None is returned if the width can't established precisely.
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,
1351
From there, we need to query the OS to get the size of the controlling
1355
- get termios.TIOCGWINSZ
1356
- if an error occurs or a negative value is obtained, returns None
1360
- win32utils.get_console_size() decides,
1361
- returns None on error (provided default value)
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.
1358
if sys.platform == 'win32':
1359
return win32utils.get_console_size(defaultx=None)[0]
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))
1379
return int(os.environ['COLUMNS'])
1380
except (KeyError, ValueError):
1383
width, height = _terminal_size(None, None)
1385
# Consider invalid values as meaning no width
1391
def _win32_terminal_size(width, height):
1392
width, height = win32utils.get_console_size(defaultx=width, defaulty=height)
1393
return width, height
1396
def _ioctl_terminal_size(width, height):
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
1369
return int(os.environ['COLUMNS'])
1370
except (KeyError, ValueError):
1374
# Consider invalid values as meaning no width
1404
return width, height
1406
_terminal_size = None
1407
"""Returns the terminal size as (width, height).
1409
:param width: Default value for width.
1410
:param height: Default value for height.
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.
1415
if sys.platform == 'win32':
1416
_terminal_size = _win32_terminal_size
1418
_terminal_size = _ioctl_terminal_size
1380
1421
def supports_executable():