Skip to content

Commit 901b27f

Browse files
Aiden Scandellawincent
Aiden Scandella
authored andcommitted
Detect terminal support before attempting colorization
Fixes #3. Change-Id: Icd4539fa91d1f12fb44427f38886aeea5ae5d9a4 Reviewed-on: https://gerrit.causes.com/23501 Tested-by: jenkins <jenkins@causes.com> Reviewed-by: Greg Hurrell <greg@causes.com>
1 parent fcb430e commit 901b27f

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
0.1.7
22

3+
* Only colorize output when logging to a TTY
34
* Fix crashing --list-templates flag
45

56
0.1.6

lib/overcommit/logger.rb

+19-7
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,44 @@ module Overcommit
55
class Logger
66
include Singleton
77

8+
attr_accessor :output
9+
810
def partial(*args)
9-
print *args
11+
out.print *args
1012
end
1113

1214
def log(*args)
13-
puts *args
15+
out.puts *args
1416
end
1517

1618
def bold(str)
17-
log "\033[1;37m#{str}\033[0m"
19+
color('1;37', str)
1820
end
1921

2022
def error(str)
21-
log "\033[31m#{str}\033[0m"
23+
color(31, str)
2224
end
2325

2426
def success(str)
25-
log "\033[32m#{str}\033[0m"
27+
color(32, str)
2628
end
2729

2830
def warning(str)
29-
log "\033[33m#{str}\033[0m"
31+
color(33, str)
3032
end
3133

3234
def notice(str)
33-
log "\033[1;33m#{str}\033[0m"
35+
color('1;33', str)
36+
end
37+
38+
def out
39+
self.output ||= $stdout
40+
end
41+
42+
private
43+
44+
def color(code, str)
45+
log(out.isatty ? "\033[#{code}m#{str}\033[0m" : str)
3446
end
3547
end
3648
end

spec/spec_helper.rb

+1-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,4 @@
88

99
def exit(*args) ; end
1010

11-
# Silence output to STDOUT
12-
class Overcommit::Logger
13-
def log(*args)
14-
end
15-
16-
def partial(*args)
17-
end
18-
end
11+
Overcommit::Logger.instance.output = StringIO.new

0 commit comments

Comments
 (0)