commandserver: attach prompt default and choices to message
authorYuya Nishihara <yuya@tcha.org>
Sun, 04 Nov 2018 12:17:20 +0900
changeset 40592 83e571ea06a9
parent 40591 c49283e740da
child 40593 6f0941f4a184
commandserver: attach prompt default and choices to message These attributes are important to provide a GUI prompt to user.
mercurial/ui.py
tests/test-commandserver.t
--- a/mercurial/ui.py	Thu Nov 08 22:25:04 2018 +0900
+++ b/mercurial/ui.py	Sun Nov 04 12:17:20 2018 +0900
@@ -1390,12 +1390,16 @@
         """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()
@@ -1449,7 +1453,7 @@
         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?
--- a/tests/test-commandserver.t	Thu Nov 08 22:25:04 2018 +0900
+++ b/tests/test-commandserver.t	Sun Nov 04 12:17:20 2018 +0900
@@ -619,6 +619,10 @@
   > @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 @@
   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