Skip to content

Commit

Permalink
refactored common diff code in shell
Browse files Browse the repository at this point in the history
  • Loading branch information
doolin authored and rafaelfranca committed May 11, 2023
1 parent 2824cd4 commit 14a209e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 78 deletions.
55 changes: 16 additions & 39 deletions lib/thor/shell/color.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
require_relative "basic"
require_relative "diff_lines"

class Thor
module Shell
# Inherit from Thor::Shell::Basic and add set_color behavior. Check
# Thor::Shell::Basic to see all available methods.
#
class Color < Basic
include DiffLines

# Embed in a String to clear all previous ANSI sequences.
CLEAR = "\e[0m"
# The start of an ANSI bold sequence.
Expand Down Expand Up @@ -108,50 +111,24 @@ def are_colors_disabled?
!ENV['NO_COLOR'].nil? && !ENV['NO_COLOR'].empty?
end

# Overwrite show_diff to show diff with colors if Diff::LCS is
# available.
#
def show_diff(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
# Overwrite show_diff to show diff with colors if Diff::LCS is
# available.
#
def show_diff(destination, content) #:nodoc:
show_diff_common(destination, content)
end
end

def output_diff_line(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
def output_diff_line(diff)
output_diff_line_common(diff)
end
end

# Check if Diff::LCS is loaded. If it is, use it to create pretty output
# for diff.
#
def diff_lcs_loaded? #: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
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
# for diff.
#
def diff_lcs_loaded? #:nodoc:
diff_lcs_loaded_common?
end
end

end
end
end
43 changes: 43 additions & 0 deletions lib/thor/shell/diff_lines.rb
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
54 changes: 15 additions & 39 deletions lib/thor/shell/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Shell
# Thor::Shell::Basic to see all available methods.
#
class HTML < Basic
include DiffLines

# The start of an HTML bold sequence.
BOLD = "font-weight: bold"

Expand Down Expand Up @@ -77,50 +79,24 @@ def can_display_colors?
true
end

# Overwrite show_diff to show diff with colors if Diff::LCS is
# available.
#
def show_diff(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
# Overwrite show_diff to show diff with colors if Diff::LCS is
# available.
#
def show_diff(destination, content) #:nodoc:
show_diff_common(destination, content)
end
end

def output_diff_line(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
def output_diff_line(diff) #:nodoc:
output_diff_line_common(diff)
end
end

# Check if Diff::LCS is loaded. If it is, use it to create pretty output
# for diff.
#
def diff_lcs_loaded? #: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
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
# for diff.
#
def diff_lcs_loaded? #:nodoc:
diff_lcs_loaded_common?
end
end

end
end
end

0 comments on commit 14a209e

Please sign in to comment.