Skip to content
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
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Need a different output format? We've got a few of those too.
Differ.format = :ascii # <- Default
Differ.format = :color
Differ.format = :html
Differ.format = :patch

Differ.format = MyCustomFormatModule

Expand Down
1 change: 1 addition & 0 deletions differ.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |s|
"lib/differ/format/ascii.rb",
"lib/differ/format/color.rb",
"lib/differ/format/html.rb",
"lib/differ/format/patch.rb",
"lib/differ/string.rb",
"spec/differ/change_spec.rb",
"spec/differ/diff_spec.rb",
Expand Down
4 changes: 3 additions & 1 deletion lib/differ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'differ/format/ascii'
require 'differ/format/color'
require 'differ/format/html'
require 'differ/format/patch'

module Differ
class << self
Expand Down Expand Up @@ -49,6 +50,7 @@ def format_for(f)
when :ascii then Format::Ascii
when :color then Format::Color
when :html then Format::HTML
when :patch then Format::Patch
when nil then nil
else raise "Unknown format type #{f.inspect}"
end
Expand Down Expand Up @@ -80,4 +82,4 @@ def change(method, array, index)
@diff.same(array.shift)
end
end
end
end
28 changes: 28 additions & 0 deletions lib/differ/format/patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Differ
module Format
module Patch
class << self
def format(change)
(change.change? && as_change(change)) ||
(change.delete? && as_delete(change)) ||
(change.insert? && as_insert(change)) ||
''
end

private

def as_insert(change)
"\033[32m+ #{change.insert}\033[0m"
end

def as_delete(change)
"\033[31m- #{change.delete}\033[0m"
end

def as_change(change)
[as_delete(change), as_insert(change)].join("\n")
end
end
end
end
end
18 changes: 18 additions & 0 deletions spec/differ/format/patch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Differ::Format::Patch do
it 'should format inserts well' do
@expected = "\033[32m+ SAMPLE\033[0m"
Differ::Format::Patch.format(+'SAMPLE').should == @expected
end

it 'should format deletes well' do
@expected = "\033[31m- SAMPLE\033[0m"
Differ::Format::Patch.format(-'SAMPLE').should == @expected
end

it 'should format changes well' do
@expected = "\033[31m- THEN\033[0m\n\033[32m+ NOW\033[0m"
Differ::Format::Patch.format('THEN' >> 'NOW').should == @expected
end
end