简体   繁体   English

使用模板切断hg日志输出中N个字符的“desc”

[英]Cut off the “desc” at N characters in hg log output with templates

I'm trying to create a custom template for hg log with a part that shows the first N (eg 72) characters of the first line. 我正在尝试为hg log创建一个自定义模板,其中一个部分显示第一行的前N个(例如72个)字符。 Based off this answer to a different question I've gotten so far: 根据我到目前为止得到的另一个问题的答案

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

Now I'm trying to limit that bit to a certain number of characters. 现在我试图将该位限制为一定数量的字符。 However, the corresponding template usage docs yields no search results for "substring", "left", etc. I've tried a few things, including 但是, 相应的模板使用文档不会产生“substring”,“left”等搜索结果。我尝试过一些事情,包括

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

and just for testing also 而且只是为了测试

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

but they give an error: 但他们给出了一个错误:

hg: parse error: unkown function '50' hg:解析错误:未知函数'50'

I'd hazard a guess that what I want is possible, but I just seem unable to find the appropriate syntax. 我猜测我想要的是可能的,但我似乎无法找到合适的语法。 How can I construct a template that outputs the first line of a commit message, up to a maximum of N characters? 如何构造一个输出提交消息第一行的模板,最多可以包含N个字符?

You can use regular expressions and the sub() function to accomplish that. 您可以使用正则表达式和sub()函数来实现它。 For example: 例如:

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

The sub() function takes three arguments, the pattern, the replacement, and the string to be processed. sub()函数接受三个参数,模式,替换和要处理的字符串。 In this example, we use a group that captures anywhere from 0 to 50 characters (except newline), followed by another pattern that slurps up the rest (so that it gets discarded). 在这个例子中,我们使用一个组来捕获0到50个字符(除了换行符)之外的任何地方,然后是另一个模式,其中包含其余的(以便它被丢弃)。 The replacement string simply shows the first group, and we use desc as the input. 替换字符串只显示第一组,我们使用desc作为输入。 Simply change the 50 in the example above with the width you actually want. 只需使用您实际需要的宽度更改上例中的50

If you don't want descriptions cut off mid-word, you can also use fill() in conjunction with firstline . 如果您不希望中间词的描述被截断,您也可以将fill()firstline结合使用。 Here, fill() will break the input into lines, then we use the first line of the output. 这里, fill()会将输入分解为行,然后我们使用输出的第一行。 Example: 例:

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

Note that words with dashes in them may still be cut off, since fill() considers the position after a dash to be a valid choice for a linebreak. 请注意,其中包含短划线的单词可能仍会被截断,因为fill()会将短划线后的位置视为换行符的有效选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM