Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some smallish updates #9

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Differ

> As streams of text swirled before the young man's eyes, his mind swam with thoughts of many things. They would have to wait, however, as he focussed his full concentration on the shifting patterns ahead of him. A glint of light reflecting off a piece of buried code caught his eye and any hope he had was lost. For the very moment he glanced aside, the landscape became Different.
> The young man gave a small sigh and trudged onward in solemn resignation, fated to wander the desolate codebanks in perpetuity.

Differ 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.

## Installation

Add this to your gemfile if you use bundler

```ruby
gem 'differ'
```

and bundle to install

```bash
bundle install
```

or install it manually

```bash
sudo gem install differ
```

## How do I use this thing?

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

```ruby
require 'differ'

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

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

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

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

Differ.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:

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

Differ.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:

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

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

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

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

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

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

Or a lot more inline:

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

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

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

Differ.separator = /([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 different output format? We've got a few of those too:

```ruby
Differ.format = :ascii # Default
Differ.format = :color
Differ.format = :html

Differ.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:

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

Or with your own formatter:

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

## Copyright

Copyright (c) 2009 Pieter Vande Bruggen.

(The GIFT License, v1)

Permission is hereby granted to use this software and/or its source code for
whatever purpose you should choose. Seriously, go nuts. Use it for your personal
RSS feed reader, your wildly profitable social network, or your mission to Mars.

I don't care, it's yours. Change the name on it if you want -- in fact, if you
start significantly changing what it does, I'd rather you did! Make it your own
little work of art, complete with a stylish flowing signature in the corner. All
I really did was give you the canvas. And my blessing.

> Know always right from wrong, and let others see your good works.
99 changes: 0 additions & 99 deletions README.rdoc

This file was deleted.

38 changes: 0 additions & 38 deletions Rakefile

This file was deleted.

4 changes: 0 additions & 4 deletions VERSION.yml

This file was deleted.

80 changes: 19 additions & 61 deletions differ.gemspec
Original file line number Diff line number Diff line change
@@ -1,63 +1,21 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'differ/version'

Gem::Specification.new do |s|
s.name = %q{differ}
s.version = "0.1.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Pieter van de Bruggen"]
s.date = %q{2011-02-17}
s.email = %q{pvande@gmail.com}
s.extra_rdoc_files = [
"README.rdoc"
]
s.files = [
"README.rdoc",
"Rakefile",
"VERSION.yml",
"differ.gemspec",
"lib/differ.rb",
"lib/differ/change.rb",
"lib/differ/diff.rb",
"lib/differ/format/ascii.rb",
"lib/differ/format/color.rb",
"lib/differ/format/html.rb",
"lib/differ/string.rb",
"spec/differ/change_spec.rb",
"spec/differ/diff_spec.rb",
"spec/differ/format/ascii_spec.rb",
"spec/differ/format/color_spec.rb",
"spec/differ/format/html_spec.rb",
"spec/differ/string_spec.rb",
"spec/differ_spec.rb",
"spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/pvande/differ}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{A simple gem for generating string diffs}
s.test_files = [
"spec/differ/change_spec.rb",
"spec/differ/diff_spec.rb",
"spec/differ/format/ascii_spec.rb",
"spec/differ/format/color_spec.rb",
"spec/differ/format/html_spec.rb",
"spec/differ/string_spec.rb",
"spec/differ_spec.rb",
"spec/spec_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else
end
end
Gem::Specification.new do |spec|
spec.name = "differ"
spec.version = Differ::VERSION
spec.authors = ["Pieter van de Bruggen", "Jonas Schubert Erlandsson"]
spec.date = %q{2011-02-17}
spec.email = ["pvande@gmail.com", "jonas.schubert.erlandsson@my-codeworks.com"]
spec.description = "A simple gem for generating string diffs"
spec.summary = "A simple gem for generating string diffs"
spec.homepage = "http://github.com/pvande/differ"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
end
Loading