diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2023-08-29 02:13:00 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2023-08-29 02:13:00 +0200 |
commit | f0b7196db8c4dd34c4b2247943e4dac44864b89c (patch) | |
tree | 2f8106ec3532a69a627fe6539b539d8bd61783b5 /libbuild2/diagnostics.cxx | |
parent | b527b31bb74ead51260f5bcf7915e6678943993e (diff) |
Add diagnostics color support on Windows (GH issue #312)
Note that currently this has to be enabled with an explicit --diag-color
option. In the future the plan is to enable it by default if supported.
Diffstat (limited to 'libbuild2/diagnostics.cxx')
-rw-r--r-- | libbuild2/diagnostics.cxx | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/libbuild2/diagnostics.cxx b/libbuild2/diagnostics.cxx index e164f10..4a46756 100644 --- a/libbuild2/diagnostics.cxx +++ b/libbuild2/diagnostics.cxx @@ -3,9 +3,10 @@ #include <libbuild2/diagnostics.hxx> -#include <cstring> // strcmp(), strchr(), memcpy() +#include <cstring> // strchr(), memcpy() #include <cstdlib> // getenv() +#include <libbutl/fdstream.hxx> // fdterm_color() #include <libbutl/process-io.hxx> #include <libbuild2/scope.hxx> @@ -30,7 +31,7 @@ namespace build2 bool diag_no_line = false; bool diag_no_column = false; - optional<const char*> stderr_term = nullopt; + bool stderr_term = false; bool stderr_term_color = false; void @@ -50,29 +51,38 @@ namespace build2 diag_color_option = c; diag_no_line = nl; diag_no_column = nc; + stderr_term = st; if (st) { - stderr_term = std::getenv ("TERM"); - - stderr_term_color = + // @@ TMP: eventually we want to enable on Windows by default. + // #ifdef _WIN32 - // For now we disable color on Windows since it's unclear if/where/how - // it is supported. Maybe one day someone will figure this out. + if (c && *c) + { +#endif + stderr_term_color = fdterm_color (stderr_fd (), !c || *c /* enable */); + + // If the user specified --diag-color on POSIX we will trust the color + // is supported (e.g., wrong TERM value, etc). // - false + if (!stderr_term_color && c && *c) + { +#ifdef _WIN32 + fail << "unable to enable diagnostics color support for stderr"; #else - // This test was lifted from GCC (Emacs shell sets TERM=dumb). - // - *stderr_term != nullptr && strcmp (*stderr_term, "dumb") != 0 + stderr_term_color = true; +#endif + } + +#ifdef _WIN32 + } + else + stderr_term_color = false; #endif - ; } else - { - stderr_term = nullopt; stderr_term_color = false; - } } // Stream verbosity. |