diff options
author | Matthew Woehlke <matthew.woehlke@kitware.com> | 2020-04-18 16:49:51 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2020-04-24 15:21:13 (GMT) |
commit | 671fe28313b1a66ece5a9b0e7dab1bd05d83460c (patch) | |
tree | 435b3fbb3d4e85d2c8c7aa92ec816eafc7388d0d /Source | |
parent | f56a6954408343f8efe24876067b52415228aaba (diff) | |
download | CMake-671fe28313b1a66ece5a9b0e7dab1bd05d83460c.zip CMake-671fe28313b1a66ece5a9b0e7dab1bd05d83460c.tar.gz CMake-671fe28313b1a66ece5a9b0e7dab1bd05d83460c.tar.bz2 |
ccmake: Improve coloring, allow customization
Change the default color for strings from BLUE (which is nearly
illegible on any terminals using the standard color palette which has
been around since at least CGA, almost 40 years ago) to CYAN. Add
ability to customize the colors via an environment variable (inspired by
LS_COLORS and using similar syntax).
Fixes: #20596
Diffstat (limited to 'Source')
-rw-r--r-- | Source/CursesDialog/cmCursesColor.cxx | 59 | ||||
-rw-r--r-- | Source/CursesDialog/cmCursesColor.h | 3 |
2 files changed, 57 insertions, 5 deletions
diff --git a/Source/CursesDialog/cmCursesColor.cxx b/Source/CursesDialog/cmCursesColor.cxx index 371eaa1..c0b468b 100644 --- a/Source/CursesDialog/cmCursesColor.cxx +++ b/Source/CursesDialog/cmCursesColor.cxx @@ -2,6 +2,12 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmCursesColor.h" +#include <cctype> +#include <cstdlib> +#include <cstring> +#include <unordered_map> +#include <utility> + #include "cmCursesStandardIncludes.h" bool cmCursesColor::HasColors() @@ -19,11 +25,54 @@ void cmCursesColor::InitColors() if (HasColors()) { start_color(); use_default_colors(); - init_pair(cmCursesColor::BoolOff, COLOR_RED, -1); - init_pair(cmCursesColor::BoolOn, COLOR_GREEN, -1); - init_pair(cmCursesColor::String, COLOR_BLUE, -1); - init_pair(cmCursesColor::Path, COLOR_YELLOW, -1); - init_pair(cmCursesColor::Choice, COLOR_MAGENTA, -1); + init_pair(BoolOff, GetColor('N', COLOR_RED), -1); + init_pair(BoolOn, GetColor('Y', COLOR_GREEN), -1); + init_pair(String, GetColor('S', COLOR_CYAN), -1); + init_pair(Path, GetColor('P', COLOR_YELLOW), -1); + init_pair(Choice, GetColor('C', COLOR_MAGENTA), -1); } #endif } + +short cmCursesColor::GetColor(char id, short fallback) +{ + static bool initialized = false; + static std::unordered_map<char, short> env; + + if (!initialized) { + if (auto* v = getenv("CCMAKE_COLORS")) { + while (v[0] && v[1] && v[1] == '=') { + auto const n = std::toupper(*v); + + char buffer[12]; + memset(buffer, 0, sizeof(buffer)); + + if (auto* const e = strchr(v, ':')) { + if (static_cast<size_t>(e - v) > sizeof(buffer)) { + break; + } + + strncpy(buffer, v + 2, static_cast<size_t>(e - v - 2)); + v = e + 1; + } else { + auto const l = strlen(v); + if (l > sizeof(buffer)) { + break; + } + + strncpy(buffer, v + 2, l - 2); + v += l; + } + + auto const c = atoi(buffer); + if (c && c < COLORS) { + env.emplace(n, static_cast<short>(c)); + } + } + } + initialized = true; + } + + auto const iter = env.find(id); + return (iter == env.end() ? fallback : iter->second); +} diff --git a/Source/CursesDialog/cmCursesColor.h b/Source/CursesDialog/cmCursesColor.h index 94d0a11..f83265f 100644 --- a/Source/CursesDialog/cmCursesColor.h +++ b/Source/CursesDialog/cmCursesColor.h @@ -19,6 +19,9 @@ public: static bool HasColors(); static void InitColors(); + +protected: + static short GetColor(char id, short fallback); }; #endif // cmCursesColor_h |