Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add custom colors to mercurial command templates?

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?

like image 843
VoY Avatar asked Sep 02 '10 09:09

VoY


2 Answers

As of 2013, Mercurial has direct support for colors on templates. You can also check that on hg help templates.

You must activate the color extension on your .hgrc:

[extensions]
color =

Then add some custom labels to be used later on on the template:

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

Then use the template referencing the labels (using {label('labelname',field)} instead of {field}:

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

The example above highlights the node (revision) in yellow and the author of the commit in bold blue. As always, you can create an alias on your .hgrc:

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

Update: Tested version 2.5.4. According to the changelog, this works since version 2.5.

like image 142
Rudy Matela Avatar answered Sep 28 '22 02:09

Rudy Matela


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.

like image 29
Niall C. Avatar answered Sep 28 '22 01:09

Niall C.