我可以为mercurial命令模板添加自定义颜色吗?

时间:2010-09-02 09:22:24

标签: formatting mercurial

我想使用hg log的自定义模板,如下所示:

hg log --template '{node|short} {desc} [{date|age} by {author}]\'n --color=always

这个默认的终端颜色不是很易读,所以例如我想让节点变为红色和desc绿色。我怎样才能做到这一点?在git中我可以像这样定义这种格式:

git log --pretty=format:'%Cred%h%Creset %Cgreen%s%Creset [%ar by %an]'

在mercurial中有类似的事情吗?

2 个答案:

答案 0 :(得分:26)

截至2013年,Mercurial直接支持colors on templates。您还可以在 hg帮助模板上查看。

您必须在 .hgrc

上激活颜色扩展名
[extensions]
color =

然后添加一些自定义标签,以便稍后在模板上使用:

[color]
custom.rev = yellow
custom.author = bold

然后使用引用标签的模板(使用 {label('labelname',field)} 代替 {field}

hg log --template "{label('custom.rev',node|short)}  {desc}  [{date|age} by {label('custom.author',author)}]\n"

上面的示例以黄色突出显示节点(修订版),以粗体蓝色突出显示提交的作者。与往常一样,您可以在.hgrc上创建别名:

[alias]
customlog = log --template "{label('custom.rev',node|short)}  {desc}  [{date|age} by {label('custom.author',author)}]\n"

更新:测试版本2.5.4。 根据{{​​3}},changelog

答案 1 :(得分:12)

AFAIK,在Mercurial中无法直接执行此操作,但如果您使用的是Unix-y系统,则可以使用ANSI escape codes来控制颜色。例如:

hg log --template "\x1B[31m{node|short} \x1B[32m{desc}\x1B[0m\n"

将为您提供红色的node和绿色的desc

在Windows命令提示符下,您必须启用ColorExtension,并且代码是color命令的参数(命令提示符中为help color),因此等效项为:

hg log --template "\x1B[4m{node|short} \x1B[0;2m{desc}"

注意:在第二个转义序列中,0将重置文本颜色,2将其设置为绿色。如果没有0,您似乎会得到一个包含颜色代码或颜色代码,在这种情况下会是黄色。