[PATCH] With corrected documentation
Matt Mackall
mpm at selenic.com
Tue Aug 9 16:55:24 CDT 2011
On Tue, 2011-08-09 at 23:24 +0200, Ingo Proetel wrote:
> # HG changeset patch
> # User Ingo Proetel <proetel at aicas.de>
> # Date 1312924615 -7200
> # Node ID 3ab34f9c42699a8136bdb23a2f1ebb0aa445b4e6
> # Parent f4522df38c658a1768ffe08c864575e119b327ce
> ui: add function 'promptselection'
I'm not a translator, so I'm not sure if this is a concern in practice,
but I suspect it gets confusing if the prompt text is split into a bunch
of different parts, all of which have to end up agreeing in terms of
keys and so on. Perhaps we'd make things easier for translators if we
had a single prompt string that put everything in the same place:
promptselection(_(
'''What shall we do?\n
abort:&Abort command\n
retry:&Retry command\n
ignore:&Ignore error\n'''), ...)
Also, my eye is now drawn to a number of coding style issues:
> diff -r f4522df38c65 -r 3ab34f9c4269 mercurial/ui.py
> --- a/mercurial/ui.py Tue Aug 02 15:21:10 2011 -0400
> +++ b/mercurial/ui.py Tue Aug 09 23:16:55 2011 +0200
> @@ -6,7 +6,7 @@
> # GNU General Public License version 2 or any later version.
>
> from i18n import _
> -import errno, getpass, os, socket, sys, tempfile, traceback
> +import errno, getpass, os, socket, sys, tempfile, traceback, re
Hmm, I wouldn't think we'd need regexes for something so simple.
> import config, scmutil, util, error
>
> class ui(object):
> @@ -584,6 +584,60 @@
> return resps.index(r.lower())
> self.write(_("unrecognized response\n"))
>
> + def promptselection(self, msg, choices, default=None):
> + '''Prompt user with msg, read response, and ensure it matches
> + one of the provided choices. choices is a sequence of tuples
> + with the format: (('command',_('help for &command')),...). command is a
Spaces follow commas.
> + non-localized string that will be the return value. Help texts are
> + localized and mark the expected user selection char with a '&'. A help
> + command for will be provided for input '?'. Responses are case
> + insensitive. If ui is not interactive, the default is returned.
> + If no default is given the first entry in choices is used.
> + Example:
> + choices = (('abort',_('&Abort command')),
> + ('new',_('Create &new request')),
> + ('reopen',_('&Reopen request')) )
> + request = ui.promptselection('now what?', choices,'new')
> + if request == 'abort':
Whitespace abomination.
> + raise util.Abort('User aborted')
> + elif request == 'new':
> + ui.status('creating new')
> + else:#request == 'reopen'
Hmm.
> + ui.status('reopen existing')
> + will ask:
> + $now what? [aNr?] ?
> + a Abort command
> + n Create new request
> + r Reopen request
> + $now what? [aNr?] n
> + creating new
> + '''
> + respsString = ''
> + for choice in choices:
> + command = choice[0]
> + helpText = choice[1]
> + commandChar = helpText[helpText.index('&')+1].lower()
x + y
Camelcase is forbidden.
What happens with lower() when text is not ASCII?
> + while True:
> + r = self.prompt('%s [%s?]' %(msg, respsString), defaultCommandChar)
x % y
> + index = respsString.lower().find(r.lower())
> + if index > -1:
> + return choices[index][0]
> + elif r == '?':
> + for choice in choices:
> + commandChar = choice[1][choice[1].index('&')+1].lower()
> + helpText = re.sub('\&','',choice[1])
x.replace('&', '') <- faster, simpler
> + self.write(' %s %s\n' % (commandChar, helpText))
> + self.write('\n')
> + else :
> + self.write(_("unrecognized response\n"))
> +
--
Mathematics is the supreme nostalgia of our time.
More information about the Mercurial-devel
mailing list