utility: add a function to prompt user to choose a revision
authorPulkit Goyal <7895pulkit@gmail.com>
Wed, 14 Mar 2018 17:59:53 +0530
changeset 3538 b314c64f336b
parent 3537 8b5093f333dc
child 3539 9bd64091e880
utility: add a function to prompt user to choose a revision This patch adds a utility function to interactively prompt user to choose a revision to proceed in cases when there are multiple revisions which are candidates. The function will be used in `hg evolve`, `hg next`, `hg prev` etc. If user feedback will be good, I plan to move it to core and use it at more possible places.
hgext3rd/evolve/utility.py
--- a/hgext3rd/evolve/utility.py	Wed Mar 14 17:20:20 2018 +0530
+++ b/hgext3rd/evolve/utility.py	Wed Mar 14 17:59:53 2018 +0530
@@ -7,6 +7,8 @@
 
 import collections
 
+from mercurial.i18n import _
+
 from mercurial.node import nullrev
 
 from . import (
@@ -129,3 +131,50 @@
         raise MultipleSuccessorsError(newer)
 
     return repo[newer[0][0]].rev()
+
+def revselectionprompt(ui, repo, revs, customheader=""):
+    """function to prompt user to choose a revision from all the revs and return
+    that revision for further tasks
+
+    revs is a list of rev number of revision from which one revision should be
+    choosed by the user
+    customheader is a text which the caller wants as the header of the prompt
+    which will list revisions to select
+
+    returns value is:
+        rev number of revision choosed: if user choose a revision
+        None: if user entered a wrong input, user quit the prompt,
+              ui.interactive is not set
+    """
+
+    # ui.interactive is not set, fallback to default behavior and avoid showing
+    # the prompt
+    if not ui.configbool('ui', 'interactive'):
+        return None
+
+    promptmsg = customheader + "\n"
+    for idx, rev in enumerate(revs):
+        curctx = repo[rev]
+        revmsg = "%d: [%s] %s\n" % (idx, curctx,
+                                    curctx.description().split("\n")[0])
+        promptmsg += revmsg
+
+    promptmsg += "q: quit the prompt\n"
+    promptmsg += "enter the index of the revision you want to select:"
+    idxselected = ui.prompt(promptmsg)
+
+    intidx = None
+    try:
+        intidx = int(idxselected)
+    except ValueError:
+        if idxselected == 'q':
+            return None
+        ui.write_err(_("invalid value '%s' entered for index\n") % idxselected)
+        return None
+
+    if intidx >= len(revs) or intidx < 0:
+        # we can make this error message better
+        ui.write_err(_("invalid value '%d' entered for index\n") % intidx)
+        return None
+
+    return revs[intidx]