udiff 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/lib/udiff/diff.rb +14 -7
- data/lib/udiff/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e18cdaccdf54e1f73c844d4d7bd98f5bec6607ea305717d1add7841315fef145
|
|
4
|
+
data.tar.gz: bbdd0bbcd99ac0ef3f715ede13cc7d90258a0de050392a857f2503a1acedec16
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: afaac65708b052338b1eff55688cde62f8e1ff78d19fd436957c9388248582201040a80255aa89bbbfb649706c9d3f0cd42f585c35b1a8171732e03079ed8681
|
|
7
|
+
data.tar.gz: 2f56bb6d86439a5a506fafa1e8afe77ae84ffc9cd8b00b20e535156b5b0d4d4b6dc2f4ab75c3f2619e058c5b2bfbb4ea7d72bc3f1d071e473ab030a78772242a
|
data/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# udiff
|
|
2
2
|
|
|
3
3
|
[](https://github.com/winebarrel/udiff/actions/workflows/ci.yml)
|
|
4
|
+
[](https://badge.fury.io/rb/udiff)
|
|
4
5
|
|
|
5
6
|
Pure Ruby unified diff library. No external dependencies, no shelling out to `diff`.
|
|
6
7
|
|
|
@@ -58,3 +59,11 @@ Produces ANSI-colored output:
|
|
|
58
59
|
# Show 1 line of context instead of the default 3
|
|
59
60
|
puts Udiff::Diff.new(a, b, context: 1).to_s
|
|
60
61
|
```
|
|
62
|
+
|
|
63
|
+
### With diff info headers
|
|
64
|
+
|
|
65
|
+
By default, file headers (`--- a` / `+++ b`) and hunk headers (`@@ ... @@`) are not included. To include them:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
puts Udiff::Diff.new(a, b, include_diff_info: true).to_s
|
|
69
|
+
```
|
data/lib/udiff/diff.rb
CHANGED
|
@@ -12,10 +12,11 @@ module Udiff
|
|
|
12
12
|
reset: "\033[0m"
|
|
13
13
|
}.freeze
|
|
14
14
|
|
|
15
|
-
def initialize(string1, string2, context: DEFAULT_CONTEXT)
|
|
15
|
+
def initialize(string1, string2, context: DEFAULT_CONTEXT, include_diff_info: false)
|
|
16
16
|
@lines1 = split_lines(string1)
|
|
17
17
|
@lines2 = split_lines(string2)
|
|
18
18
|
@context = context
|
|
19
|
+
@include_diff_info = include_diff_info
|
|
19
20
|
end
|
|
20
21
|
|
|
21
22
|
def to_s(format = :text)
|
|
@@ -24,10 +25,12 @@ module Udiff
|
|
|
24
25
|
return "" if hunks.empty?
|
|
25
26
|
|
|
26
27
|
out = +""
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
if @include_diff_info
|
|
29
|
+
out << colorize("--- a", :header, format) << "\n"
|
|
30
|
+
out << colorize("+++ b", :header, format) << "\n"
|
|
31
|
+
end
|
|
29
32
|
hunks.each do |hunk|
|
|
30
|
-
out << format_hunk(hunk, format)
|
|
33
|
+
out << format_hunk(hunk, format, @include_diff_info)
|
|
31
34
|
end
|
|
32
35
|
out
|
|
33
36
|
end
|
|
@@ -199,7 +202,7 @@ module Udiff
|
|
|
199
202
|
"#{code}#{text}#{ANSI_COLORS[:reset]}"
|
|
200
203
|
end
|
|
201
204
|
|
|
202
|
-
def format_hunk(hunk, format)
|
|
205
|
+
def format_hunk(hunk, format, include_diff_info)
|
|
203
206
|
old_start = hunk[:old_start] + 1
|
|
204
207
|
new_start = hunk[:new_start] + 1
|
|
205
208
|
old_count = 0
|
|
@@ -222,8 +225,12 @@ module Udiff
|
|
|
222
225
|
end
|
|
223
226
|
end
|
|
224
227
|
|
|
225
|
-
|
|
226
|
-
|
|
228
|
+
if include_diff_info
|
|
229
|
+
header = colorize("@@ -#{old_start},#{old_count} +#{new_start},#{new_count} @@", :hunk_header, format)
|
|
230
|
+
"#{header}\n#{lines.join}"
|
|
231
|
+
else
|
|
232
|
+
lines.join
|
|
233
|
+
end
|
|
227
234
|
end
|
|
228
235
|
|
|
229
236
|
def ensure_newline(line)
|
data/lib/udiff/version.rb
CHANGED