-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-diff-syntax-highlight.rb
executable file
·230 lines (201 loc) · 5.29 KB
/
git-diff-syntax-highlight.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env ruby
# Source: https://gist.github.com/skanev/0eeb943e3111a1df55fd
# This hardcodes the path to ruby to avoid interference from rvm
#
# Usage: add the following to .gitconfig
#
# [core]
# pager = /path/to/git-diff-syntax-highlight.rb --highlight | less -F -X
# # The -F -X is optional
#
# Installing Dependencies:
#
# gem install --user term-ansicolor coderay
#
# If you are using RVM, you may wish to hardcode the first line of this script to the path of the
# ruby interpreter that was used to install those dependencies.
# (found in a path such as /home/username/.rvm/rubies/ruby-x.y.z/bin/ruby)
require 'coderay'
require 'term/ansicolor'
require 'optparse'
# --- Adjusting colors -------------------------------------------------------
RGB = Term::ANSIColor::RGBTriple
Attribute = Term::ANSIColor::Attribute
Color = Term::ANSIColor
GREEN = RGB.new(0, 256, 0)
RED = RGB.new(256, 0, 0)
GRAY = RGB.new(180, 180, 180)
ADJUSTMENTS = {}
GRADIENTS = {}
def adjust(text, target, gradient = 8)
open = adjust_seq("\e[37m", target, gradient)
begin
adjusted = text.chomp.gsub(/\e\[[0-9;]+m/) { |seq| adjust_seq(seq, target, gradient) }
rescue ArgumentError
# invalid byte sequence in UTF-8
adjusted = text.chomp
end
"#{open}#{adjusted}"
end
def adjust_seq(seq, target, level)
key = [seq, target]
ADJUSTMENTS[key] ||=
begin
escapes = []
seq[/\d+(;\d+)*/].split(';').map(&:to_i).each do |number|
case number
when 0 then escapes << ["\e[0m", gradient_to(255, target, level)]
when 1 then escapes << "\e[1m"
when 4 then escapes << "\e[4m"
when 30..37 then escapes << gradient_to(number - 30, target, level)
when 40..47 then escapes << gradient_to(number - 30, target, level, :background)
else escapes << seq
end
end
escapes.join('')
end
end
def gradient_to(num, target, level, background = false)
key = [num, target, background]
GRADIENTS[key] ||=
begin
triple = triple(num)
target = triple.gradient_to(target)[level]
html = target.html
method = background ? "on_#{html}" : html
Attribute[method].apply
end
end
def triple(num)
Attribute[num].to_rgb_triple
end
# --- Parsing git diffs ------------------------------------------------------
FORMATS = {
'Gemfile' => :ruby,
'rb' => :ruby,
'c' => :c,
'h' => :c,
'cpp' => :cpp,
'cxx' => :cpp,
'hpp' => :cpp,
'clj' => :clojure,
'css' => :css,
'erb' => :erb,
'go' => :go,
'java' => :java,
'js' => :javascript,
'json' => :json,
'php' => :php,
'html' => :php,
'inc' => :php,
'phpt' => :php,
'lua' => :lua,
'py' => :python,
'sass' => :sass,
'scss' => :scss,
'sql' => :sql,
'xml' => :xml,
'yml' => :yaml,
'yaml' => :yaml,
}
CACHED_OBJECTS = {}
def show(sha, path)
return [] if sha =~ /^0+$/
cached = CACHED_OBJECTS.delete sha
return cached if cached
format = FORMATS[path[/(\w+)$/, 1]]
code = `git show #{sha} 2> /dev/null`
# This can be a directory?
code = File.read(path) if (code.empty? and File.file? path)
code = CodeRay.scan(code, format).terminal if format
CACHED_OBJECTS[sha] = code.lines
end
def process(options)
new = nil
old = nil
remaining = nil
old_file = nil
new_file = nil
old_hash = nil
new_hash = nil
while gets
begin
stripped = $_.gsub(/(\e\[[0-9;]*m)*/, '')
rescue ArgumentError
# "invalid byte sequence in UTF-8"
puts $_
next
end
case stripped
when /^index (\w+)..(\w+)/
old_hash = $1
new_hash = $2
puts $_
# @@ -1,4 +1,4 @@
when /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/
old_start, old_length = $1.to_i - 1, ($2 || 1).to_i
new_start, new_length = $3.to_i - 1, ($4 || 1).to_i
remaining = old_length + new_length
puts $_
when /^\+\+\+ (.*)$/
new_file = $1.sub(/^[ab]\//, '')
old = show old_hash, old_file
new = show new_hash, new_file
puts $_
when /^--- (.*)$/
old_file = $1.sub(/^[ab]\//, '')
puts $_
when /^ /
if remaining.nil? || remaining <= 0
puts $_
next
elsif options[:highlight]
puts adjust(" #{new[new_start]}", GRAY, 1)
else
puts $_
end
remaining -= 2
old_start += 1
new_start += 1
when /^\+/
if new_start.nil?
puts $_
else
puts adjust("+#{new[new_start]}", GREEN)
new_start += 1
end
unless remaining.nil?
remaining -= 1
end
when /^-/
unless old_start.nil?
puts adjust("-#{old[old_start]}", RED)
old_start += 1
end
unless remaining.nil?
remaining -= 1
end
else
puts $_
end
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
opts.on("-h", "--[no-]highlight", "Highlight all the code") do |v|
options[:highlight] = v
end
opts.on("-s", "--[no-]coderay-colors", "Use coderay's standard terminal color scheme instead of a vim-like color scheme") do |v|
options[:coderay_colors] = v
end
end.parse!
begin
if !options[:coderay_colors]
# Custom overrides for colors
require_relative 'vim'
end
process(options)
rescue Errno::EPIPE
exit 0
end