Sha256: 72fefab5f11a1e42ee2029991539f8f58bdbc27e5665a719f6b2c9361bcbeb1f

Contents?: true

Size: 1.3 KB

Versions: 10

Compression:

Stored size: 1.3 KB

Contents

#!/usr/bin/env ruby

require 'diff'

def loadfile(filename)
  lines = nil
  File.open(filename, "r") { |f|
    lines = f.readlines
  }
  return lines
end

def diffrange(a, b)
  if (a == b)
    "#{a}"
  else
    "#{a},#{b}"
  end
end

class Diff
  def to_diff(io = $defout)
    offset = 0
    @diffs.each { |b|
      first = b[0][1]
      length = b.length
      action = b[0][0]
      addcount = 0
      remcount = 0
      b.each { |l| 
        if l[0] == "+"
          addcount += 1
        elsif l[0] == "-"
          remcount += 1
        end
      }
      if addcount == 0
        puts "#{diffrange(first+1, first+remcount)}d#{first+offset}"
      elsif remcount == 0
        puts "#{first-offset}a#{diffrange(first+1, first+addcount)}"
      else
        puts "#{diffrange(first+1, first+remcount)}c#{diffrange(first+offset+1, first+offset+addcount)}"
      end
      lastdel = (b[0][0] == "-")
      b.each { |l|
        if l[0] == "-"
          offset -= 1
          print "< "
        elsif l[0] == "+"
          offset += 1
	  if lastdel
	    lastdel = false
            puts "---"
          end
          print "> "
        end
	print l[2]
      }
    }
  end
end

if $0 == __FILE__

  file1 = ARGV.shift
  file2 = ARGV.shift

  ary1 = loadfile file1
  ary2 = loadfile file2

  diff = Diff.new(ary1, ary2)
  diff.to_diff

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
diff-0.3.6 lib/unixdiff.rb
diff-0.3.5 lib/unixdiff.rb
diff-0.3.4.4 lib/unixdiff.rb
diff-0.3.4.3 lib/unixdiff.rb
diff-0.3.4.2 lib/unixdiff.rb
diff-0.3.4.1 lib/unixdiff.rb
diff-0.3.4 lib/unixdiff.rb
diff-0.3.3 lib/unixdiff.rb
diff-0.3.2 lib/unixdiff.rb
diff-0.3.1 lib/unixdiff.rb