difftastic 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/difftastic/ansi.rb +23 -0
- data/lib/difftastic/differ.rb +49 -10
- data/lib/difftastic/version.rb +1 -1
- data/lib/difftastic.rb +2 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0a07e5dd20bb2cd12ad616d4939db9db3398c77eae8c859c17a8ecc269b07a0
|
4
|
+
data.tar.gz: ed659c1c84fd0acb0f5798046d26ebb14ca07d16892b769414d76eeef0ac782a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc61ad28fe6a5981cf5a5fd15007655a8e1fddf85b937d1fbeb203197581530c42a0bd67f2bd173be9f3a455c5e2ebd31deffdab99a380d1dc5e62fd4868674c
|
7
|
+
data.tar.gz: 311f6717f04bc4edcbc2310bb77be7eb9b25cfda213cd7dc328fc6d44838642dde76653ecea6c9363eca1181bc6ce2989ed5a971b427748f96d8481fe300d0d4
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Difftastic::ANSI
|
4
|
+
RED = "\e[91;1m"
|
5
|
+
GREEN = "\e[92;1m"
|
6
|
+
RESET = "\e[0m"
|
7
|
+
|
8
|
+
def self.green(string = "")
|
9
|
+
"#{GREEN}#{string}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.red(string = "")
|
13
|
+
"#{RED}#{string}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.reset(string = "")
|
17
|
+
"#{RESET}#{string}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.strip_formatting(string)
|
21
|
+
string.to_s.gsub(/\e\[[0-9;]*m/, "")
|
22
|
+
end
|
23
|
+
end
|
data/lib/difftastic/differ.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Difftastic::Differ
|
4
|
-
def initialize(background: nil, color: nil, syntax_highlight: nil, context: nil, tab_width: nil, parse_error_limit: nil, underline_highlights: true)
|
4
|
+
def initialize(background: nil, color: nil, syntax_highlight: nil, context: nil, tab_width: nil, parse_error_limit: nil, underline_highlights: true, left_label: nil, right_label: nil)
|
5
5
|
@show_paths = false
|
6
6
|
@background = background => :dark | :light | nil
|
7
7
|
@color = color => :always | :never | :auto | nil
|
@@ -10,6 +10,8 @@ class Difftastic::Differ
|
|
10
10
|
@tab_width = tab_width => Integer | nil
|
11
11
|
@parse_error_limit = parse_error_limit => Integer | nil
|
12
12
|
@underline_highlights = underline_highlights => true | false
|
13
|
+
@left_label = left_label => String | nil
|
14
|
+
@right_label = right_label => String | nil
|
13
15
|
end
|
14
16
|
|
15
17
|
def diff_objects(old, new)
|
@@ -297,17 +299,54 @@ class Difftastic::Differ
|
|
297
299
|
result = result.byteslice(new_line_index, result.bytesize - new_line_index)
|
298
300
|
end
|
299
301
|
|
300
|
-
if @
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
302
|
+
if @left_label || @right_label
|
303
|
+
# Get the first content line to calculate offset
|
304
|
+
offset_line = @show_paths ? 1 : 0
|
305
|
+
first_line = result.split("\n")[offset_line]
|
306
|
+
|
307
|
+
# Calculate padding needed between labels
|
308
|
+
offset = right_label_offset(first_line)
|
309
|
+
|
310
|
+
left_part = if @left_label
|
311
|
+
Difftastic::ANSI.red(@left_label.to_s.ljust(offset))
|
312
|
+
else
|
313
|
+
" " * offset
|
314
|
+
end
|
315
|
+
|
316
|
+
right_part = if @right_label
|
317
|
+
Difftastic::ANSI.green(@right_label.to_s)
|
318
|
+
else
|
319
|
+
""
|
320
|
+
end
|
321
|
+
|
322
|
+
# Insert formatted labels at the top
|
323
|
+
result = "\n#{left_part}#{right_part}#{Difftastic::ANSI.reset}\n#{result}"
|
309
324
|
end
|
310
325
|
|
326
|
+
# Removed due to inconsistencies in the original output. Need to improve the pattern matching.
|
327
|
+
# if @underline_highlights
|
328
|
+
# result.gsub!(/\e\[([0-9;]*)m/) {
|
329
|
+
# codes = $1
|
330
|
+
# if codes =~ /9[12];1|1;9[12]/ # Matches 91;1, 92;1, 1;91, or 1;92
|
331
|
+
# "\e[#{codes};4m"
|
332
|
+
# else
|
333
|
+
# "\e[#{codes}m"
|
334
|
+
# end
|
335
|
+
# }
|
336
|
+
# end
|
337
|
+
|
311
338
|
result
|
312
339
|
end
|
340
|
+
|
341
|
+
private
|
342
|
+
|
343
|
+
def right_label_offset(line)
|
344
|
+
stripped_line = ::Difftastic::ANSI.strip_formatting(line)
|
345
|
+
_lhs, rhs = stripped_line.split(/\s{#{@tab_width},}/, 2)
|
346
|
+
|
347
|
+
offset = (stripped_line.index("#{' ' * @tab_width}#{rhs}") || 0) + @tab_width
|
348
|
+
minimum_offset = 29
|
349
|
+
|
350
|
+
[minimum_offset, offset].max
|
351
|
+
end
|
313
352
|
end
|
data/lib/difftastic/version.rb
CHANGED
data/lib/difftastic.rb
CHANGED
@@ -4,6 +4,7 @@ require "difftastic/version"
|
|
4
4
|
require "tempfile"
|
5
5
|
|
6
6
|
module Difftastic
|
7
|
+
autoload :ANSI, "difftastic/ansi"
|
7
8
|
autoload :Differ, "difftastic/differ"
|
8
9
|
autoload :Upstream, "difftastic/upstream"
|
9
10
|
|
@@ -39,7 +40,7 @@ module Difftastic
|
|
39
40
|
MESSAGE
|
40
41
|
end
|
41
42
|
|
42
|
-
exe_file = Dir.glob(File.expand_path(File.join(exe_path, "
|
43
|
+
exe_file = Dir.glob(File.expand_path(File.join(exe_path, "**", "difft"))).find do |f|
|
43
44
|
Gem::Platform.match_gem?(Gem::Platform.new(File.basename(File.dirname(f))), GEM_NAME)
|
44
45
|
end
|
45
46
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: difftastic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Drapper
|
@@ -20,6 +20,7 @@ files:
|
|
20
20
|
- README.md
|
21
21
|
- exe/difft
|
22
22
|
- lib/difftastic.rb
|
23
|
+
- lib/difftastic/ansi.rb
|
23
24
|
- lib/difftastic/differ.rb
|
24
25
|
- lib/difftastic/upstream.rb
|
25
26
|
- lib/difftastic/version.rb
|