From f1f9eb5a661516a4d2a2a476033a951cec26b442 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 18 Feb 2020 14:29:53 +0100 Subject: [PATCH] console: add disable_color flag. --- CMakeLists.txt | 1 + src/console.cpp | 30 ++++++++++++++++++++++++++ src/console.h | 2 ++ src/console_ansi.cpp | 8 +++++++ src/console_win32.cpp | 50 +++++++++++++++++++++++-------------------- 5 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 src/console.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d0195d1..a143774 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ include(GNUInstallDirs) set (PROGRAM_EXE ${CMAKE_PROJECT_NAME}) set (PROGRAM_SOURCE + src/console.cpp src/string.cpp src/base58.cpp src/WIF.cpp diff --git a/src/console.cpp b/src/console.cpp new file mode 100644 index 0000000..c685541 --- /dev/null +++ b/src/console.cpp @@ -0,0 +1,30 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "console.h" + +namespace eoskeygen { namespace console { + +bool disable_color = false; + +} } // namespace eoskeygen::console diff --git a/src/console.h b/src/console.h index bc20e0e..bd7f0fd 100644 --- a/src/console.h +++ b/src/console.h @@ -30,6 +30,8 @@ namespace eoskeygen { namespace console { + extern bool disable_color; + // enum for all supported colors. enum Color { default_fg, diff --git a/src/console_ansi.cpp b/src/console_ansi.cpp index 272615a..0398d84 100644 --- a/src/console_ansi.cpp +++ b/src/console_ansi.cpp @@ -29,6 +29,10 @@ namespace eoskeygen { namespace console { std::ostream& reset(std::ostream& os) { + + if (disable_color) { + return os; + } return os << "\033[0m"; } @@ -38,6 +42,10 @@ std::ostream& operator<<(std::ostream& os, const fg& obj) { int attr; int code; + if (disable_color) { + return os; + } + switch(obj._color) { case black : code = 30; break; case red : code = 31; break; diff --git a/src/console_win32.cpp b/src/console_win32.cpp index a6c2800..24e38fa 100644 --- a/src/console_win32.cpp +++ b/src/console_win32.cpp @@ -50,8 +50,9 @@ namespace console { std::ostream& reset(std::ostream& os) { - ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), FG_DEFAULT); - + if (disable_color == false) { + ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), FG_DEFAULT); + } return os; } @@ -60,28 +61,31 @@ std::ostream& operator<<(std::ostream& os, const fg& obj) { int code; - switch(obj._color) { - case black : code = FG_BLACK; break; - case red : code = FG_RED; break; - case green : code = FG_GREEN; break; - case blue : code = FG_BLUE; break; - case yellow : code = FG_YELLOW; break; - case magenta : code = FG_MAGENTA; break; - case cyan : code = FG_CYAN; break; - case light_grey : code = FG_GREY; break; - case light_red : code = FG_LIGHTRED; break; - case light_green : code = FG_LIGHTGREEN; break; - case light_yellow : code = FG_LIGHTYELLOW; break; - case light_blue : code = FG_LIGHTBLUE; break; - case light_magenta : code = FG_LIGHTMAGENTA; break; - case light_cyan : code = FG_LIGHTCYAN; break; - case dark_grey : code = FG_DARKGREY; break; - case white : code = FG_WHITE; break; - case default_fg : default : - code = FG_DEFAULT; - } + if (disable_color == false) { - ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), code); + switch(obj._color) { + case black : code = FG_BLACK; break; + case red : code = FG_RED; break; + case green : code = FG_GREEN; break; + case blue : code = FG_BLUE; break; + case yellow : code = FG_YELLOW; break; + case magenta : code = FG_MAGENTA; break; + case cyan : code = FG_CYAN; break; + case light_grey : code = FG_GREY; break; + case light_red : code = FG_LIGHTRED; break; + case light_green : code = FG_LIGHTGREEN; break; + case light_yellow : code = FG_LIGHTYELLOW; break; + case light_blue : code = FG_LIGHTBLUE; break; + case light_magenta : code = FG_LIGHTMAGENTA; break; + case light_cyan : code = FG_LIGHTCYAN; break; + case dark_grey : code = FG_DARKGREY; break; + case white : code = FG_WHITE; break; + case default_fg : default : + code = FG_DEFAULT; + } + + ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), code); + } // WinAPI does not support text attributes in the console. // so we ignore those.