comparison mercurial/templatefilters.py @ 22668:13e3f07d74a3

templater: add count template filter, plus tests Previously there was no way of telling how much children or bookmarks or tags a certain changeset has in a template. It was possible to tell if a changeset has either 0 or not 0 bookmarks, but not to tell if it has 1 or 2 of them, for example. This filter, simply named count, makes it possible to count the number of items in a list or the length of a string (or, anything that python's len can count). E.g.: {children|count}, {bookmarks|count}, {file_adds|count}. Testing the filter on node hash and shortened node hash is chosen because they both have defined length. As for lists of strings - children, tags and file_adds are used, because they provide some variety and also prove that what's counted is the number of string items in the list, and not the list stringified (they are lists of non-empty, multi-character strings). Additionally, revset template function is used for testing the filter, since the combination is very flexible and will possibly be used together a lot. (The previous version of this patch had an incorrect email subject and was apparently lost - patchwork says the patch has been accepted, but it's not so. The changes between that and this patch are minimal: now the filter does not disturb the alphabetical order of function definitions and dict keys.)
author Anton Shestakov <engored@ya.ru>
date Tue, 09 Sep 2014 22:14:13 +0900
parents cf599f8a2da8
children ae5447de4c11
comparison
equal deleted inserted replaced
22667:3acc3f95548c 22668:13e3f07d74a3
63 component of the path after splitting by the path separator 63 component of the path after splitting by the path separator
64 (ignoring trailing separators). For example, "foo/bar/baz" becomes 64 (ignoring trailing separators). For example, "foo/bar/baz" becomes
65 "baz" and "foo/bar//" becomes "bar". 65 "baz" and "foo/bar//" becomes "bar".
66 """ 66 """
67 return os.path.basename(path) 67 return os.path.basename(path)
68
69 def count(i):
70 """:count: List or text. Returns the length as an integer."""
71 return len(i)
68 72
69 def datefilter(text): 73 def datefilter(text):
70 """:date: Date. Returns a date in a Unix date format, including the 74 """:date: Date. Returns a date in a Unix date format, including the
71 timezone: "Mon Sep 04 15:13:13 2006 0700". 75 timezone: "Mon Sep 04 15:13:13 2006 0700".
72 """ 76 """
364 368
365 filters = { 369 filters = {
366 "addbreaks": addbreaks, 370 "addbreaks": addbreaks,
367 "age": age, 371 "age": age,
368 "basename": basename, 372 "basename": basename,
373 "count": count,
369 "date": datefilter, 374 "date": datefilter,
370 "domain": domain, 375 "domain": domain,
371 "email": email, 376 "email": email,
372 "escape": escape, 377 "escape": escape,
373 "fill68": fill68, 378 "fill68": fill68,