From bf7107bb864d0383028202e3f4a4228c02302961 Mon Sep 17 00:00:00 2001 From: Jan Niklas Hasse Date: Tue, 13 Nov 2018 15:15:43 +0100 Subject: Allow disabling of escape code stripping, fix #1475 Don't strip colors when CLICOLOR_FORCE is set to a non-zero value. This environment variable is also used by CMake's Make back-end. --- misc/output_test.py | 21 ++++++++++++++++----- src/build.cc | 1 - src/line_printer.cc | 4 ++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/misc/output_test.py b/misc/output_test.py index 6a5b635..878de19 100755 --- a/misc/output_test.py +++ b/misc/output_test.py @@ -11,11 +11,14 @@ import sys import tempfile import unittest -def run(build_ninja, flags='', pipe=False): - env = dict(os.environ) - if 'NINJA_STATUS' in env: - del env['NINJA_STATUS'] - env['TERM'] = '' +default_env = dict(os.environ) +if 'NINJA_STATUS' in default_env: + del default_env['NINJA_STATUS'] +if 'CLICOLOR_FORCE' in default_env: + del default_env['CLICOLOR_FORCE'] +default_env['TERM'] = '' + +def run(build_ninja, flags='', pipe=False, env=default_env): with tempfile.NamedTemporaryFile('w') as f: f.write(build_ninja) f.flush() @@ -84,5 +87,13 @@ red red ''') + # CLICOLOR_FORCE=1 can be used to disable escape code stripping. + env = default_env.copy() + env['CLICOLOR_FORCE'] = '1' + self.assertEqual(run(print_red, pipe=True, env=env), +'''[1/1] echo a +\x1b[31mred\x1b[0m +''') + if __name__ == '__main__': unittest.main() diff --git a/src/build.cc b/src/build.cc index 6b33024..ed219fd 100644 --- a/src/build.cc +++ b/src/build.cc @@ -154,7 +154,6 @@ void BuildStatus::BuildEdgeFinished(Edge* edge, // (Launching subprocesses in pseudo ttys doesn't work because there are // only a few hundred available on some systems, and ninja can launch // thousands of parallel compile commands.) - // TODO: There should be a flag to disable escape code stripping. string final_output; if (!printer_.supports_color()) final_output = StripAnsiEscapeCodes(output); diff --git a/src/line_printer.cc b/src/line_printer.cc index a3a551e..6effca6 100644 --- a/src/line_printer.cc +++ b/src/line_printer.cc @@ -42,6 +42,10 @@ LinePrinter::LinePrinter() : have_blank_line_(true), console_locked_(false) { smart_terminal_ = GetConsoleScreenBufferInfo(console_, &csbi); #endif supports_color_ = smart_terminal_; + if (!supports_color_) { + const char* clicolor_force = getenv("CLICOLOR_FORCE"); + supports_color_ = clicolor_force && string(clicolor_force) != "0"; + } } void LinePrinter::Print(string to_print, LineType type) { -- cgit v0.12