[PATCH 4 of 8] ui: extract helpers to write message with type or label
Yuya Nishihara
yuya at tcha.org
Thu Nov 8 14:24:42 UTC 2018
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1421571328 -32400
# Sun Jan 18 17:55:28 2015 +0900
# Node ID c03e57f814d840dd97eb5e88b4534250456a2db6
# Parent 6a4ccc024119f3bd983ab2cdf6337396a7bcab7e
ui: extract helpers to write message with type or label
This provides a 'type' attribute to command-server clients, which seems
more solid than relying on 'ui.<type>' labels. In future patches,
type='progress' will be added to send raw progress information.
diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -78,6 +78,9 @@ class channeledmessage(object):
data length (unsigned int),
encoded message and metadata, as a flat key-value dict.
+
+ Each message should have 'type' attribute. Messages of unknown type
+ should be ignored.
"""
# teach ui that write() can take **opts
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1042,6 +1042,12 @@ class ui(object):
self._blockedtimes['stdio_blocked'] += \
(util.timer() - starttime) * 1000
+ def _writemsg(self, dest, *args, **opts):
+ _writemsgwith(self._write, dest, *args, **opts)
+
+ def _writemsgnobuf(self, dest, *args, **opts):
+ _writemsgwith(self._writenobuf, dest, *args, **opts)
+
def flush(self):
# opencode timeblockedsection because this is a critical path
starttime = util.timer()
@@ -1388,18 +1394,18 @@ class ui(object):
If ui is not interactive, the default is returned.
"""
if not self.interactive():
- self._write(self._fmsgout, msg, ' ', label='ui.prompt')
- self._write(self._fmsgout, default or '', "\n",
- label='ui.promptecho')
+ self._writemsg(self._fmsgout, msg, ' ', type='prompt')
+ self._writemsg(self._fmsgout, default or '', "\n",
+ type='promptecho')
return default
- self._writenobuf(self._fmsgout, msg, label='ui.prompt')
+ self._writemsgnobuf(self._fmsgout, msg, type='prompt')
self.flush()
try:
r = self._readline()
if not r:
r = default
if self.configbool('ui', 'promptecho'):
- self._write(self._fmsgout, r, "\n", label='ui.promptecho')
+ self._writemsg(self._fmsgout, r, "\n", type='promptecho')
return r
except EOFError:
raise error.ResponseExpected()
@@ -1450,14 +1456,14 @@ class ui(object):
if r.lower() in resps:
return resps.index(r.lower())
# TODO: shouldn't it be a warning?
- self._write(self._fmsgout, _("unrecognized response\n"))
+ self._writemsg(self._fmsgout, _("unrecognized response\n"))
def getpass(self, prompt=None, default=None):
if not self.interactive():
return default
try:
- self._write(self._fmsgerr, prompt or _('password: '),
- label='ui.prompt')
+ self._writemsg(self._fmsgerr, prompt or _('password: '),
+ type='prompt')
# disable getpass() only if explicitly specified. it's still valid
# to interact with tty even if fin is not a tty.
with self.timeblockedsection('stdio'):
@@ -1477,24 +1483,21 @@ class ui(object):
This adds an output label of "ui.status".
'''
if not self.quiet:
- opts[r'label'] = opts.get(r'label', '') + ' ui.status'
- self._write(self._fmsgout, *msg, **opts)
+ self._writemsg(self._fmsgout, type='status', *msg, **opts)
def warn(self, *msg, **opts):
'''write warning message to output (stderr)
This adds an output label of "ui.warning".
'''
- opts[r'label'] = opts.get(r'label', '') + ' ui.warning'
- self._write(self._fmsgerr, *msg, **opts)
+ self._writemsg(self._fmsgerr, type='warning', *msg, **opts)
def error(self, *msg, **opts):
'''write error message to output (stderr)
This adds an output label of "ui.error".
'''
- opts[r'label'] = opts.get(r'label', '') + ' ui.error'
- self._write(self._fmsgerr, *msg, **opts)
+ self._writemsg(self._fmsgerr, type='error', *msg, **opts)
def note(self, *msg, **opts):
'''write note to output (if ui.verbose is True)
@@ -1502,8 +1505,7 @@ class ui(object):
This adds an output label of "ui.note".
'''
if self.verbose:
- opts[r'label'] = opts.get(r'label', '') + ' ui.note'
- self._write(self._fmsgout, *msg, **opts)
+ self._writemsg(self._fmsgout, type='note', *msg, **opts)
def debug(self, *msg, **opts):
'''write debug message to output (if ui.debugflag is True)
@@ -1511,8 +1513,7 @@ class ui(object):
This adds an output label of "ui.debug".
'''
if self.debugflag:
- opts[r'label'] = opts.get(r'label', '') + ' ui.debug'
- self._write(self._fmsgout, *msg, **opts)
+ self._writemsg(self._fmsgout, type='debug', *msg, **opts)
def edit(self, text, user, extra=None, editform=None, pending=None,
repopath=None, action=None):
@@ -1981,3 +1982,14 @@ def _selectmsgdests(ui):
if name == b'stderr':
return ui.ferr, ui.ferr
raise error.Abort(b'invalid ui.message-output destination: %s' % name)
+
+def _writemsgwith(write, dest, *args, **opts):
+ """Write ui message with the given ui._write*() function
+
+ The specified message type is translated to 'ui.<type>' label if the dest
+ isn't a structured channel, so that the message will be colorized.
+ """
+ # TODO: maybe change 'type' to a mandatory option
+ if r'type' in opts and not getattr(dest, 'structured', False):
+ opts[r'label'] = opts.get(r'label', '') + ' ui.%s' % opts.pop(r'type')
+ write(dest, *args, **opts)
diff --git a/tests/test-commandserver.t b/tests/test-commandserver.t
--- a/tests/test-commandserver.t
+++ b/tests/test-commandserver.t
@@ -745,11 +745,11 @@ structured message channel:
pid: * (glob)
pgid: * (glob)
*** runcommand -R repo2 verify
- message: '\xa2DdataTchecking changesets\nElabelJ ui.status'
- message: '\xa2DdataSchecking manifests\nElabelJ ui.status'
- message: '\xa2DdataX0crosschecking files in changesets and manifests\nElabelJ ui.status'
- message: '\xa2DdataOchecking files\nElabelJ ui.status'
- message: '\xa2DdataX/checked 0 changesets with 0 changes to 0 files\nElabelJ ui.status'
+ message: '\xa2DdataTchecking changesets\nDtypeFstatus'
+ message: '\xa2DdataSchecking manifests\nDtypeFstatus'
+ message: '\xa2DdataX0crosschecking files in changesets and manifests\nDtypeFstatus'
+ message: '\xa2DdataOchecking files\nDtypeFstatus'
+ message: '\xa2DdataX/checked 0 changesets with 0 changes to 0 files\nDtypeFstatus'
bad message encoding:
More information about the Mercurial-devel
mailing list