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

VoY*_*VoY 9 formatting mercurial

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

hg log --template '{node|short} {desc} [{date|age} by {author}]\'n --color=always
Run Code Online (Sandbox Code Playgroud)

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

git log --pretty=format:'%Cred%h%Creset %Cgreen%s%Creset [%ar by %an]'
Run Code Online (Sandbox Code Playgroud)

在mercurial中类似的事情是否可能?

Rud*_*ela 27

截至2013年,Mercurial直接支持模板上的颜色.您还可以在hg帮助模板上查看.

您必须激活.hgrc上的颜色扩展名:

[extensions]
color =
Run Code Online (Sandbox Code Playgroud)

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

[color]
custom.rev = yellow
custom.author = bold
Run Code Online (Sandbox Code Playgroud)

然后使用引用标签的模板(使用{label('labelname',field)}而不是{field}:

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

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

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

更新:测试版本2.5.4.根据更新日志,这可以从版本2.5开始.

  • 要查看Mercurial自己使用的内置标签,请使用`--color = debug`运行命令; 例如`hg log -l1 --color = debug`. (3认同)

Nia*_* C. 12

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

hg log --template "\x1B[31m{node|short} \x1B[32m{desc}\x1B[0m\n"
Run Code Online (Sandbox Code Playgroud)

会给你node红色和desc绿色.

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

hg log --template "\x1B[4m{node|short} \x1B[0;2m{desc}"
Run Code Online (Sandbox Code Playgroud)

注意:在第二个转义序列中,0是重置文本颜色,2而是将其设置为绿色.没有0它,似乎你得到一个包容性或颜色代码,在这种情况下将是黄色.