comparison mercurial/ui.py @ 40520:fd60c2afb484

ui: factor out function that writes data to fout/ferr with labeling I'm thinking of adding an option to send status messages to stderr (or a dedicated command-server channel) so that structured output (e.g. JSON) would never be interleaved with non-formatter output. A unified write() interface helps to do that.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 03 Nov 2018 17:32:35 +0900
parents 3c4b9dace7de
children 49746e53ac92
comparison
equal deleted inserted replaced
40519:3c4b9dace7de 40520:fd60c2afb484
945 label = opts.get(r'label', '') 945 label = opts.get(r'label', '')
946 self._buffers[-1].extend(self.label(a, label) for a in args) 946 self._buffers[-1].extend(self.label(a, label) for a in args)
947 else: 947 else:
948 self._buffers[-1].extend(args) 948 self._buffers[-1].extend(args)
949 else: 949 else:
950 self._writenobuf(*args, **opts) 950 self._writenobuf(self._write, *args, **opts)
951 951
952 def _writenobuf(self, *args, **opts): 952 def _writenobuf(self, write, *args, **opts):
953 self._progclear() 953 self._progclear()
954 if self._colormode == 'win32': 954 if self._colormode == 'win32':
955 # windows color printing is its own can of crab, defer to 955 # windows color printing is its own can of crab, defer to
956 # the color module and that is it. 956 # the color module and that is it.
957 color.win32print(self, self._write, *args, **opts) 957 color.win32print(self, write, *args, **opts)
958 else: 958 else:
959 msgs = args 959 msgs = args
960 if self._colormode is not None: 960 if self._colormode is not None:
961 label = opts.get(r'label', '') 961 label = opts.get(r'label', '')
962 msgs = [self.label(a, label) for a in args] 962 msgs = [self.label(a, label) for a in args]
963 self._write(*msgs, **opts) 963 write(*msgs, **opts)
964 964
965 def _write(self, *msgs, **opts): 965 def _write(self, *msgs, **opts):
966 # opencode timeblockedsection because this is a critical path 966 # opencode timeblockedsection because this is a critical path
967 starttime = util.timer() 967 starttime = util.timer()
968 try: 968 try:
974 (util.timer() - starttime) * 1000 974 (util.timer() - starttime) * 1000
975 975
976 def write_err(self, *args, **opts): 976 def write_err(self, *args, **opts):
977 if self._bufferstates and self._bufferstates[-1][0]: 977 if self._bufferstates and self._bufferstates[-1][0]:
978 self.write(*args, **opts) 978 self.write(*args, **opts)
979 return
980 self._progclear()
981 if self._colormode == 'win32':
982 # windows color printing is its own can of crab, defer to
983 # the color module and that is it.
984 color.win32print(self, self._write_err, *args, **opts)
985 else: 979 else:
986 msgs = args 980 self._writenobuf(self._write_err, *args, **opts)
987 if self._colormode is not None:
988 label = opts.get(r'label', '')
989 msgs = [self.label(a, label) for a in args]
990 self._write_err(*msgs, **opts)
991 981
992 def _write_err(self, *msgs, **opts): 982 def _write_err(self, *msgs, **opts):
993 try: 983 try:
994 with self.timeblockedsection('stdio'): 984 with self.timeblockedsection('stdio'):
995 if not getattr(self.fout, 'closed', False): 985 if not getattr(self.fout, 'closed', False):
1350 If ui is not interactive, the default is returned. 1340 If ui is not interactive, the default is returned.
1351 """ 1341 """
1352 if not self.interactive(): 1342 if not self.interactive():
1353 self.write(msg, ' ', default or '', "\n") 1343 self.write(msg, ' ', default or '', "\n")
1354 return default 1344 return default
1355 self._writenobuf(msg, label='ui.prompt') 1345 self._writenobuf(self._write, msg, label='ui.prompt')
1356 self.flush() 1346 self.flush()
1357 try: 1347 try:
1358 r = self._readline() 1348 r = self._readline()
1359 if not r: 1349 if not r:
1360 r = default 1350 r = default