Patchwork [6,of,8] commandserver: attach prompt default and choices to message

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 8, 2018, 2:24 p.m.
Message ID <ae086b413ab2bdf76d32.1541687084@mimosa>
Download mbox | patch
Permalink /patch/36475/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 8, 2018, 2:24 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1541301440 -32400
#      Sun Nov 04 12:17:20 2018 +0900
# Node ID ae086b413ab2bdf76d3207d9b6250f145a65eaf3
# Parent  601786ab02a1925a42bf63fac605322a135b040b
commandserver: attach prompt default and choices to message

These attributes are important to provide a GUI prompt to user.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1393,12 +1393,16 @@  class ui(object):
         """Prompt user with msg, read response.
         If ui is not interactive, the default is returned.
         """
+        return self._prompt(msg, default=default)
+
+    def _prompt(self, msg, **opts):
+        default = opts[r'default']
         if not self.interactive():
-            self._writemsg(self._fmsgout, msg, ' ', type='prompt')
+            self._writemsg(self._fmsgout, msg, ' ', type='prompt', **opts)
             self._writemsg(self._fmsgout, default or '', "\n",
                            type='promptecho')
             return default
-        self._writemsgnobuf(self._fmsgout, msg, type='prompt')
+        self._writemsgnobuf(self._fmsgout, msg, type='prompt', **opts)
         self.flush()
         try:
             r = self._readline()
@@ -1452,7 +1456,7 @@  class ui(object):
         msg, choices = self.extractchoices(prompt)
         resps = [r for r, t in choices]
         while True:
-            r = self.prompt(msg, resps[default])
+            r = self._prompt(msg, default=resps[default], choices=choices)
             if r.lower() in resps:
                 return resps.index(r.lower())
             # TODO: shouldn't it be a warning?
diff --git a/tests/test-commandserver.t b/tests/test-commandserver.t
--- a/tests/test-commandserver.t
+++ b/tests/test-commandserver.t
@@ -619,6 +619,10 @@  changelog and manifest would have invali
   > @command(b"debugprompt", norepo=True)
   > def debugprompt(ui):
   >     ui.write(b"%s\n" % ui.prompt(b"prompt:"))
+  > @command(b"debugpromptchoice", norepo=True)
+  > def debugpromptchoice(ui):
+  >     msg = b"promptchoice (y/n)? $$ &Yes $$ &No"
+  >     ui.write(b"%d\n" % ui.promptchoice(msg))
   > @command(b"debugreadstdin", norepo=True)
   > def debugreadstdin(ui):
   >     ui.write(b"read: %r\n" % sys.stdin.read(1))
@@ -751,6 +755,24 @@  structured message channel:
   message: '\xa2DdataOchecking files\nDtypeFstatus'
   message: '\xa2DdataX/checked 0 changesets with 0 changes to 0 files\nDtypeFstatus'
 
+  >>> from hgclient import checkwith, readchannel, runcommand, stringio
+  >>> @checkwith(extraargs=[b'--config', b'ui.message-output=channel',
+  ...                       b'--config', b'cmdserver.message-encodings=cbor',
+  ...                       b'--config', b'extensions.dbgui=dbgui.py'])
+  ... def prompt(server):
+  ...     readchannel(server)
+  ...     interactive = [b'--config', b'ui.interactive=True']
+  ...     runcommand(server, [b'debugprompt'] + interactive,
+  ...                input=stringio(b'5678\n'))
+  ...     runcommand(server, [b'debugpromptchoice'] + interactive,
+  ...                input=stringio(b'n\n'))
+  *** runcommand debugprompt --config ui.interactive=True
+  message: '\xa3DdataGprompt:GdefaultAyDtypeFprompt'
+   5678
+  *** runcommand debugpromptchoice --config ui.interactive=True
+  message: '\xa4Gchoices\x82\x82AyCYes\x82AnBNoDdataTpromptchoice (y/n)? GdefaultAyDtypeFprompt'
+   1
+
 bad message encoding:
 
   $ hg serve --cmdserver pipe --config ui.message-output=channel