File tree 3 files changed +33
-7
lines changed
3 files changed +33
-7
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ module Overcommit::Hook::PreCommit
5
5
class HtmlHint < Base
6
6
def run
7
7
result = execute ( command + applicable_files )
8
- output = strip_color_codes ( result . stdout . chomp )
8
+ output = Overcommit :: Utils . strip_color_codes ( result . stdout . chomp )
9
9
10
10
message_groups = output . split ( "\n \n " ) [ 0 ..-2 ]
11
11
message_groups . map do |group |
@@ -17,11 +17,5 @@ def run
17
17
)
18
18
end . flatten
19
19
end
20
-
21
- private
22
-
23
- def strip_color_codes ( output )
24
- output . gsub ( /\e \[ (\d +)(;\d +)*m/ , '' )
25
- end
26
20
end
27
21
end
Original file line number Diff line number Diff line change @@ -72,6 +72,16 @@ def git_dir(repo_dir = repo_root)
72
72
end
73
73
end
74
74
75
+ # Remove ANSI escape sequences from a string.
76
+ #
77
+ # This is useful for stripping colorized output from external tools.
78
+ #
79
+ # @param text [String]
80
+ # @return [String]
81
+ def strip_color_codes ( text )
82
+ text . gsub ( /\e \[ (\d +)(;\d +)*m/ , '' )
83
+ end
84
+
75
85
# Shamelessly stolen from:
76
86
# stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby
77
87
def snake_case ( str )
Original file line number Diff line number Diff line change 77
77
end
78
78
end
79
79
80
+ describe '.strip_color_codes' do
81
+ subject { described_class . strip_color_codes ( text ) }
82
+
83
+ context 'with an empty string' do
84
+ let ( :text ) { '' }
85
+
86
+ it { should == '' }
87
+ end
88
+
89
+ context 'with a string with no escape sequences' do
90
+ let ( :text ) { 'A normal string' }
91
+
92
+ it { should == text }
93
+ end
94
+
95
+ context 'with a string with escape sequences' do
96
+ let ( :text ) { "A \e [31mcolored string\e [39m" }
97
+
98
+ it { should == 'A colored string' }
99
+ end
100
+ end
101
+
80
102
describe '.snake_case' do
81
103
it 'converts camel case to underscores' do
82
104
described_class . snake_case ( 'HelloWorld' ) . should == 'hello_world'
You can’t perform that action at this time.
0 commit comments