From 494ed3b964ba76178e5bb15ea31d74b446b891f8 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 13 May 2008 23:08:25 -0400 Subject: [PATCH] - New color management integrated. --- src/Table.cpp | 170 +++++++++---------------------------- src/Table.h | 8 +- src/color.cpp | 230 ++++++++++++++++++++++++++++++++++++++++++++------ src/color.h | 25 ++++-- src/parse.cpp | 53 +++++++++++- 5 files changed, 319 insertions(+), 167 deletions(-) diff --git a/src/Table.cpp b/src/Table.cpp index c209f4f8a..6a46cec6a 100644 --- a/src/Table.cpp +++ b/src/Table.cpp @@ -45,20 +45,20 @@ Table::~Table () //////////////////////////////////////////////////////////////////////////////// void Table::setTableColor (Text::color fg, Text::color bg) { - mFg["table"] = fg; - mBg["table"] = bg; + mFg["table"] = Text::colorName (fg); + mBg["table"] = Text::colorName (bg); } //////////////////////////////////////////////////////////////////////////////// void Table::setTableFg (Text::color c) { - mFg["table"] = c; + mFg["table"] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// void Table::setTableBg (Text::color c) { - mBg["table"] = c; + mBg["table"] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// @@ -97,8 +97,8 @@ void Table::setColumnColor (int column, Text::color fg, Text::color bg) { char id[12]; sprintf (id, "col:%d", column); - mFg[id] = fg; - mBg[id] = bg; + mFg[id] = Text::colorName (fg); + mBg[id] = Text::colorName (bg); } //////////////////////////////////////////////////////////////////////////////// @@ -106,7 +106,7 @@ void Table::setColumnFg (int column, Text::color c) { char id[12]; sprintf (id, "col:%d", column); - mFg[id] = c; + mFg[id] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// @@ -114,7 +114,7 @@ void Table::setColumnBg (int column, Text::color c) { char id[12]; sprintf (id, "col:%d", column); - mBg[id] = c; + mBg[id] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// @@ -174,8 +174,8 @@ void Table::setRowColor (const int row, const Text::color fg, const Text::color { char id[12]; sprintf (id, "row:%d", row); - mFg[id] = fg; - mBg[id] = bg; + mFg[id] = Text::colorName (fg); + mBg[id] = Text::colorName (bg); } //////////////////////////////////////////////////////////////////////////////// @@ -183,7 +183,7 @@ void Table::setRowFg (const int row, const Text::color c) { char id[12]; sprintf (id, "row:%d", row); - mFg[id] = c; + mFg[id] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// @@ -191,7 +191,7 @@ void Table::setRowBg (const int row, const Text::color c) { char id[12]; sprintf (id, "row:%d", row); - mBg[id] = c; + mBg[id] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// @@ -284,8 +284,8 @@ void Table::setCellColor (const int row, const int col, const Text::color fg, co { char id[24]; sprintf (id, "cell:%d,%d", row, col); - mFg[id] = fg; - mBg[id] = bg; + mFg[id] = Text::colorName (fg); + mBg[id] = Text::colorName (bg); } //////////////////////////////////////////////////////////////////////////////// @@ -293,7 +293,7 @@ void Table::setCellFg (const int row, const int col, const Text::color c) { char id[24]; sprintf (id, "cell:%d,%d", row, col); - mFg[id] = c; + mFg[id] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// @@ -301,7 +301,7 @@ void Table::setCellBg (const int row, const int col, const Text::color c) { char id[24]; sprintf (id, "cell:%d,%d", row, col); - mBg[id] = c; + mBg[id] = Text::colorName (c); } //////////////////////////////////////////////////////////////////////////////// @@ -320,20 +320,20 @@ Text::color Table::getFg (const int row, const int col) char idCell[24]; sprintf (idCell, "cell:%d,%d", row, col); if (mFg.find (idCell) != mFg.end ()) - return mFg[idCell]; + return Text::colorCode (mFg[idCell]); char idRow[12]; sprintf (idRow, "row:%d", row); if (mFg.find (idRow) != mFg.end ()) - return mFg[idRow]; + return Text::colorCode (mFg[idRow]); char idCol[12]; sprintf (idCol, "col:%d", col); if (mFg.find (idCol) != mFg.end ()) - return mFg[idCol]; + return Text::colorCode (mFg[idCol]); if (mFg.find ("table") != mFg.end ()) - return mFg["table"]; + return Text::colorCode (mFg["table"]); return Text::nocolor; } @@ -344,8 +344,8 @@ Text::color Table::getHeaderFg (int col) char idCol[12]; sprintf (idCol, "col:%d", col); - return mFg.find (idCol) != mFg.end () ? mFg[idCol] - : mFg.find ("table") != mFg.end () ? mFg["table"] + return mFg.find (idCol) != mFg.end () ? Text::colorCode (mFg[idCol]) + : mFg.find ("table") != mFg.end () ? Text::colorCode (mFg["table"]) : Text::nocolor; } @@ -355,20 +355,20 @@ Text::color Table::getBg (const int row, const int col) char idCell[24]; sprintf (idCell, "cell:%d,%d", row, col); if (mBg.find (idCell) != mBg.end ()) - return mBg[idCell]; + return Text::colorCode (mBg[idCell]); char idRow[12]; sprintf (idRow, "row:%d", row); if (mBg.find (idRow) != mBg.end ()) - return mBg[idRow]; + return Text::colorCode (mBg[idRow]); char idCol[12]; sprintf (idCol, "col:%d", col); if (mBg.find (idCol) != mBg.end ()) - return mBg[idCol]; + return Text::colorCode (mBg[idCol]); if (mBg.find ("table") != mBg.end ()) - return mBg["table"]; + return Text::colorCode (mBg["table"]); return Text::nocolor; } @@ -379,19 +379,19 @@ Text::color Table::getHeaderBg (int col) char idCol[12]; sprintf (idCol, "col:%d", col); - return mBg.find (idCol) != mBg.end () ? mBg[idCol] - : mBg.find ("table") != mBg.end () ? mBg["table"] + return mBg.find (idCol) != mBg.end () ? Text::colorCode (mBg[idCol]) + : mBg.find ("table") != mBg.end () ? Text::colorCode (mBg["table"]) : Text::nocolor; } //////////////////////////////////////////////////////////////////////////////// -Text::attr Table::getHeaderUnderline (int col) +Text::color Table::getHeaderUnderline (int col) { char idCol[12]; sprintf (idCol, "col:%d", col); return mUnderline.find (idCol) != mUnderline.end () ? Text::underline - : Text::normal; + : Text::nocolor; } //////////////////////////////////////////////////////////////////////////////// @@ -522,42 +522,17 @@ const std::string Table::formatHeader ( { assert (width > 0); - Text::color fg = getHeaderFg (col); - Text::color bg = getHeaderBg (col); - std::string data = mColumns[col]; - Text::attr decoration = getHeaderUnderline (col); + Text::color fg = getHeaderFg (col); + Text::color bg = getHeaderBg (col); + std::string data = mColumns[col]; + Text::color decoration = getHeaderUnderline (col); - std::string colorOn = ""; std::string pad = ""; std::string intraPad = ""; std::string preJust = ""; std::string attrOn = ""; std::string attrOff = ""; std::string postJust = ""; - std::string colorOff = ""; - - if (fg != Text::nocolor) - { - char c[12]; - sprintf (c, "\033[%dm", fg + 29); - colorOn += c; - } - - if (bg != Text::nocolor) - { - char c[12]; - sprintf (c, "\033[%dm", bg + 39); - colorOn += c; - } - - if (colorOn != "") - colorOff += "\033[0m"; - - if (decoration == Text::underline) - { - attrOn = "\033[4m"; - attrOff = "\033[0m"; - } for (int i = 0; i < padding; ++i) pad += " "; @@ -572,16 +547,11 @@ const std::string Table::formatHeader ( for (int i = 0; i < getIntraPadding (); ++i) intraPad += " "; - return colorOn + - attrOn + - pad + - preJust + - data + - postJust + - pad + - attrOff + - intraPad + - colorOff; + return Text::colorize ( + fg, bg, + Text::colorize ( + decoration, Text::nocolor, + pad + preJust+ data + postJust + pad) + intraPad); } //////////////////////////////////////////////////////////////////////////////// @@ -600,27 +570,8 @@ void Table::formatCell ( just justification = getJustification (row, col); std::string data = getCell (row, col); - std::string colorOn = ""; std::string pad = ""; std::string intraPad = ""; - std::string colorOff = ""; - - if (fg != Text::nocolor) - { - char c[12]; - sprintf (c, "\033[%dm", fg + 29); - colorOn += c; - } - - if (bg != Text::nocolor) - { - char c[12]; - sprintf (c, "\033[%dm", bg + 39); - colorOn += c; - } - - if (fg != Text::nocolor || bg != Text::nocolor) - colorOff += "\033[0m"; for (int i = 0; i < padding; ++i) pad += " "; @@ -660,14 +611,7 @@ void Table::formatCell ( } lines.push_back ( - colorOn + - pad + - preJust + - chunks[chunk] + - postJust + - pad + - intraPad + - colorOff); + Text::colorize (fg, bg, pad + preJust + chunks[chunk] + postJust + pad + intraPad)); } // The blank is used to vertically pad cells that have blank lines. @@ -675,11 +619,7 @@ void Table::formatCell ( for (int i = 0; i < width; ++i) pad += " "; - blank = - colorOn + - pad + - intraPad + - colorOff; + blank = Text::colorize (fg, bg, pad + intraPad); } //////////////////////////////////////////////////////////////////////////////// @@ -696,29 +636,10 @@ const std::string Table::formatCell ( just justification = getJustification (row, col); std::string data = getCell (row, col); - std::string colorOn = ""; std::string pad = ""; std::string intraPad = ""; std::string preJust = ""; std::string postJust = ""; - std::string colorOff = ""; - - if (fg != Text::nocolor) - { - char c[12]; - sprintf (c, "\033[%dm", fg + 29); - colorOn += c; - } - - if (bg != Text::nocolor) - { - char c[12]; - sprintf (c, "\033[%dm", bg + 39); - colorOn += c; - } - - if (fg != Text::nocolor || bg != Text::nocolor) - colorOff += "\033[0m"; for (int i = 0; i < padding; ++i) pad += " "; @@ -749,14 +670,7 @@ const std::string Table::formatCell ( for (int i = 0; i < getIntraPadding (); ++i) intraPad += " "; - return colorOn + - pad + - preJust + - data + - postJust + - pad + - intraPad + - colorOff; + return Text::colorize (fg, bg, pad + preJust + data + postJust + pad + intraPad); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Table.h b/src/Table.h index 045630b4d..cffa47b45 100644 --- a/src/Table.h +++ b/src/Table.h @@ -70,7 +70,7 @@ private: Text::color getHeaderFg (const int); Text::color getBg (const int, const int); Text::color getHeaderBg (const int); - Text::attr getHeaderUnderline (const int); + Text::color getHeaderUnderline (const int); int getPadding (const int); int getIntraPadding (); void calculateColumnWidths (); @@ -87,9 +87,9 @@ private: std::vector mColumns; int mRows; int mIntraPadding; - std::map mFg; - std::map mBg; - std::map mUnderline; + std::map mFg; + std::map mBg; + std::map mUnderline; // Padding... int mTablePadding; diff --git a/src/color.cpp b/src/color.cpp index 075586001..c08b229d5 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -7,58 +7,234 @@ #include "color.h" //////////////////////////////////////////////////////////////////////////////// -std::string Text::colorName (Text::color c) +namespace Text +{ + +std::string colorName (color c) { switch (c) { - case black: return "black"; - case red: return "red"; - case green: return "green"; - case yellow: return "yellow"; - case blue: return "blue"; - case magenta: return "magenta"; - case cyan: return "cyan"; - case white: return "white"; - case nocolor: return ""; + case nocolor: return ""; + case off: return "off"; + + case bold: return "bold"; + case underline: return "underline"; + case bold_underline: return "bold_underline"; + + case black: return "black"; + case red: return "red"; + case green: return "green"; + case yellow: return "yellow"; + case blue: return "blue"; + case magenta: return "magenta"; + case cyan: return "cyan"; + case white: return "white"; + + case bold_black: return "bold_black"; + case bold_red: return "bold_red"; + case bold_green: return "bold_green"; + case bold_yellow: return "bold_yellow"; + case bold_blue: return "bold_blue"; + case bold_magenta: return "bold_magenta"; + case bold_cyan: return "bold_cyan"; + case bold_white: return "bold_white"; + + case underline_black: return "underline_black"; + case underline_red: return "underline_red"; + case underline_green: return "underline_green"; + case underline_yellow: return "underline_yellow"; + case underline_blue: return "underline_blue"; + case underline_magenta: return "underline_magenta"; + case underline_cyan: return "underline_cyan"; + case underline_white: return "underline_white"; + + case bold_underline_black: return "bold_underline_black"; + case bold_underline_red: return "bold_underline_red"; + case bold_underline_green: return "bold_underline_green"; + case bold_underline_yellow: return "bold_underline_yellow"; + case bold_underline_blue: return "bold_underline_blue"; + case bold_underline_magenta: return "bold_underline_magenta"; + case bold_underline_cyan: return "bold_underline_cyan"; + case bold_underline_white: return "bold_underline_white"; + + case on_black: return "on_black"; + case on_red: return "on_red"; + case on_green: return "on_green"; + case on_yellow: return "on_yellow"; + case on_blue: return "on_blue"; + case on_magenta: return "on_magenta"; + case on_cyan: return "on_cyan"; + case on_white: return "on_white"; + + case on_bright_black: return "on_bright_black"; + case on_bright_red: return "on_bright_red"; + case on_bright_green: return "on_bright_green"; + case on_bright_yellow: return "on_bright_yellow"; + case on_bright_blue: return "on_bright_blue"; + case on_bright_magenta: return "on_bright_magenta"; + case on_bright_cyan: return "on_bright_cyan"; + case on_bright_white: return "on_bright_white"; + + default: throw "Unknown Text::color value"; } return ""; } //////////////////////////////////////////////////////////////////////////////// -Text::color Text::colorCode (const std::string& c) +color colorCode (const std::string& c) { - if (c == "black") return black; - if (c == "red") return red; - if (c == "green") return green; - if (c == "yellow") return yellow; - if (c == "blue") return blue; - if (c == "magenta") return magenta; - if (c == "cyan") return cyan; - if (c == "white") return white; + if (c == "off") return off; + else if (c == "bold") return bold; + else if (c == "underline") return underline; + else if (c == "bold_underline") return bold_underline; + + else if (c == "black") return black; + else if (c == "red") return red; + else if (c == "green") return green; + else if (c == "yellow") return yellow; + else if (c == "blue") return blue; + else if (c == "magenta") return magenta; + else if (c == "cyan") return cyan; + else if (c == "white") return white; + + else if (c == "bold_black") return bold_black; + else if (c == "bold_red") return bold_red; + else if (c == "bold_green") return bold_green; + else if (c == "bold_yellow") return bold_yellow; + else if (c == "bold_blue") return bold_blue; + else if (c == "bold_magenta") return bold_magenta; + else if (c == "bold_cyan") return bold_cyan; + else if (c == "bold_white") return bold_white; + + else if (c == "underline_black") return underline_black; + else if (c == "underline_red") return underline_red; + else if (c == "underline_green") return underline_green; + else if (c == "underline_yellow") return underline_yellow; + else if (c == "underline_blue") return underline_blue; + else if (c == "underline_magenta") return underline_magenta; + else if (c == "underline_cyan") return underline_cyan; + else if (c == "underline_white") return underline_white; + + else if (c == "bold_underline_black") return bold_underline_black; + else if (c == "bold_underline_red") return bold_underline_red; + else if (c == "bold_underline_green") return bold_underline_green; + else if (c == "bold_underline_yellow") return bold_underline_yellow; + else if (c == "bold_underline_blue") return bold_underline_blue; + else if (c == "bold_underline_magenta") return bold_underline_magenta; + else if (c == "bold_underline_cyan") return bold_underline_cyan; + else if (c == "bold_underline_white") return bold_underline_white; + + else if (c == "on_black") return on_black; + else if (c == "on_red") return on_red; + else if (c == "on_green") return on_green; + else if (c == "on_yellow") return on_yellow; + else if (c == "on_blue") return on_blue; + else if (c == "on_magenta") return on_magenta; + else if (c == "on_cyan") return on_cyan; + else if (c == "on_white") return on_white; + + else if (c == "on_bright_black") return on_bright_black; + else if (c == "on_bright_red") return on_bright_red; + else if (c == "on_bright_green") return on_bright_green; + else if (c == "on_bright_yellow") return on_bright_yellow; + else if (c == "on_bright_blue") return on_bright_blue; + else if (c == "on_bright_magenta") return on_bright_magenta; + else if (c == "on_bright_cyan") return on_bright_cyan; + else if (c == "on_bright_white") return on_bright_white; return nocolor; } //////////////////////////////////////////////////////////////////////////////// -std::string Text::attrName (Text::attr a) +std::string decode (color c) { - switch (a) + switch (c) { - case underline: return "underline"; - case normal: return ""; + case nocolor: return ""; + case off: return "\033[0m"; + + case bold: return "\033[1m"; + case underline: return "\033[4m"; + case bold_underline: return "\033[1;4m"; + + case black: return "\033[30m"; + case red: return "\033[31m"; + case green: return "\033[32m"; + case yellow: return "\033[33m"; + case blue: return "\033[34m"; + case magenta: return "\033[35m"; + case cyan: return "\033[36m"; + case white: return "\033[37m"; + + case bold_black: return "\033[90m"; + case bold_red: return "\033[91m"; + case bold_green: return "\033[92m"; + case bold_yellow: return "\033[93m"; + case bold_blue: return "\033[94m"; + case bold_magenta: return "\033[95m"; + case bold_cyan: return "\033[96m"; + case bold_white: return "\033[97m"; + + case underline_black: return "\033[4;30m"; + case underline_red: return "\033[4;31m"; + case underline_green: return "\033[4;32m"; + case underline_yellow: return "\033[4;33m"; + case underline_blue: return "\033[4;34m"; + case underline_magenta: return "\033[4;35m"; + case underline_cyan: return "\033[4;36m"; + case underline_white: return "\033[4;37m"; + + case bold_underline_black: return "\033[1;4;30m"; + case bold_underline_red: return "\033[1;4;31m"; + case bold_underline_green: return "\033[1;4;32m"; + case bold_underline_yellow: return "\033[1;4;33m"; + case bold_underline_blue: return "\033[1;4;34m"; + case bold_underline_magenta: return "\033[1;4;35m"; + case bold_underline_cyan: return "\033[1;4;36m"; + case bold_underline_white: return "\033[1;4;37m"; + + case on_black: return "\033[40m"; + case on_red: return "\033[41m"; + case on_green: return "\033[42m"; + case on_yellow: return "\033[43m"; + case on_blue: return "\033[44m"; + case on_magenta: return "\033[45m"; + case on_cyan: return "\033[46m"; + case on_white: return "\033[47m"; + + case on_bright_black: return "\033[100m"; + case on_bright_red: return "\033[101m"; + case on_bright_green: return "\033[102m"; + case on_bright_yellow: return "\033[103m"; + case on_bright_blue: return "\033[104m"; + case on_bright_magenta: return "\033[105m"; + case on_bright_cyan: return "\033[106m"; + case on_bright_white: return "\033[107m"; + + default: throw "Unknown Text::color value"; } return ""; } //////////////////////////////////////////////////////////////////////////////// -Text::attr Text::attrCode (const std::string& a) +std::string colorize (color fg, color bg, const std::string& input) { - if (a == "underline") return underline; - - return normal; + return decode (fg) + decode (bg) + input + decode (off); } //////////////////////////////////////////////////////////////////////////////// +std::string colorize (color fg, color bg) +{ + return decode (fg) + decode (bg); +} +//////////////////////////////////////////////////////////////////////////////// +std::string colorize () +{ + return decode (off); +} + +//////////////////////////////////////////////////////////////////////////////// +} diff --git a/src/color.h b/src/color.h index ce8ab7111..6ab7ef545 100644 --- a/src/color.h +++ b/src/color.h @@ -8,14 +8,27 @@ namespace Text { - enum color {nocolor = 0, black, red, green, yellow, blue, magenta, cyan, white}; - enum attr {normal = 0, underline}; + enum color + { + nocolor = 0, + off, + bold, underline, bold_underline, + black, bold_black, underline_black, bold_underline_black, on_black, on_bright_black, + red, bold_red, underline_red, bold_underline_red, on_red, on_bright_red, + green, bold_green, underline_green, bold_underline_green, on_green, on_bright_green, + yellow, bold_yellow, underline_yellow, bold_underline_yellow, on_yellow, on_bright_yellow, + blue, bold_blue, underline_blue, bold_underline_blue, on_blue, on_bright_blue, + magenta, bold_magenta, underline_magenta, bold_underline_magenta, on_magenta, on_bright_magenta, + cyan, bold_cyan, underline_cyan, bold_underline_cyan, on_cyan, on_bright_cyan, + white, bold_white, underline_white, bold_underline_white, on_white, on_bright_white + }; - std::string colorName (Text::color); - Text::color colorCode (const std::string&); + std::string colorName (color); + color colorCode (const std::string&); - std::string attrName (Text::attr); - Text::attr attrCode (const std::string&); + std::string colorize (color, color, const std::string& string); + std::string colorize (color, color); + std::string colorize (); } #endif diff --git a/src/parse.cpp b/src/parse.cpp index 480457448..b7c0f5bee 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -15,14 +15,63 @@ //////////////////////////////////////////////////////////////////////////////// static char* colors[] = { + "bold", + "underline", + "bold_underline", + "black", - "blue", "red", "green", + "yellow", + "blue", "magenta", "cyan", - "yellow", "white", + + "bold_black", + "bold_red", + "bold_green", + "bold_yellow", + "bold_blue", + "bold_magenta", + "bold_cyan", + "bold_white", + + "underline_black", + "underline_red", + "underline_green", + "underline_yellow", + "underline_blue", + "underline_magenta", + "underline_cyan", + "underline_white", + + "bold_underline_black", + "bold_underline_red", + "bold_underline_green", + "bold_underline_yellow", + "bold_underline_blue", + "bold_underline_magenta", + "bold_underline_cyan", + "bold_underline_white", + + "on_black", + "on_red", + "on_green", + "on_yellow", + "on_blue", + "on_magenta", + "on_cyan", + "on_white", + + "on_bright_black", + "on_bright_red", + "on_bright_green", + "on_bright_yellow", + "on_bright_blue", + "on_bright_magenta", + "on_bright_cyan", + "on_bright_white", "", };