Skip to content

Commit f3d5d19

Browse files
committed
Merge PR #327
2 parents 2824cd4 + 2453257 commit f3d5d19

File tree

3 files changed

+55
-90
lines changed

3 files changed

+55
-90
lines changed

lib/thor/shell/color.rb

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
require_relative "basic"
2+
require_relative "lcs_diff"
23

34
class Thor
45
module Shell
56
# Inherit from Thor::Shell::Basic and add set_color behavior. Check
67
# Thor::Shell::Basic to see all available methods.
78
#
89
class Color < Basic
10+
include LCSDiff
11+
912
# Embed in a String to clear all previous ANSI sequences.
1013
CLEAR = "\e[0m"
1114
# The start of an ANSI bold sequence.
@@ -107,51 +110,6 @@ def are_colors_supported?
107110
def are_colors_disabled?
108111
!ENV['NO_COLOR'].nil? && !ENV['NO_COLOR'].empty?
109112
end
110-
111-
# Overwrite show_diff to show diff with colors if Diff::LCS is
112-
# available.
113-
#
114-
def show_diff(destination, content) #:nodoc:
115-
if diff_lcs_loaded? && ENV["THOR_DIFF"].nil? && ENV["RAILS_DIFF"].nil?
116-
actual = File.binread(destination).to_s.split("\n")
117-
content = content.to_s.split("\n")
118-
119-
Diff::LCS.sdiff(actual, content).each do |diff|
120-
output_diff_line(diff)
121-
end
122-
else
123-
super
124-
end
125-
end
126-
127-
def output_diff_line(diff) #:nodoc:
128-
case diff.action
129-
when "-"
130-
say "- #{diff.old_element.chomp}", :red, true
131-
when "+"
132-
say "+ #{diff.new_element.chomp}", :green, true
133-
when "!"
134-
say "- #{diff.old_element.chomp}", :red, true
135-
say "+ #{diff.new_element.chomp}", :green, true
136-
else
137-
say " #{diff.old_element.chomp}", nil, true
138-
end
139-
end
140-
141-
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
142-
# for diff.
143-
#
144-
def diff_lcs_loaded? #:nodoc:
145-
return true if defined?(Diff::LCS)
146-
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
147-
148-
@diff_lcs_loaded = begin
149-
require "diff/lcs"
150-
true
151-
rescue LoadError
152-
false
153-
end
154-
end
155113
end
156114
end
157115
end

lib/thor/shell/html.rb

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
require_relative "basic"
2+
require_relative "lcs_diff"
23

34
class Thor
45
module Shell
56
# Inherit from Thor::Shell::Basic and add set_color behavior. Check
67
# Thor::Shell::Basic to see all available methods.
78
#
89
class HTML < Basic
10+
include LCSDiff
11+
912
# The start of an HTML bold sequence.
1013
BOLD = "font-weight: bold"
1114

@@ -76,51 +79,6 @@ def ask(statement, color = nil)
7679
def can_display_colors?
7780
true
7881
end
79-
80-
# Overwrite show_diff to show diff with colors if Diff::LCS is
81-
# available.
82-
#
83-
def show_diff(destination, content) #:nodoc:
84-
if diff_lcs_loaded? && ENV["THOR_DIFF"].nil? && ENV["RAILS_DIFF"].nil?
85-
actual = File.binread(destination).to_s.split("\n")
86-
content = content.to_s.split("\n")
87-
88-
Diff::LCS.sdiff(actual, content).each do |diff|
89-
output_diff_line(diff)
90-
end
91-
else
92-
super
93-
end
94-
end
95-
96-
def output_diff_line(diff) #:nodoc:
97-
case diff.action
98-
when "-"
99-
say "- #{diff.old_element.chomp}", :red, true
100-
when "+"
101-
say "+ #{diff.new_element.chomp}", :green, true
102-
when "!"
103-
say "- #{diff.old_element.chomp}", :red, true
104-
say "+ #{diff.new_element.chomp}", :green, true
105-
else
106-
say " #{diff.old_element.chomp}", nil, true
107-
end
108-
end
109-
110-
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
111-
# for diff.
112-
#
113-
def diff_lcs_loaded? #:nodoc:
114-
return true if defined?(Diff::LCS)
115-
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
116-
117-
@diff_lcs_loaded = begin
118-
require "diff/lcs"
119-
true
120-
rescue LoadError
121-
false
122-
end
123-
end
12482
end
12583
end
12684
end

lib/thor/shell/lcs_diff.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module LCSDiff
2+
protected
3+
4+
# Overwrite show_diff to show diff with colors if Diff::LCS is
5+
# available.
6+
def show_diff(destination, content) #:nodoc:
7+
if diff_lcs_loaded? && ENV["THOR_DIFF"].nil? && ENV["RAILS_DIFF"].nil?
8+
actual = File.binread(destination).to_s.split("\n")
9+
content = content.to_s.split("\n")
10+
11+
Diff::LCS.sdiff(actual, content).each do |diff|
12+
output_diff_line(diff)
13+
end
14+
else
15+
super
16+
end
17+
end
18+
19+
private
20+
21+
def output_diff_line(diff) #:nodoc:
22+
case diff.action
23+
when "-"
24+
say "- #{diff.old_element.chomp}", :red, true
25+
when "+"
26+
say "+ #{diff.new_element.chomp}", :green, true
27+
when "!"
28+
say "- #{diff.old_element.chomp}", :red, true
29+
say "+ #{diff.new_element.chomp}", :green, true
30+
else
31+
say " #{diff.old_element.chomp}", nil, true
32+
end
33+
end
34+
35+
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
36+
# for diff.
37+
def diff_lcs_loaded? #:nodoc:
38+
return true if defined?(Diff::LCS)
39+
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
40+
41+
@diff_lcs_loaded = begin
42+
require "diff/lcs"
43+
true
44+
rescue LoadError
45+
false
46+
end
47+
end
48+
49+
end

0 commit comments

Comments
 (0)