From 7f92c1b30f4d0e56ff8840cbdd7fc109085af696 Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Sun, 30 Aug 2009 11:48:39 -0400 Subject: Add helper functions for handling abbrevations abbreviation_help_string(['apple', 'pear', 'potato']) => '[a]pple, [pe]ar, [po]tato' expand_abbreviation('pea', ['apple', 'pear', 'potato']) => 'pear' --- git-bz | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/git-bz b/git-bz index 99136ff..792d0b7 100755 --- a/git-bz +++ b/git-bz @@ -706,6 +706,29 @@ def split_subject_body(lines): return subject, "".join(lines[i + 1:]).strip() +def _shortest_unique_abbreviation(full, l): + for i in xrange(1, len(full) + 1): + abbrev = full[0:i] + if not any((x != full and x.startswith(abbrev) for x in l)): + return abbrev + # Duplicate items or one item is a prefix of another + raise ValueError("%s has no unique abbreviation in %s" % (full, l)) + +def _abbreviation_item_help(full, l): + abbrev = _shortest_unique_abbreviation(full, l) + return '[%s]%s' % (abbrev, full[len(abbrev):]) + +# Return '[a]pple, [pe]ar, [po]tato' +def abbreviation_help_string(l): + return ", ".join((_abbreviation_item_help(full, l) for full in l)) + +# Find the unique element in l that starts with abbrev +def expand_abbreviation(abbrev, l): + for full in l: + if full.startswith(abbrev) and len(abbrev) >= len(_shortest_unique_abbreviation(full, l)): + return full + raise ValueError("No unique abbreviation expansion") + def prompt(message): print message, "[yn] ", line = sys.stdin.readline().strip() -- cgit v1.2.3