From 92f2c4ffccf28c1b6c12f1d27e535a6670d07a1b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 16 Jan 2020 10:08:09 +0100 Subject: [PATCH] Adding console text color/attribute support (ansi) --- CMakeLists.txt | 1 + src/console.h | 83 ++++++++++++++++++++++++++++++++++++++++++++ src/console_ansi.cpp | 49 ++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 src/console.h create mode 100644 src/console_ansi.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ead58d1..13250b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ include(GNUInstallDirs) set (PROGRAM_EXE ${CMAKE_PROJECT_NAME}) set (PROGRAM_SOURCE + src/console_ansi.cpp src/string.cpp src/ec.cpp src/base58.cpp diff --git a/src/console.h b/src/console.h new file mode 100644 index 0000000..c81a5b7 --- /dev/null +++ b/src/console.h @@ -0,0 +1,83 @@ +/** + * 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. + */ +#ifndef CONSOLE_H +#define CONSOLE_H + +#include + +namespace console { + + // enum for all supported colors. + enum Color { + default_fg, + black, + white, + red, + green, + blue, + yellow, + magenta, + cyan, + + // Light colors. + light_grey, + light_red, + light_green, + light_blue, + light_yellow, + light_magenta, + light_cyan, + + // Dark colors + dark_grey + }; + + enum Attribute { + normal, + bold, + italic + }; + + // Resets all colors/attributes + std::ostream& reset(std::ostream& os); + + // Foreground color + // Defined as a class with overloaded "<<" operator so you can write: + // std::cout << fg(red) << "Text"; + class fg + { + public: + fg(Color color, Attribute attribute = normal) + : _color(color), _attr(attribute) {} + + friend std::ostream& operator<<(std::ostream& os, const fg& obj); + + protected : + Color _color; + Attribute _attr; + }; + +} // namespace console + +#endif /* CONSOLE_H */ diff --git a/src/console_ansi.cpp b/src/console_ansi.cpp new file mode 100644 index 0000000..ed37b0a --- /dev/null +++ b/src/console_ansi.cpp @@ -0,0 +1,49 @@ + +#include +#include "console.h" + +namespace console { + +std::ostream& reset(std::ostream& os) { + return os << "\033[0m"; +} + +// Foreground +std::ostream& operator<<(std::ostream& os, const fg& obj) { + + int attr; + int code; + + switch(obj._color) { + case black : code = 30; break; + case red : code = 31; break; + case green : code = 32; break; + case yellow : code = 33; break; + case blue : code = 34; break; + case magenta : code = 35; break; + case cyan : code = 36; break; + case light_grey : code = 37; break; + case dark_grey : code = 90; break; + case light_red : code = 91; break; + case light_green : code = 92; break; + case light_yellow : code = 93; break; + case light_blue : code = 94; break; + case light_magenta : code = 95; break; + case light_cyan : code = 96; break; + case white : code = 97; break; + case default_fg : default : + code = 39; + } + + switch(obj._attr) { + + case bold : attr = 1; break; + case italic : attr = 2; break; + case normal : default : + attr = 0; + } + + return os << "\033[" << attr << ";" << code << "m"; +} + +} // namespace console