From 5755ec31eb0bc2134d9c228c8a0edbe2c3e3c9b5 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 8 Dec 2022 10:52:19 +0200 Subject: Add --[no]diag-color options (infrastructure only) --- bdep/bdep.cxx | 71 +++++++++++++++++++++++++++++++++++++++++++------------- bdep/common.cli | 12 ++++++++++ bdep/sync.cxx | 1 + bdep/utility.cxx | 3 ++- bdep/utility.hxx | 12 ++++++++-- bdep/utility.txx | 16 +++++++++++++ 6 files changed, 96 insertions(+), 19 deletions(-) diff --git a/bdep/bdep.cxx b/bdep/bdep.cxx index 3061650..73c1302 100644 --- a/bdep/bdep.cxx +++ b/bdep/bdep.cxx @@ -6,6 +6,7 @@ #endif #include +#include // getenv() #include // strcmp() #include #include // set_terminate(), terminate_handler @@ -281,31 +282,47 @@ init (const common_options& co, // Verify common options. // - // Also merge the --progress/--no-progress options, overriding a less - // specific flag with a more specific. + // Also merge the --*/--no-* options, overriding a less specific flag with + // a more specific. // - optional progress; - auto merge_progress = [&progress] - (const O& o, - const default_options_entry* e = nullptr) + optional progress, diag_color; + auto merge_no = [&progress, &diag_color] ( + const O& o, + const default_options_entry* e = nullptr) { - if (o.progress () && o.no_progress ()) { - diag_record dr; - (e != nullptr ? dr << fail (e->file) : dr << fail) + if (o.progress () && o.no_progress ()) + { + diag_record dr; + (e != nullptr ? dr << fail (e->file) : dr << fail) << "both --progress and --no-progress specified"; + } + + if (o.progress ()) + progress = true; + else if (o.no_progress ()) + progress = false; } - if (o.progress ()) - progress = true; - else if (o.no_progress ()) - progress = false; + { + if (o.diag_color () && o.no_diag_color ()) + { + diag_record dr; + (e != nullptr ? dr << fail (e->file) : dr << fail) + << "both --diag-color and --no-diag-color specified"; + } + + if (o.diag_color ()) + diag_color = true; + else if (o.no_diag_color ()) + diag_color = false; + } }; for (const default_options_entry& e: dos) - merge_progress (e.options, &e); + merge_no (e.options, &e); - merge_progress (o); + merge_no (o); o = merge_options (dos, o); @@ -314,6 +331,12 @@ init (const common_options& co, o.progress (*progress); o.no_progress (!*progress); } + + if (diag_color) + { + o.diag_color (*diag_color); + o.no_diag_color (!*diag_color); + } } catch (const invalid_argument& e) { @@ -368,7 +391,23 @@ try default_terminate = set_terminate (custom_terminate); - stderr_term = fdterm (stderr_fd ()); + if (fdterm (stderr_fd ())) + { + stderr_term = std::getenv ("TERM"); + + stderr_term_color = +#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. + // + false +#else + // This test was lifted from GCC (Emacs shell sets TERM=dumb). + // + *stderr_term != nullptr && strcmp (*stderr_term, "dumb") != 0 +#endif + ; + } argv0 = argv[0]; exec_dir = path (argv0).directory (); diff --git a/bdep/common.cli b/bdep/common.cli index ed7a901..a90794b 100644 --- a/bdep/common.cli +++ b/bdep/common.cli @@ -122,6 +122,18 @@ namespace bdep network transfers, building, etc." } + bool --diag-color + { + "Use color in diagnostics. If printing to a terminal the color is used + by default provided the terminal is not dumb. Use \cb{--no-diag-color} + to suppress." + } + + bool --no-diag-color + { + "Don't use color in diagnostics." + } + path --bpkg { "", diff --git a/bdep/sync.cxx b/bdep/sync.cxx index 8140426..0cda5af 100644 --- a/bdep/sync.cxx +++ b/bdep/sync.cxx @@ -1861,6 +1861,7 @@ namespace bdep << " run '" << argv0 << "' sync --hook=1 " << "--verbose $build.verbosity " << "($build.progress == [null] ? : $build.progress ? --progress : --no-progress) " << + "($build.diag_color == [null] ? : $build.diag_color ? --diag-color : --no-diag-color) " << "--config \"$out_root\"" << endl << "}" << endl; diff --git a/bdep/utility.cxx b/bdep/utility.cxx index 01456eb..3d97090 100644 --- a/bdep/utility.cxx +++ b/bdep/utility.cxx @@ -80,7 +80,8 @@ namespace bdep } } - bool stderr_term; + optional stderr_term = nullopt; + bool stderr_term_color = false; dir_path current_directory () diff --git a/bdep/utility.hxx b/bdep/utility.hxx index f5b0412..c7d4666 100644 --- a/bdep/utility.hxx +++ b/bdep/utility.hxx @@ -143,9 +143,17 @@ namespace bdep return move (normalize (r, what)); } - // Progress. + // Diagnostics. // - extern bool stderr_term; // True if stderr is a terminal. + // If stderr is not a terminal, then the value is absent (so can be used as + // bool). Otherwise, it is the value of the TERM environment variable (which + // can be NULL). + // + extern optional stderr_term; + + // True if the color can be used on the stderr terminal. + // + extern bool stderr_term_color; // Filesystem. // diff --git a/bdep/utility.txx b/bdep/utility.txx index 100ee07..a7d060d 100644 --- a/bdep/utility.txx +++ b/bdep/utility.txx @@ -121,6 +121,14 @@ namespace bdep ops.push_back ("--no-progress"); } + // Forward our --[no]diag-color options. + // + if (co.diag_color ()) + ops.push_back ("--diag-color"); + + if (co.no_diag_color ()) + ops.push_back ("--no-diag-color"); + // Forward our --build* options. // if (co.build_specified ()) @@ -236,6 +244,14 @@ namespace bdep ops.push_back ("--no-progress"); } + // Forward our --[no]diag-color options. + // + if (co.diag_color ()) + ops.push_back ("--diag-color"); + + if (co.no_diag_color ()) + ops.push_back ("--no-diag-color"); + return process_start_callback ( [v] (const char* const args[], size_t n) { -- cgit v1.1