##// END OF EJS Templates
ui: add "extractchoices()" to share the logic to extract choices from prompt
FUJIWARA Katsunori -
r20265:e5803150 default
parent child Browse files
Show More
@@ -640,6 +640,20 b' class ui(object):'
640 except EOFError:
640 except EOFError:
641 raise util.Abort(_('response expected'))
641 raise util.Abort(_('response expected'))
642
642
643 @staticmethod
644 def extractchoices(prompt):
645 """Extract prompt message and list of choices from specified prompt.
646
647 This returns tuple "(message, choices)", and "choices" is the
648 list of tuple "(response character, text without &)".
649 """
650 parts = prompt.split('$$')
651 msg = parts[0].rstrip(' ')
652 choices = [p.strip(' ') for p in parts[1:]]
653 return (msg,
654 [(s[s.index('&') + 1].lower(), s.replace('&', '', 1))
655 for s in choices])
656
643 def promptchoice(self, prompt, default=0):
657 def promptchoice(self, prompt, default=0):
644 """Prompt user with a message, read response, and ensure it matches
658 """Prompt user with a message, read response, and ensure it matches
645 one of the provided choices. The prompt is formatted as follows:
659 one of the provided choices. The prompt is formatted as follows:
@@ -651,10 +665,8 b' class ui(object):'
651 returned.
665 returned.
652 """
666 """
653
667
654 parts = prompt.split('$$')
668 msg, choices = self.extractchoices(prompt)
655 msg = parts[0].rstrip(' ')
669 resps = [r for r, t in choices]
656 choices = [p.strip(' ') for p in parts[1:]]
657 resps = [s[s.index('&') + 1].lower() for s in choices]
658 while True:
670 while True:
659 r = self.prompt(msg, resps[default])
671 r = self.prompt(msg, resps[default])
660 if r.lower() in resps:
672 if r.lower() in resps:
General Comments 0
You need to be logged in to leave comments. Login now