Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add patch format. #8

Open
wants to merge 1 commit into from

2 participants

Chad Boyd echarp
Chad Boyd

This commit adds a patch format similar to diff:

<?xml version="1.0" encoding="UTF-8"?>
<foo>
-  <bar>one</bar>
+  <bar>two</bar>
</foo>

Just need to set Differ.format = :patch.

echarp

This looks just great, although without the console colors, it would be even better ;)

Chad Boyd

I actually like the console colors, and think they are helpful. With a simple example like above, it doesn't really matter. But when you have a long many lines of text and they are littered with changes, the colors really help make the additions and deletions stand out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 21, 2012
  1. Chad Boyd

    Add patch format.

    hoverlover authored
This page is out of date. Refresh to see the latest.
1  README.rdoc
View
@@ -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
1  differ.gemspec
View
@@ -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",
4 lib/differ.rb
View
@@ -3,6 +3,7 @@
require 'differ/format/ascii'
require 'differ/format/color'
require 'differ/format/html'
+require 'differ/format/patch'
module Differ
class << self
@@ -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
@@ -80,4 +82,4 @@ def change(method, array, index)
@diff.same(array.shift)
end
end
-end
+end
28 lib/differ/format/patch.rb
View
@@ -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 spec/differ/format/patch_spec.rb
View
@@ -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
Something went wrong with that request. Please try again.