From f0b7196db8c4dd34c4b2247943e4dac44864b89c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 29 Aug 2023 02:13:00 +0200 Subject: 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. --- libbuild2/diagnostics.cxx | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'libbuild2/diagnostics.cxx') 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 -#include // strcmp(), strchr(), memcpy() +#include // strchr(), memcpy() #include // getenv() +#include // fdterm_color() #include #include @@ -30,7 +31,7 @@ namespace build2 bool diag_no_line = false; bool diag_no_column = false; - optional 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. -- cgit v1.1