b6989b5b27ef

Add some utility methods for parsing filters.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 24 Nov 2009 19:34:12 -0500
parents 2a8eea4463c3
children 7dec3992e6fc
branches/tags (none)
files prompt.py

Changes

--- a/prompt.py	Thu Nov 19 22:51:34 2009 -0500
+++ b/prompt.py	Tue Nov 24 19:34:12 2009 -0500
@@ -21,6 +21,8 @@
 CACHE_PATH = ".hg/prompt/cache"
 CACHE_TIMEOUT = timedelta(minutes=15)
 
+FILTER_ARG = re.compile(r'\|.+\((.*)\)')
+
 def _cache_remote(repo, kind):
     cache = path.join(repo.root, CACHE_PATH, kind)
     c_tmp = cache + '.temp'
@@ -38,6 +40,24 @@
     return ("%s" + out + "%s") % (g[0][:-1] if g[0] else '',
                                   g[1][1:]  if g[1] else '')    
 
+def _get_filter(name, g):
+    '''Return the filter with the given name, or None if it was not used.'''
+    matching_filters = filter(lambda s: s and s.startswith('|%s' % name), g)
+    if not matching_filters:
+        return None
+    
+    # Later filters will override earlier ones, for now.
+    f = matching_filters[-1]
+    
+    return f
+
+def _get_filter_arg(f):
+    args = FILTER_ARG.match(f).groups()
+    if args:
+        return args[0]
+    else:
+        return None
+
 def prompt(ui, repo, fs='', **opts):
     '''get repository information for use in a shell prompt