summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Niklas Hasse <jhasse@bixense.com>2018-11-13 15:15:43 +0100
committerJan Niklas Hasse <jhasse@bixense.com>2018-11-13 15:15:43 +0100
commitbf7107bb864d0383028202e3f4a4228c02302961 (patch)
treead4ecdd3f32ab606d5d664862a400c2f3d4641ad
parent99c5c2287b11c8bab05fb2a8cf89dc4856c708bc (diff)
downloadninja-bf7107bb864d0383028202e3f4a4228c02302961.tar.gz
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.
-rwxr-xr-xmisc/output_test.py21
-rw-r--r--src/build.cc1
-rw-r--r--src/line_printer.cc4
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) {