且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-18 20:12:02

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

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

会给你节点以红色显示, desc 以绿色显示。



在Windows命令提示符下,必须启用 ColorExtension ,代码是color命令的参数(帮助颜色在命令提示符下),所以相当于:
$ b $ $ p $ hg log - 模板\ x1B [4m {node | short} \x1B [0; 2m {desc}



在第二个转义序列中, 0 用于重置文本颜色, 2 是将其设置为绿色。如果没有 0 ,看起来你会得到包含或颜色代码,在这种情况下,它将是黄色的。


I want to use customized template for hg log which looks like this:

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

This in default terminal color is not very readable, so for instance I would like to make node red and desc green. How can I do this? In git I can define this kind of formatting like this:

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

Is a similar thing possible in mercurial?

AFAIK, there's no way to do this directly in Mercurial, but if you're on a Unix-y system you could use ANSI escape codes to control the colors. For example:

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

will give you the node in red and the desc in green.

On the Windows command prompt, you have to enable the ColorExtension and the codes are the parameters to the color command (help color in the command prompt), so the equivalent would be:

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

Note: in the second escape sequence, the 0 is to reset the text color and the 2 is to set it to green. Without the 0, it seems you get an inclusive-or of the color codes, which in this case would be yellow.