Mercurial > hg
changeset 49494:c96ed4029fda
templates: add filter to reverse list
The filter supports only lists because for lists, it’s straightforward to
implement. Reversing text doesn’t seem very useful and is hard to implement.
Reversing the bytes would break multi-bytes encodings. Reversing the code
points would break characters consisting of multiple code points. Reversing
graphemes is non-trivial without using a library not included in the standard
library.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Thu, 15 Sep 2022 01:48:38 +0200 |
parents | 4367c46a89ee |
children | 59a72267f5ce |
files | mercurial/templatefilters.py tests/test-template-functions.t |
diffstat | 2 files changed, 23 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/templatefilters.py Wed Sep 07 14:56:45 2022 -0400 +++ b/mercurial/templatefilters.py Thu Sep 15 01:48:38 2022 +0200 @@ -390,6 +390,14 @@ return stringutil.person(author) +@templatefilter(b'reverse') +def reverse(list_): + """List. Reverses the order of list items.""" + if isinstance(list_, list): + return templateutil.hybridlist(list_[::-1], name=b'item') + raise error.ParseError(_(b'not reversible')) + + @templatefilter(b'revescape', intype=bytes) def revescape(text): """Any text. Escapes all "special" characters, except @.
--- a/tests/test-template-functions.t Wed Sep 07 14:56:45 2022 -0400 +++ b/tests/test-template-functions.t Thu Sep 15 01:48:38 2022 +0200 @@ -1718,4 +1718,19 @@ $ hg log -T "{config('templateconfig', 'knob', if(true, 'foo', 'bar'))}\n" foo +reverse filter: + + $ hg log -T "{'abc\ndef\nghi'|splitlines|reverse}\n" + ghi def abc + + $ hg log -T "{'abc'|reverse}\n" + hg: parse error: not reversible + (incompatible use of template filter 'reverse') + [10] + + $ hg log -T "{date|reverse}\n" + hg: parse error: not reversible + (template filter 'reverse' is not compatible with keyword 'date') + [10] + $ cd ..