diff --git a/dune/stuff/common/color.hh b/dune/stuff/common/color.hh index e710a9b0b26a38c09bbe3e04270a42550234c941..897d289c1c619927edd161d547a763fafba0c083 100644 --- a/dune/stuff/common/color.hh +++ b/dune/stuff/common/color.hh @@ -18,6 +18,13 @@ namespace Stuff { namespace Common { +/** + * @brief namespace to define color constants that can be + * used to print colored text in an output stream. + * + * + * @warning Some color codes might be unsupported by your terminal. + */ namespace Color { // foreground colors static const char* const black = "\033[30m"; @@ -68,11 +75,23 @@ static const char* const endunderline = "\033[24m"; static const char* const endblink = "\033[25m"; static const char* const endreverse = "\033[27m"; +/** + * @brief Chooses a color from a 256 color map for a foreground color. + * + * @param i The color number between 0 and 255. + * @returns A string describing a color code. + */ std::string color(int i) { return "\033[38;5;" + Dune::Stuff::Common::String::convertTo(i) + "m"; } +/** + * @brief Chooses a color from a 256 color map for a background color. + * + * @param i The color number between 0 and 255. + * @returns A string describing a color code. + */ std::string backcolor(int i) { return "\033[38;5;" + Dune::Stuff::Common::String::convertTo(i) + "m"; @@ -84,7 +103,13 @@ int templateColorChooser(int i) return i % 256; } -// highlight templates depending on the "template"-level +/** + * @brief Highlights templates depending on the "template"-level. + * + * @param str The string containing the template string + * @param maxlevel The maximal template-level the string is reduced to. + * @returns A colored template string. + */ std::string highlightTemplate(std::string str, int maxlevel = 10000) { if (maxlevel < 0) @@ -115,23 +140,36 @@ std::string highlightTemplate(std::string str, int maxlevel = 10000) return str; } // highlightTemplate -// highlight a string in a specified color +/** + * @brief A simple function highlighting a whole string in a specified foreground color. + * + * @param str The string you want to highlight. + * @param colornr A color number from a 256 color map between 0 and 255. + * @returns The highlighted string. + */ std::string highlightString(std::string str, int colornr = 0) { return "\033[38;5;" + Dune::Stuff::Common::String::convertTo(colornr % 256) + "m" + str + "\033[38;5;0m"; } -// highlight a string which is searched in another string in a specified color -std::string highlightSearchString(std::string str, std::string searchstr, int colornr = 0) +/** + * @brief Highlights a substring of another string in a specified color. + * + * @param str The string where you want to highlight substrings. + * @param substr The sub string you want to highlight in str. + * @param colornr A color number from a 256 color map between 0 and 255. + * @returns The highlighted string. + */ +std::string highlightSearchString(std::string str, std::string substr, int colornr = 0) { - int index = str.find(searchstr, 0); + int index = str.find(substr, 0); while (index != int(std::string::npos)) { std::string dummy = "\033[38;5;" + Dune::Stuff::Common::String::convertTo(colornr % 256) + "m"; std::string dummy2 = "\033[38;5;0m"; str.insert(index, dummy); - str.insert(index + searchstr.size() + dummy.size(), dummy2); - index = str.find(searchstr, index + dummy.size() + searchstr.size() + dummy2.size()); + str.insert(index + substr.size() + dummy.size(), dummy2); + index = str.find(substr, index + dummy.size() + substr.size() + dummy2.size()); } return str; } // highlightSearchString diff --git a/dune/stuff/test/common_color.cc b/dune/stuff/test/common_color.cc index 3cad1bb239ce26031525c1a9dfe82aea7692f4b9..f3bdeee5e95b1ffd0d7a3b0b879fcda6cc662ae5 100644 --- a/dune/stuff/test/common_color.cc +++ b/dune/stuff/test/common_color.cc @@ -4,6 +4,10 @@ using namespace Dune::Stuff::Common::Color; int main(int, char**) { + std::cout << "Testing some color codes for this terminal." << std::endl; + std::cout << "WARNING: This test will succeed although some of the color codes" << std::endl; + std::cout << "are not supported by your terminal!" << std::endl; + std::cout << "(Sometimes 'blink', 'reverse' or 'italic' are not supported.)" << std::endl; std::cout << underline << "a simple 'hello world': " << endunderline << "hello world" << std::endl; std::cout << underline << "a colored 'hello world': " << endunderline << highlightString("hello world", 1) << std::endl; @@ -27,4 +31,5 @@ int main(int, char**) for (int i = 0; i < 256; ++i) std::cout << highlightString("hello world - ", i); std::cout << std::endl; + return 0; } // main