找到你要的答案

Q:How can I color Mercurial branches based on their names?

Q:我怎么能色水银分支的基础上他们的名字吗?

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

我想用颜色来区分特征/舞台/发行分支。我有一个分支命名惯例。

有没有办法在颜色分支名汞分支输出根据一些正则表达式规则?

除了编写自定义脚本并为它创建别名外,还有更好的方法吗?

顺便说一下,有一个问题,一个伟大的答案如何在git做这个

answer1: 回答1:

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

默认情况下,汞的标准颜色扩展只允许基于分支状态着色。例如.:

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

(取自汞帮助颜色。)

为了指定基于正则表达式的颜色,您需要脚本和(方便)别名。下面是Ruby,因为露比的case语句使得正则表达式很容易做选择。

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

你也可以如果你已经安装使用的彩色宝石。

然后你可以添加这个你hgrc别名。例如,如果上面的脚本驻留在/路径/ color-branches.rb,做:

[alias]
colorbranches = !$HG branches $@ | ruby /path/to/color-branches.rb
mercurial  branch  mercurial-extension