Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/ui.py @ 20265:e5803150ea1d
ui: add "extractchoices()" to share the logic to extract choices from prompt
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Mon, 02 Dec 2013 00:50:29 +0900 |
parents | e828975722c8 |
children | 5614f8cf0861 |
comparison
equal
deleted
inserted
replaced
20264:d9e1c167943b | 20265:e5803150ea1d |
---|---|
638 return default | 638 return default |
639 return r | 639 return r |
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: |
646 | 660 |
647 "would you like fries with that (Yn)? $$ &Yes $$ &No" | 661 "would you like fries with that (Yn)? $$ &Yes $$ &No" |
649 The index of the choice is returned. Responses are case | 663 The index of the choice is returned. Responses are case |
650 insensitive. If ui is not interactive, the default is | 664 insensitive. If ui is not interactive, the default is |
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: |
661 return resps.index(r.lower()) | 673 return resps.index(r.lower()) |
662 self.write(_("unrecognized response\n")) | 674 self.write(_("unrecognized response\n")) |