comparison mercurial/util.py @ 11010:18e81d42ee5c stable

util: fix default termwidth() under Windows sys.stdout.write('-'*80 + '\n') or sys.stdout.write('-'*80 + '\r') do not work on Windows as they do on unix. On a 80 columns Windows console, the extra CR or LF are interpreted as if belonging to the next line, so the first command displays 2 lines (only one on unix) and the second one leave the line visible and move back to the following line. To avoid this, we sacrifice one column under Windows.
author Patrick Mezard <pmezard@gmail.com>
date Mon, 26 Apr 2010 22:30:40 +0200
parents 997173e49115
children 648130161e4d 640d419725d0
comparison
equal deleted inserted replaced
11007:a0102da324ab 11010:18e81d42ee5c
1243 1243
1244 def uirepr(s): 1244 def uirepr(s):
1245 # Avoid double backslash in Windows path repr() 1245 # Avoid double backslash in Windows path repr()
1246 return repr(s).replace('\\\\', '\\') 1246 return repr(s).replace('\\\\', '\\')
1247 1247
1248 def termwidth():
1249 if 'COLUMNS' in os.environ:
1250 try:
1251 return int(os.environ['COLUMNS'])
1252 except ValueError:
1253 pass
1254 try:
1255 import termios, array, fcntl
1256 for dev in (sys.stderr, sys.stdout, sys.stdin):
1257 try:
1258 try:
1259 fd = dev.fileno()
1260 except AttributeError:
1261 continue
1262 if not os.isatty(fd):
1263 continue
1264 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
1265 return array.array('h', arri)[1]
1266 except ValueError:
1267 pass
1268 except IOError, e:
1269 if e[0] == errno.EINVAL:
1270 pass
1271 else:
1272 raise
1273 except ImportError:
1274 pass
1275 return 80
1276
1277 def wrap(line, hangindent, width=None): 1248 def wrap(line, hangindent, width=None):
1278 if width is None: 1249 if width is None:
1279 width = termwidth() - 2 1250 width = termwidth() - 2
1280 if width <= hangindent: 1251 if width <= hangindent:
1281 # adjust for weird terminal size 1252 # adjust for weird terminal size
1355 def all(iterable): 1326 def all(iterable):
1356 for i in iterable: 1327 for i in iterable:
1357 if not i: 1328 if not i:
1358 return False 1329 return False
1359 return True 1330 return True
1331
1332 def termwidth():
1333 if 'COLUMNS' in os.environ:
1334 try:
1335 return int(os.environ['COLUMNS'])
1336 except ValueError:
1337 pass
1338 return termwidth_()