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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19a27ebf3406c05f249c900c3c2ffad8473ab48c39a768e14246d7d5f4c8ebd4
4
- data.tar.gz: 5550f665f2bfd0533e60be40fef3f764fe72244e85c1cd10628f2f5bd2116890
3
+ metadata.gz: e18cdaccdf54e1f73c844d4d7bd98f5bec6607ea305717d1add7841315fef145
4
+ data.tar.gz: bbdd0bbcd99ac0ef3f715ede13cc7d90258a0de050392a857f2503a1acedec16
5
5
  SHA512:
6
- metadata.gz: 01a7163b270071880ee6f4e550b70e737f891011efff3331c8e6cbc95e051bd667da30474744240887cb3ca31bbf363f444ce82b283bad3441716e3749676ac2
7
- data.tar.gz: cfe719370e4f07846e93957be09bcbd176208009c59a49bae94ff5135788aa6ca1d1568a5e4b7d965acc1a2bc796aa22d00620d511aa16d1a681afbae6530c15
6
+ metadata.gz: afaac65708b052338b1eff55688cde62f8e1ff78d19fd436957c9388248582201040a80255aa89bbbfb649706c9d3f0cd42f585c35b1a8171732e03079ed8681
7
+ data.tar.gz: 2f56bb6d86439a5a506fafa1e8afe77ae84ffc9cd8b00b20e535156b5b0d4d4b6dc2f4ab75c3f2619e058c5b2bfbb4ea7d72bc3f1d071e473ab030a78772242a
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # udiff
2
2
 
3
3
  [![CI](https://github.com/winebarrel/udiff/actions/workflows/ci.yml/badge.svg)](https://github.com/winebarrel/udiff/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/udiff.svg)](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
- out << colorize("--- a", :header, format) << "\n"
28
- out << colorize("+++ b", :header, format) << "\n"
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
- header = colorize("@@ -#{old_start},#{old_count} +#{new_start},#{new_count} @@", :hunk_header, format)
226
- "#{header}\n#{lines.join}"
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Udiff
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: udiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel