Mercurial > public > mercurial-scm > hg
comparison mercurial/ui.py @ 42119:2d428b859282
readline: provide styled prompt to readline (issue6070)
Differential Revision: https://phab.mercurial-scm.org/D6168
author | Kyle Lippincott <spectral@google.com> |
---|---|
date | Mon, 15 Apr 2019 14:32:47 -0700 |
parents | ba064f95175e |
children | 3de4f17f4824 |
comparison
equal
deleted
inserted
replaced
42118:967c098eed33 | 42119:2d428b859282 |
---|---|
1428 # usually those are non-interactive | 1428 # usually those are non-interactive |
1429 return self._isatty(self._fout) | 1429 return self._isatty(self._fout) |
1430 | 1430 |
1431 return i | 1431 return i |
1432 | 1432 |
1433 def _readline(self): | 1433 def _readline(self, prompt=' ', promptopts=None): |
1434 # Replacing stdin/stdout temporarily is a hard problem on Python 3 | 1434 # Replacing stdin/stdout temporarily is a hard problem on Python 3 |
1435 # because they have to be text streams with *no buffering*. Instead, | 1435 # because they have to be text streams with *no buffering*. Instead, |
1436 # we use rawinput() only if call_readline() will be invoked by | 1436 # we use rawinput() only if call_readline() will be invoked by |
1437 # PyOS_Readline(), so no I/O will be made at Python layer. | 1437 # PyOS_Readline(), so no I/O will be made at Python layer. |
1438 usereadline = (self._isatty(self._fin) and self._isatty(self._fout) | 1438 usereadline = (self._isatty(self._fin) and self._isatty(self._fout) |
1447 readline.read_history_file | 1447 readline.read_history_file |
1448 # windows sometimes raises something other than ImportError | 1448 # windows sometimes raises something other than ImportError |
1449 except Exception: | 1449 except Exception: |
1450 usereadline = False | 1450 usereadline = False |
1451 | 1451 |
1452 if self._colormode == 'win32' or not usereadline: | |
1453 if not promptopts: | |
1454 promptopts = {} | |
1455 self._writemsgnobuf(self._fmsgout, prompt, type='prompt', | |
1456 **promptopts) | |
1457 self.flush() | |
1458 prompt = ' ' | |
1459 else: | |
1460 prompt = self.label(prompt, 'ui.prompt') + ' ' | |
1461 | |
1452 # prompt ' ' must exist; otherwise readline may delete entire line | 1462 # prompt ' ' must exist; otherwise readline may delete entire line |
1453 # - http://bugs.python.org/issue12833 | 1463 # - http://bugs.python.org/issue12833 |
1454 with self.timeblockedsection('stdio'): | 1464 with self.timeblockedsection('stdio'): |
1455 if usereadline: | 1465 if usereadline: |
1456 line = encoding.strtolocal(pycompat.rawinput(r' ')) | 1466 line = encoding.strtolocal(pycompat.rawinput(prompt)) |
1457 # When stdin is in binary mode on Windows, it can cause | 1467 # When stdin is in binary mode on Windows, it can cause |
1458 # raw_input() to emit an extra trailing carriage return | 1468 # raw_input() to emit an extra trailing carriage return |
1459 if pycompat.oslinesep == b'\r\n' and line.endswith(b'\r'): | 1469 if pycompat.oslinesep == b'\r\n' and line.endswith(b'\r'): |
1460 line = line[:-1] | 1470 line = line[:-1] |
1461 else: | 1471 else: |
1462 self._fout.write(b' ') | 1472 self._fout.write(pycompat.bytestr(prompt)) |
1463 self._fout.flush() | 1473 self._fout.flush() |
1464 line = self._fin.readline() | 1474 line = self._fin.readline() |
1465 if not line: | 1475 if not line: |
1466 raise EOFError | 1476 raise EOFError |
1467 line = line.rstrip(pycompat.oslinesep) | 1477 line = line.rstrip(pycompat.oslinesep) |
1479 if not self.interactive(): | 1489 if not self.interactive(): |
1480 self._writemsg(self._fmsgout, msg, ' ', type='prompt', **opts) | 1490 self._writemsg(self._fmsgout, msg, ' ', type='prompt', **opts) |
1481 self._writemsg(self._fmsgout, default or '', "\n", | 1491 self._writemsg(self._fmsgout, default or '', "\n", |
1482 type='promptecho') | 1492 type='promptecho') |
1483 return default | 1493 return default |
1484 self._writemsgnobuf(self._fmsgout, msg, type='prompt', **opts) | |
1485 self.flush() | |
1486 try: | 1494 try: |
1487 r = self._readline() | 1495 r = self._readline(prompt=msg, promptopts=opts) |
1488 if not r: | 1496 if not r: |
1489 r = default | 1497 r = default |
1490 if self.configbool('ui', 'promptecho'): | 1498 if self.configbool('ui', 'promptecho'): |
1491 self._writemsg(self._fmsgout, r, "\n", type='promptecho') | 1499 self._writemsg(self._fmsgout, r, "\n", type='promptecho') |
1492 return r | 1500 return r |