comparison mercurial/templatekw.py @ 25726:b44e483726d3

templatekw: allow the caller of showlist() to specify the join() separator The keyword {latesttag} currently manually joins the list of tags using ':', which prevents a transparent switch over to a hybrid list.
author Matt Harbison <matt_harbison@yahoo.com>
date Mon, 06 Jul 2015 23:12:24 -0400
parents 4474a750413f
children b8245386ab40
comparison
equal deleted inserted replaced
25725:f5f43178bdde 25726:b44e483726d3
38 def __getattr__(self, name): 38 def __getattr__(self, name):
39 if name != 'get': 39 if name != 'get':
40 raise AttributeError(name) 40 raise AttributeError(name)
41 return getattr(self.values, name) 41 return getattr(self.values, name)
42 42
43 def showlist(name, values, plural=None, element=None, **args): 43 def showlist(name, values, plural=None, element=None, separator=' ', **args):
44 if not element: 44 if not element:
45 element = name 45 element = name
46 f = _showlist(name, values, plural, **args) 46 f = _showlist(name, values, plural, separator, **args)
47 return _hybrid(f, values, lambda x: {element: x}) 47 return _hybrid(f, values, lambda x: {element: x})
48 48
49 def _showlist(name, values, plural=None, **args): 49 def _showlist(name, values, plural=None, separator=' ', **args):
50 '''expand set of values. 50 '''expand set of values.
51 name is name of key in template map. 51 name is name of key in template map.
52 values is list of strings or dicts. 52 values is list of strings or dicts.
53 plural is plural of name, if not simply name + 's'. 53 plural is plural of name, if not simply name + 's'.
54 separator is used to join values as a string
54 55
55 expansion works like this, given name 'foo'. 56 expansion works like this, given name 'foo'.
56 57
57 if values is empty, expand 'no_foos'. 58 if values is empty, expand 'no_foos'.
58 59
59 if 'foo' not in template map, return values as a string, 60 if 'foo' not in template map, return values as a string,
60 joined by space. 61 joined by 'separator'.
61 62
62 expand 'start_foos'. 63 expand 'start_foos'.
63 64
64 for each value, expand 'foo'. if 'last_foo' in template 65 for each value, expand 'foo'. if 'last_foo' in template
65 map, expand it instead of 'foo' for last key. 66 map, expand it instead of 'foo' for last key.
75 if noname in templ: 76 if noname in templ:
76 yield templ(noname, **args) 77 yield templ(noname, **args)
77 return 78 return
78 if name not in templ: 79 if name not in templ:
79 if isinstance(values[0], str): 80 if isinstance(values[0], str):
80 yield ' '.join(values) 81 yield separator.join(values)
81 else: 82 else:
82 for v in values: 83 for v in values:
83 yield dict(v, **args) 84 yield dict(v, **args)
84 return 85 return
85 startname = 'start_' + names 86 startname = 'start_' + names