Disable "desc" for N characters in the hg output log using templates

I am trying to create my own template for hg log

with a part that displays the first N (for example 72) characters of the first line. Based on this answer to another question I got so far:

hg log --template '{desc|strip|firstline}\n'

      

Now I am trying to limit this bit to a certain number of characters. However, the corresponding template usage documents do not give search results for "substring", "left", etc. I have tried several things including

hg log --template '{desc|strip|firstline|strip|50}\n'

      

and just for testing as well

hg log --template '{desc|strip|50}\n'

      

but they give an error:

hg: parsing error: unkown function '50'

I would venture to suggest that what I want is possible, but I just can't seem to find the appropriate syntax. How do I create a template that outputs the first line of the commit message, up to a maximum of N characters?

+3


source to share


1 answer


You can use regular expressions and function for this sub()

. For example:

hg log --template '{sub("^(.{0,50})(.|\n)*","\\1",desc)}\n'

      

The function sub()

takes three arguments, a pattern, a replacement, and a string to be processed. In this example, we use a group that captures 0 to 50 characters (except newline), followed by another pattern that overlaps the others (so that it gets discarded). The replacement string just shows the first group and we use it desc

as input. Just change 50

in the above example to the width you want.



If you don't want the descriptions to be cut off with the middle word, you can also use fill()

in conjunction with firstline

. Here it fill()

breaks the input into lines, then we use the first line of the output. Example:

hg log --template '{fill(desc,"50")|firstline}\n'

      

Note that words with a dash in them may still be truncated, as it fill()

considers the position after the dash to be a valid choice for the line.

+4


source







All Articles