[英]colorized diffs with Rugged?
I'm trying to colorize the output of a patch. 我正在尝试着色补丁的输出。 Setting the color.diff
config (via my .gitconfig) doesn't seem to do it. 设置color.diff
配置(通过我的.gitconfig)似乎没有这样做。
repo = Rugged::Repository.new('/some/path')
repo.config = Rugged::Config.new("#{ENV['HOME']}/.gitconfig")
log.info repo.config['color.diff']
INFO color.diff: always INFO color.diff:总是
And I'm doing the following to show unstaged changes: 我正在执行以下操作以显示未分级的更改:
repo.index.diff.each do |patch|
puts patch
end
Can I get a prettier colorized diff? 我可以获得更漂亮的彩色差异吗?
Here's how I did it with the colorize gem: 以下是我使用colorize gem进行的操作:
def diff
diff = @repo.index
.diff
.each_patch
.to_a
diff.each do |patch|
patch.to_s.split("\n").each do |line|
puts colorize_diff(line)
end
end
end
def colorize_diff(line)
color =
case line[0, 1]
when "+"
:green
when "-"
:red
when "@"
:cyan
end
color ? line.send(color) : line
end
color.diff
is an option for the git user-facing tool to put colours on the terminal. color.diff
是git面向用户工具的一个选项,用于在终端上放置颜色。 There is no equivalent for rugged/libgit2, as they do not handle the user interface or print to the terminal but instead produce the data. 坚固/ libgit2没有等价物,因为它们不处理用户界面或打印到终端而是生成数据。
How to generate colour on a terminal (or other device) is its own complex issue that requires its own libraries and workarounds for common problems and it lies completely outside of rugged/libgit2's scope. 如何在终端(或其他设备)上生成颜色是它自己的复杂问题,需要自己的库和常见问题的解决方法,它完全不在rugged / libgit2的范围之内。
I would recommend looking in https://rubygems.org for a gem which knows how to handle the terminals you're interested in. 我建议在https://rubygems.org上查找一个知道如何处理您感兴趣的终端的gem。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.