[PATCH 1 of 3] record: refactor the record extension's prompt()

Steve Losh steve at stevelosh.com
Mon Jan 4 18:44:52 CST 2010


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1262652127 18000
# Node ID 1fa39ad1ca75699329f538aae321fcdcfbb5dfda
# Parent  f5e55f1ca92738458295ca9f82f31c54a89babbb
record: refactor the record extension's prompt()

This changeset refactors record.filterpatch.prompt() to be more flexible when
defining options.

diff --git a/hgext/record.py b/hgext/record.py
--- a/hgext/record.py
+++ b/hgext/record.py
@@ -284,34 +284,53 @@
             return resp_file[0]
         while True:
             resps = _('[Ynsfdaq?]')
-            choices = (_('&Yes, record this change'),
-                    _('&No, skip this change'),
-                    _('&Skip remaining changes to this file'),
-                    _('Record remaining changes to this &file'),
-                    _('&Done, skip remaining changes and files'),
-                    _('Record &all changes to all remaining files'),
-                    _('&Quit, recording no changes'),
-                    _('&?'))
+            all_choices = {
+                'yes': _('&Yes, record this change'),
+                'no': _('&No, skip this change'),
+                'skip': _('&Skip remaining changes to this file'),
+                'file': _('Record remaining changes to this &file'),
+                'done': _('&Done, skip remaining changes and files'),
+                'all': _('Record &all changes to all remaining files'),
+                'quit': _('&Quit, recording no changes'),
+                'help': _('&?'),
+            }
+            choices = (all_choices['yes'],
+                    all_choices['no'],
+                    all_choices['skip'],
+                    all_choices['file'],
+                    all_choices['done'],
+                    all_choices['all'],
+                    all_choices['quit'],
+                    all_choices['help'],)
             r = ui.promptchoice("%s %s" % (query, resps), choices)
-            if r == 7: # ?
+            choice = [k for k in all_choices if all_choices[k] == choices[r]][0]
+            if choice == 'help':
                 doc = gettext(record.__doc__)
                 c = doc.find(_('y - record this change'))
-                for l in doc[c:].splitlines():
+                doc_lines = doc[c:].splitlines()
+                if not at_hunk:
+                    doc_lines = [l for l in doc_lines if not l.startswith('p - split')]
+                else:
+                    doc_lines = filter(
+                        lambda l: l.replace('(if you are currently at a hunk)', ''),
+                        doc_lines
+                    )
+                for l in doc_lines:
                     if l: ui.write(l.strip(), '\n')
                 continue
-            elif r == 0: # yes
+            elif choice == 'yes':
                 ret = True
-            elif r == 1: # no
+            elif choice == 'no':
                 ret = False
-            elif r == 2: # Skip
+            elif choice == 'skip':
                 ret = resp_file[0] = False
-            elif r == 3: # file (Record remaining)
+            elif choice == 'file':
                 ret = resp_file[0] = True
-            elif r == 4: # done, skip remaining
+            elif choice == 'done':
                 ret = resp_all[0] = False
-            elif r == 5: # all
+            elif choice == 'all':
                 ret = resp_all[0] = True
-            elif r == 6: # quit
+            elif choice == 'quit':
                 raise util.Abort(_('user quit'))
             return ret
     pos, total = 0, len(chunks) - 1
@@ -370,6 +389,7 @@
 
       y - record this change
       n - skip this change
+      p - split this hunk (if you are currently at a hunk)
 
       s - skip remaining changes to this file
       f - record remaining changes to this file


More information about the Mercurial-devel mailing list