mercurial/ui.py
changeset 27408 2916ebaef312
parent 27275 f2cd240f2f7c
parent 27392 00aa37c65e0a
child 27500 62b1b6a89d2a
equal deleted inserted replaced
27407:bf4d5d8dc2aa 27408:2916ebaef312
     9 
     9 
    10 import errno
    10 import errno
    11 import getpass
    11 import getpass
    12 import inspect
    12 import inspect
    13 import os
    13 import os
       
    14 import re
    14 import socket
    15 import socket
    15 import sys
    16 import sys
    16 import tempfile
    17 import tempfile
    17 import traceback
    18 import traceback
    18 
    19 
   810     def extractchoices(prompt):
   811     def extractchoices(prompt):
   811         """Extract prompt message and list of choices from specified prompt.
   812         """Extract prompt message and list of choices from specified prompt.
   812 
   813 
   813         This returns tuple "(message, choices)", and "choices" is the
   814         This returns tuple "(message, choices)", and "choices" is the
   814         list of tuple "(response character, text without &)".
   815         list of tuple "(response character, text without &)".
   815         """
   816 
   816         parts = prompt.split('$$')
   817         >>> ui.extractchoices("awake? $$ &Yes $$ &No")
   817         msg = parts[0].rstrip(' ')
   818         ('awake? ', [('y', 'Yes'), ('n', 'No')])
   818         choices = [p.strip(' ') for p in parts[1:]]
   819         >>> ui.extractchoices("line\\nbreak? $$ &Yes $$ &No")
       
   820         ('line\\nbreak? ', [('y', 'Yes'), ('n', 'No')])
       
   821         >>> ui.extractchoices("want lots of $$money$$?$$Ye&s$$N&o")
       
   822         ('want lots of $$money$$?', [('s', 'Yes'), ('o', 'No')])
       
   823         """
       
   824 
       
   825         # Sadly, the prompt string may have been built with a filename
       
   826         # containing "$$" so let's try to find the first valid-looking
       
   827         # prompt to start parsing. Sadly, we also can't rely on
       
   828         # choices containing spaces, ASCII, or basically anything
       
   829         # except an ampersand followed by a character.
       
   830         m = re.match(r'(?s)(.+?)\$\$([^\$]*&[^ \$].*)', prompt)
       
   831         msg = m.group(1)
       
   832         choices = [p.strip(' ') for p in m.group(2).split('$$')]
   819         return (msg,
   833         return (msg,
   820                 [(s[s.index('&') + 1].lower(), s.replace('&', '', 1))
   834                 [(s[s.index('&') + 1].lower(), s.replace('&', '', 1))
   821                  for s in choices])
   835                  for s in choices])
   822 
   836 
   823     def promptchoice(self, prompt, default=0):
   837     def promptchoice(self, prompt, default=0):