From dc011fd1a03bbe555803072adb0e5a98b137ab3f Mon Sep 17 00:00:00 2001 From: Stuart Campbell Date: Thu, 24 Aug 2017 22:24:58 -0700 Subject: [PATCH] Optionally disable colorized output via env Colorized output is now disabled if the environment variable `OVERCOMMIT_COLOR` is present with the value `0`. Fixes #508. --- lib/overcommit/logger.rb | 6 +++++- spec/overcommit/logger_spec.rb | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/overcommit/logger.rb b/lib/overcommit/logger.rb index f726e617..dfedb3e4 100644 --- a/lib/overcommit/logger.rb +++ b/lib/overcommit/logger.rb @@ -78,7 +78,11 @@ def bold_warning(*args) # @param partial [true,false] whether to omit a newline def color(code, str, partial = false) send(partial ? :partial : :log, - @out.tty? ? "\033[#{code}m#{str}\033[0m" : str) + colorize? ? "\033[#{code}m#{str}\033[0m" : str) + end + + def colorize? + @out.tty? && ENV.fetch('OVERCOMMIT_COLOR', '1') != '0' end end end diff --git a/spec/overcommit/logger_spec.rb b/spec/overcommit/logger_spec.rb index 35c935bf..df0a4338 100644 --- a/spec/overcommit/logger_spec.rb +++ b/spec/overcommit/logger_spec.rb @@ -80,6 +80,23 @@ output.should_not include "\033" end end + + context 'when colorization is disabled' do + before do + io.stub(:tty?).and_return(true) + end + + around do |example| + Overcommit::Utils.with_environment 'OVERCOMMIT_COLOR' => '0' do + example.run + end + end + + it 'omits the color escape sequence' do + subject + output.should_not include "\033" + end + end end describe '#debug' do