Skip to content

Latest commit

 

History

History
181 lines (122 loc) · 5.29 KB

README.md

File metadata and controls

181 lines (122 loc) · 5.29 KB

Diffident

Gem Version Build Status Code Climate

Based on the differ gem by Pieter Vande Bruggen

Diffident is a flexible, pure-Ruby diff library, suitable for use in both command line scripts and web applications. The flexibility comes from the fact that diffs can be built at completely arbitrary levels of granularity (some common ones are built-in), and can be output in a variety of formats.

It also gives you raw access to the diff structure, should you need it, so you can build whatever you need that needs a diff.

Internals

Diffident uses the Ruby StringScan class to walk through the input and collect differances. It records additions, subtractions, changes and can merge two diffs that might additionally contain conflicts.

Installation

Bundler

Add this to your Gemfile

gem 'diffident'

and run bundle to install

bundle install

Manual

Install it via gem

sudo gem install diffident

and require it in your project

require 'diffident'

How do I use this thing?

There are a number of ways to use Diffident, depending on your situation and needs. Lets examplify:

@original = "Epic lolcat fail!"
@current  = "Epic wolfman fail!"

There are a number of built-in diff_by_* methods to choose from for standard use:

Diffident.diff_by_line(@current, @original)
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}

Diffident.diff_by_word(@current, @original)
# => Epic {"lolcat" >> "wolfman"} fail!

Diffident.diff_by_char(@current, @original)
# => Epic {+"wo"}l{-"olcat "}f{+"m"}a{+"n fa"}il!

Ok, but that doesn't quite cover my case, I have to split by "whatever".

No problem, you can call diff directly and supply your own boundary string:

Diffident.diff(@current, @original) # Implicitly by line by default
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}

Diffident.diff(@current, @original, 'i')
# => Epi{"c lolcat fa" >> "c wolfman fa"}il

Yeah, thats nice. But a simple string might not always cut it...

Well, you can supply a regex instead of your string if you have to:

Diffident.diff(@original, @current, /[a-z]i/)
# => E{"c wolfman f" >> "c lolcat f"}l!

Include a capture group if you want to keep the delimiter:

Diffident.diff(@original, @current, /([a-z]i)/)
# => Epi{"c wolfman f" >> "c lolcat f"}ail!

Ok, ok, but I don't like having to write "Diffident" everywhere.

If you would like something a little more inline you can require 'diffident/string' to get some added inline string magic:

@current.diff(@original) # Implicitly by line by default
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}

Or a lot more inline:

@current - @original # Implicitly by line by default
# => {"Epic lolcat fail!" >> "Epic wolfman fail!"}

Diffident.delimiter = ' ' # Custom string
@current - @original
# => Epic {"lolcat" >> "wolfman"} fail!

Diffident.delimiter = /[a-z]i/ # Custom regex without capture group
@original - @current
# => E{"c wolfman f" >> "c lolcat f"}l!

Diffident.delimiter = /([a-z]i)/ # Custom regex with capture group
@original - @current
# => Epi{"c wolfman f" >> "c lolcat f"}ail!

So we've pretty much got you covered.

What about output formatting?

Need a diffidentent output format? We've got a few of those too:

Diffident.format = :ascii  # Default
Diffident.format = :color
Diffident.format = :html

Diffident.format = MyCustomFormatModule

The formatter must respond to the call method that takes an instant of the Change class as an argument and returns a string.

But I don't want to change the system-wide default for only a single diff output!

Yeah, we either:

diff = @current - @original
diff.format_as(:color)

Or with your own formatter:

diff = @current - @original
diff.format_as(->(c){c.to_s})

Copyright

Know always right from wrong, and let others see your good works. - Pieter Vande Bruggen

The MIT License (MIT)

Copyright (c) 2013 my codeworks.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.