-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored common diff code in shell
- Loading branch information
1 parent
2824cd4
commit 14a209e
Showing
3 changed files
with
74 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module DiffLines | ||
|
||
protected | ||
def show_diff_common(destination, content) #:nodoc: | ||
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil? | ||
actual = File.binread(destination).to_s.split("\n") | ||
content = content.to_s.split("\n") | ||
|
||
Diff::LCS.sdiff(actual, content).each do |diff| | ||
output_diff_line(diff) | ||
end | ||
else | ||
super | ||
end | ||
end | ||
|
||
def output_diff_line_common(diff) #:nodoc: | ||
case diff.action | ||
when '-' | ||
say "- #{diff.old_element.chomp}", :red, true | ||
when '+' | ||
say "+ #{diff.new_element.chomp}", :green, true | ||
when '!' | ||
say "- #{diff.old_element.chomp}", :red, true | ||
say "+ #{diff.new_element.chomp}", :green, true | ||
else | ||
say " #{diff.old_element.chomp}", nil, true | ||
end | ||
end | ||
|
||
def diff_lcs_loaded_common? #:nodoc: | ||
return true if defined?(Diff::LCS) | ||
return @diff_lcs_loaded unless @diff_lcs_loaded.nil? | ||
|
||
@diff_lcs_loaded = begin | ||
require 'diff/lcs' | ||
true | ||
rescue LoadError | ||
false | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters