Skip to content

winebarrel/udiff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

udiff

CI Gem Version AI Generated

Pure Ruby unified diff library. No external dependencies, no shelling out to diff.

Compatible with Diffy::Diff.new(a, b, diff: '-u').to_s.

Uses the Myers diff algorithm.

Installation

Add this line to your Gemfile:

gem "udiff"

Usage

Basic

require "udiff"

a = "foo\nbar\nbaz\n"
b = "foo\nqux\nbaz\n"

puts Udiff::Diff.new(a, b).to_s
 foo
-bar
+qux
 baz

With diff info headers

By default, file headers (--- a / +++ b) and hunk headers (@@ ... @@) are not included. To include them:

puts Udiff::Diff.new(a, b, include_diff_info: true).to_s
--- a
+++ b
@@ -1,3 +1,3 @@
 foo
-bar
+qux
 baz

Color output

puts Udiff::Diff.new(a, b).to_s(:color)

Produces ANSI-colored output:

  • Red: removed lines
  • Green: added lines
  • Cyan: hunk headers (@@...@@)
  • Gray: file headers (---/+++)

Custom context lines

# Show 1 line of context instead of the default 3
puts Udiff::Diff.new(a, b, context: 1).to_s

About

Pure Ruby unified diff library. No external dependencies, no shelling out to diff.

Topics

Resources

License

Stars

Watchers

Forks

Contributors