0

I want to distinguish feature/stage/release branches by color. I have a branch naming convention.

Is there a way to color branch names in the output of hg branches according to some regexp-based rules?

Is there a better way to do this other than writing a custom script and creating an alias for it?

By the way, there is a question and a great answer on how to do this in git

Community
  • 1
  • 1
Luboš Turek
  • 6,273
  • 9
  • 40
  • 50

1 Answers1

4

By default, Mercurial's standard color extension only allows colorization based on branch status. E.g.:

[extensions]
color=
[color]
branches.active = none
branches.closed = black bold
branches.current = green
branches.inactive = none

(Taken from hg help color.)

In order to specify colors based on regular expressions, you'll need a script and (for convenience) an alias. The following is in Ruby, because Ruby's case statement makes it fairly easy to do selection by regular expression.

class String
  def colorize(code) "\033[#{code}m#{self}\033[0m" end
  def colorize_bold(code) "\033[#{code};1m#{self}\033[0m" end

  def black() colorize(30) end
  def red() colorize(31) end
  def green() colorize(32) end
  def yellow() colorize(33) end
  def blue() colorize(34) end 
  def magenta() colorize(35) end
  def cyan() colorize(36) end
  def white() colorize(37) end
  def gray() colorize_bold(30) end
  def bold_red() colorize_bold(31) end
  def bold_green() colorize_bold(32) end
  def bold_yellow() colorize_bold(33) end
  def bold_blue() colorize_bold(34) end 
  def bold_magenta() colorize_bold(35) end
  def bold_cyan() colorize_bold(36) end
  def bold_white() colorize_bold(37) end
end

for line in ARGF do
  case line
  when /^foo/
    print line.bold_magenta
  when /^bar/
    print line.yellow
  else
    print line.gray
  end
end

You can also use the colorize gem if you already have it installed.

You can then add this as an alias to your .hgrc. For example, if the above script resides in /path/to/color-branches.rb, do:

[alias]
colorbranches = !$HG branches $@ | ruby /path/to/color-branches.rb
Reimer Behrends
  • 8,600
  • 15
  • 19