1294
1294
normalized_filename = _inaccessible_normalized_filename
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."""
1304
# If the env var is set, take it, user is always right
1306
return int(os.environ['COLUMNS'])
1307
except (KeyError, ValueError):
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
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
1308
1322
if sys.platform == 'win32':
1309
return win32utils.get_console_size()[0]
1323
return win32utils.get_console_size(default_tty_width)[0]
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]
1320
width = int(os.environ['COLUMNS'])
1330
except (IOError, AttributeError):
1334
width = default_tty_width