Feature - 256-color support

- Eliminated old color implementation.
- Integration, debugging remains.
This commit is contained in:
Paul Beckingham 2009-09-22 16:42:46 -04:00
parent 37436071a0
commit 58730a48b3
4 changed files with 144 additions and 369 deletions

View file

@ -31,6 +31,7 @@
#include <algorithm> #include <algorithm>
#include "Color.h" #include "Color.h"
#include "text.h" #include "text.h"
#include "i18n.h"
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
static struct static struct
@ -41,16 +42,16 @@ static struct
int index; // offset red=3 (therefore fg=33, bg=43) int index; // offset red=3 (therefore fg=33, bg=43)
} allColors[] = } allColors[] =
{ {
// Color.h enum i18n.h English Index // Color.h enum i18n.h English Index
{ Color::nocolor, 0, "", 0}, { Color::nocolor, 0, "", 0},
{ Color::black, 0/*COLOR_BLACK*/, "black", 1}, // fg 29+0 bg 39+0 { Color::black, COLOR_BLACK, "black", 1}, // fg 29+0 bg 39+0
{ Color::red, 0/*COLOR_RED*/, "red", 2}, { Color::red, COLOR_RED, "red", 2},
{ Color::green, 0/*COLOR_GREEN*/, "green", 3}, { Color::green, COLOR_GREEN, "green", 3},
{ Color::yellow, 0/*COLOR_YELLOW*/, "yellow", 4}, { Color::yellow, COLOR_YELLOW, "yellow", 4},
{ Color::blue, 0/*COLOR_BLUE*/, "blue", 5}, { Color::blue, COLOR_BLUE, "blue", 5},
{ Color::magenta, 0/*COLOR_MAGENTA*/, "magenta", 6}, { Color::magenta, COLOR_MAGENTA, "magenta", 6},
{ Color::cyan, 0/*COLOR_CYAN*/, "cyan", 7}, { Color::cyan, COLOR_CYAN, "cyan", 7},
{ Color::white, 0/*COLOR_WHITE*/, "white", 8}, { Color::white, COLOR_WHITE, "white", 8},
}; };
@ -58,7 +59,7 @@ static struct
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Color::Color () Color::Color ()
: value (COLOR_NOFG | COLOR_NOBG) : value (_COLOR_NOFG | _COLOR_NOBG)
{ {
} }
@ -70,13 +71,13 @@ Color::Color (const Color& other)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Color::Color (unsigned int c) Color::Color (unsigned int c)
: value (COLOR_NOFG | COLOR_NOBG) : value (_COLOR_NOFG | _COLOR_NOBG)
{ {
if (!(c & COLOR_FG)) value &= ~COLOR_FG; if (!(c & _COLOR_FG)) value &= ~_COLOR_FG;
if (!(c & COLOR_BG)) value &= ~COLOR_BG; if (!(c & _COLOR_BG)) value &= ~_COLOR_BG;
value = c & (COLOR_256 | COLOR_UNDERLINE | COLOR_BOLD | COLOR_BRIGHT | value = c & (_COLOR_256 | _COLOR_UNDERLINE | _COLOR_BOLD | _COLOR_BRIGHT |
COLOR_BG | COLOR_FG); _COLOR_BG | _COLOR_FG);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -92,7 +93,7 @@ Color::Color (unsigned int c)
// colorN 0 <= N <= 255 fg 38;5;N bg 48;5;N // colorN 0 <= N <= 255 fg 38;5;N bg 48;5;N
// rgbRGB 0 <= R,G,B <= 5 fg 38;5;16 + R*36 + G*6 + B bg 48;5;16 + R*36 + G*6 + B // rgbRGB 0 <= R,G,B <= 5 fg 38;5;16 + R*36 + G*6 + B bg 48;5;16 + R*36 + G*6 + B
Color::Color (const std::string& spec) Color::Color (const std::string& spec)
: value (COLOR_NOFG | COLOR_NOBG) : value (_COLOR_NOFG | _COLOR_NOBG)
{ {
// By converting underscores to spaces, we inherently support the old "on_red" // By converting underscores to spaces, we inherently support the old "on_red"
// style of specifying background colors. // style of specifying background colors.
@ -113,17 +114,17 @@ Color::Color (const std::string& spec)
if (word == "bold") if (word == "bold")
{ {
value |= COLOR_BOLD; value |= _COLOR_BOLD;
value &= ~COLOR_256; value &= ~_COLOR_256;
} }
else if (word == "bright") else if (word == "bright")
{ {
value |= COLOR_BRIGHT; value |= _COLOR_BRIGHT;
value &= ~COLOR_256; value &= ~_COLOR_256;
} }
else if (word == "underline") else if (word == "underline")
{ {
value |= COLOR_UNDERLINE; value |= _COLOR_UNDERLINE;
} }
else if (word == "on") else if (word == "on")
{ {
@ -135,12 +136,12 @@ Color::Color (const std::string& spec)
{ {
if (bg) if (bg)
{ {
value &= ~COLOR_NOBG; value &= ~_COLOR_NOBG;
value |= index << 8; value |= index << 8;
} }
else else
{ {
value &= ~COLOR_NOFG; value &= ~_COLOR_NOFG;
value |= index; value |= index;
} }
} }
@ -155,16 +156,16 @@ Color::Color (const std::string& spec)
if (bg) if (bg)
{ {
value &= ~COLOR_NOBG; value &= ~_COLOR_NOBG;
value |= (index + 232) << 8; value |= (index + 232) << 8;
} }
else else
{ {
value &= ~COLOR_NOFG; value &= ~_COLOR_NOFG;
value |= index + 232; value |= index + 232;
} }
value |= COLOR_256; value |= _COLOR_256;
} }
// rgbRGB, where 0 <= R,G,B <= 5. // rgbRGB, where 0 <= R,G,B <= 5.
@ -186,16 +187,16 @@ Color::Color (const std::string& spec)
index = 16 + r*36 + g*6 + b; index = 16 + r*36 + g*6 + b;
if (bg) if (bg)
{ {
value &= ~COLOR_NOBG; value &= ~_COLOR_NOBG;
value |= index << 8; value |= index << 8;
} }
else else
{ {
value &= ~COLOR_NOFG; value &= ~_COLOR_NOFG;
value |= index; value |= index;
} }
value |= COLOR_256; value |= _COLOR_256;
} }
// colorN, where 0 <= N <= 255. // colorN, where 0 <= N <= 255.
@ -207,16 +208,16 @@ Color::Color (const std::string& spec)
if (bg) if (bg)
{ {
value &= ~COLOR_NOBG; value &= ~_COLOR_NOBG;
value |= index << 8; value |= index << 8;
} }
else else
{ {
value &= ~COLOR_NOFG; value &= ~_COLOR_NOFG;
value |= index; value |= index;
} }
value |= COLOR_256; value |= _COLOR_256;
} }
else else
throw std::string ("The color '") + *it + "' is not recognized."; throw std::string ("The color '") + *it + "' is not recognized.";
@ -224,22 +225,50 @@ Color::Color (const std::string& spec)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Color::Color (color_id fg, color_id bg, bool underline, bool bold, bool bright) Color::Color (color_id fg)
: value (COLOR_NOFG | COLOR_NOBG) : value (_COLOR_NOFG | _COLOR_NOBG)
{ {
value = ((underline ? 1 : 0) << 18) if (fg != Color::nocolor)
| ((bold ? 1 : 0) << 17) {
| ((bright ? 1 : 0) << 16); value &= ~_COLOR_NOFG;
value |= fg;
}
}
////////////////////////////////////////////////////////////////////////////////
Color::Color (color_id fg, color_id bg)
: value (_COLOR_NOFG | _COLOR_NOBG)
{
if (bg != Color::nocolor) if (bg != Color::nocolor)
{ {
value &= ~COLOR_NOBG; value &= ~_COLOR_NOBG;
value |= (bg << 8); value |= (bg << 8);
} }
if (fg != Color::nocolor) if (fg != Color::nocolor)
{ {
value &= ~COLOR_NOFG; value &= ~_COLOR_NOFG;
value |= fg;
}
}
////////////////////////////////////////////////////////////////////////////////
Color::Color (color_id fg, color_id bg, bool underline, bool bold, bool bright)
: value (_COLOR_NOFG | _COLOR_NOBG)
{
value |= ((underline ? 1 : 0) << 18)
| ((bold ? 1 : 0) << 17)
| ((bright ? 1 : 0) << 16);
if (bg != Color::nocolor)
{
value &= ~_COLOR_NOBG;
value |= (bg << 8);
}
if (fg != Color::nocolor)
{
value &= ~_COLOR_NOFG;
value |= fg; value |= fg;
} }
} }
@ -262,19 +291,19 @@ Color& Color::operator= (const Color& other)
Color::operator std::string () Color::operator std::string ()
{ {
std::string description; std::string description;
if (value & COLOR_BOLD) description += "bold"; if (value & _COLOR_BOLD) description += "bold";
if (value & COLOR_UNDERLINE) if (value & _COLOR_UNDERLINE)
description += std::string (description.length () ? " " : "") + "underline"; description += std::string (description.length () ? " " : "") + "underline";
if (!(value & COLOR_NOFG)) if (!(value & _COLOR_NOFG))
description += std::string (description.length () ? " " : "") + fg (); description += std::string (description.length () ? " " : "") + fg ();
if (!(value & COLOR_NOBG)) if (!(value & _COLOR_NOBG))
{ {
description += std::string (description.length () ? " " : "") + "on"; description += std::string (description.length () ? " " : "") + "on";
if (value & COLOR_BRIGHT) if (value & _COLOR_BRIGHT)
description += std::string (description.length () ? " " : "") + "bright"; description += std::string (description.length () ? " " : "") + "bright";
description += " " + bg (); description += " " + bg ();
@ -295,74 +324,74 @@ Color::operator int ()
void Color::blend (const Color& other) void Color::blend (const Color& other)
{ {
// Matching 256-color specifications. Merge all relevant bits. // Matching 256-color specifications. Merge all relevant bits.
if (value & COLOR_256 && if (value & _COLOR_256 &&
other.value & COLOR_256) other.value & _COLOR_256)
{ {
if (!(other.value & COLOR_NOBG)) if (!(other.value & _COLOR_NOBG))
{ {
value &= ~COLOR_BG; // Remove previous color. value &= ~_COLOR_BG; // Remove previous color.
value |= (other.value & COLOR_BG); // Apply new color. value |= (other.value & _COLOR_BG); // Apply new color.
value &= ~COLOR_NOBG; // Now have a color. value &= ~_COLOR_NOBG; // Now have a color.
} }
if (!(other.value & COLOR_NOFG)) if (!(other.value & _COLOR_NOFG))
{ {
value &= ~COLOR_FG; // Remove previous color. value &= ~_COLOR_FG; // Remove previous color.
value |= (other.value & COLOR_FG); // Apply new color. value |= (other.value & _COLOR_FG); // Apply new color.
value &= ~COLOR_NOFG; // Now have a color. value &= ~_COLOR_NOFG; // Now have a color.
} }
} }
// Matching 16-color specifications. Merge all relevant bits. // Matching 16-color specifications. Merge all relevant bits.
else if (!(value & COLOR_256) && else if (!(value & _COLOR_256) &&
!(other.value & COLOR_256)) !(other.value & _COLOR_256))
{ {
value |= (other.value & COLOR_BOLD); // Inherit boldness. value |= (other.value & _COLOR_BOLD); // Inherit boldness.
value |= (other.value & COLOR_BRIGHT); // Inherit brightness. value |= (other.value & _COLOR_BRIGHT); // Inherit brightness.
if (!(other.value & COLOR_NOBG)) if (!(other.value & _COLOR_NOBG))
{ {
value &= ~COLOR_BG; // Remove previous color. value &= ~_COLOR_BG; // Remove previous color.
value |= (other.value & COLOR_BG); // Apply new color. value |= (other.value & _COLOR_BG); // Apply new color.
value &= ~COLOR_NOBG; // Now have a color. value &= ~_COLOR_NOBG; // Now have a color.
} }
if (!(other.value & COLOR_NOFG)) if (!(other.value & _COLOR_NOFG))
{ {
value &= ~COLOR_FG; // Remove previous color. value &= ~_COLOR_FG; // Remove previous color.
value |= (other.value & COLOR_FG); // Apply new color. value |= (other.value & _COLOR_FG); // Apply new color.
value &= ~COLOR_NOFG; // Now have a color. value &= ~_COLOR_NOFG; // Now have a color.
} }
} }
// If a 16-color is blended with a 256-color, then the 16-color is upgraded. // If a 16-color is blended with a 256-color, then the 16-color is upgraded.
else if (!(value & COLOR_256) && else if (!(value & _COLOR_256) &&
other.value & COLOR_256) other.value & _COLOR_256)
{ {
value |= COLOR_256; // Upgrade to 256-color. value |= _COLOR_256; // Upgrade to 256-color.
value &= ~COLOR_BOLD; // Ignore boldness. value &= ~_COLOR_BOLD; // Ignore boldness.
value &= ~COLOR_BRIGHT; // Ignore brightness. value &= ~_COLOR_BRIGHT; // Ignore brightness.
value &= ~COLOR_FG; // Ignore original 16-color. value &= ~_COLOR_FG; // Ignore original 16-color.
value &= ~COLOR_BG; // Ignore original 16-color. value &= ~_COLOR_BG; // Ignore original 16-color.
value |= COLOR_NOFG; // No fg. value |= _COLOR_NOFG; // No fg.
value |= COLOR_NOBG; // No bg. value |= _COLOR_NOBG; // No bg.
if (!(other.value & COLOR_NOBG)) if (!(other.value & _COLOR_NOBG))
{ {
value &= ~COLOR_BG; // Remove previous color. value &= ~_COLOR_BG; // Remove previous color.
value |= (other.value & COLOR_BG); // Apply new color. value |= (other.value & _COLOR_BG); // Apply new color.
value &= ~COLOR_NOBG; // Now have a color. value &= ~_COLOR_NOBG; // Now have a color.
} }
if (!(other.value & COLOR_NOFG)) if (!(other.value & _COLOR_NOFG))
{ {
value &= ~COLOR_FG; // Remove previous color. value &= ~_COLOR_FG; // Remove previous color.
value |= (other.value & COLOR_FG); // Apply new color. value |= (other.value & _COLOR_FG); // Apply new color.
value &= ~COLOR_NOFG; // Now have a color. value &= ~_COLOR_NOFG; // Now have a color.
} }
} }
value |= (other.value & COLOR_UNDERLINE); // Always inherit underline. value |= (other.value & _COLOR_UNDERLINE); // Always inherit underline.
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -387,16 +416,16 @@ std::string Color::colorize (const std::string& input)
std::stringstream result; std::stringstream result;
// 256 color // 256 color
if (value & COLOR_256) if (value & _COLOR_256)
{ {
if (value & COLOR_UNDERLINE) if (value & _COLOR_UNDERLINE)
result << "\033[4m"; result << "\033[4m";
if (!(value & COLOR_NOFG)) if (!(value & _COLOR_NOFG))
result << "\033[38;5;" << (value & COLOR_FG) << "m"; result << "\033[38;5;" << (value & _COLOR_FG) << "m";
if (!(value & COLOR_NOBG)) if (!(value & _COLOR_NOBG))
result << "\033[48;5;" << ((value & COLOR_BG) >> 8) << "m"; result << "\033[48;5;" << ((value & _COLOR_BG) >> 8) << "m";
} }
// 16 color // 16 color
@ -404,28 +433,28 @@ std::string Color::colorize (const std::string& input)
{ {
result << "\033["; result << "\033[";
if (value & COLOR_BOLD) if (value & _COLOR_BOLD)
{ {
if (count++) result << ";"; if (count++) result << ";";
result << "1"; result << "1";
} }
if (value & COLOR_UNDERLINE) if (value & _COLOR_UNDERLINE)
{ {
if (count++) result << ";"; if (count++) result << ";";
result << "4"; result << "4";
} }
if (!(value & COLOR_NOBG)) if (!(value & _COLOR_NOBG))
{ {
if (count++) result << ";"; if (count++) result << ";";
result << ((value & COLOR_BRIGHT ? 99 : 39) + ((value & COLOR_BG) >> 8)); result << ((value & _COLOR_BRIGHT ? 99 : 39) + ((value & _COLOR_BG) >> 8));
} }
if (!(value & COLOR_NOFG)) if (!(value & _COLOR_NOFG))
{ {
if (count++) result << ";"; if (count++) result << ";";
result << (29 + (value & COLOR_FG)); result << (29 + (value & _COLOR_FG));
} }
result << "m"; result << "m";
@ -455,14 +484,14 @@ int Color::find (const std::string& input)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Color::fg () std::string Color::fg ()
{ {
int index = value & COLOR_FG; int index = value & _COLOR_FG;
if (value & COLOR_256) if (value & _COLOR_256)
{ {
if (!(value & COLOR_NOFG)) if (!(value & _COLOR_NOFG))
{ {
std::stringstream s; std::stringstream s;
s << "color" << (value & COLOR_FG); s << "color" << (value & _COLOR_FG);
return s.str (); return s.str ();
} }
} }
@ -479,14 +508,14 @@ std::string Color::fg ()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Color::bg () std::string Color::bg ()
{ {
int index = (value & COLOR_BG) >> 8; int index = (value & _COLOR_BG) >> 8;
if (value & COLOR_256) if (value & _COLOR_256)
{ {
if (!(value & COLOR_NOBG)) if (!(value & _COLOR_NOBG))
{ {
std::stringstream s; std::stringstream s;
s << "color" << ((value & COLOR_BG) >> 8); s << "color" << ((value & _COLOR_BG) >> 8);
return s.str (); return s.str ();
} }
} }

View file

@ -29,17 +29,15 @@
#include <string> #include <string>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// TODO Define how these bits are used. #define _COLOR_256 0x00200000 // 256-color mode.
#define COLOR_256 0x00200000 #define _COLOR_NOBG 0x00100000 // No background color (all values taken).
#define COLOR_NOBG 0x00100000 #define _COLOR_NOFG 0x00080000 // No foreground color (all values taken).
#define COLOR_NOFG 0x00080000 #define _COLOR_UNDERLINE 0x00040000 // General underline attribute.
#define COLOR_UNDERLINE 0x00040000 #define _COLOR_BOLD 0x00020000 // 16-color bold attribute.
#define COLOR_BOLD 0x00020000 #define _COLOR_BRIGHT 0x00010000 // 16-color bright background attribute.
#define COLOR_BRIGHT 0x00010000 #define _COLOR_BG 0x0000FF00 // 8-bit background color index.
#define COLOR_BG 0x0000FF00 #define _COLOR_FG 0x000000FF // 8-bit foreground color index.
#define COLOR_FG 0x000000FF
class Color class Color
{ {
@ -50,6 +48,8 @@ public:
Color (const Color&); Color (const Color&);
Color (unsigned int); // 256 | UNDERLINE | BOLD | BRIGHT | (BG << 8) | FG Color (unsigned int); // 256 | UNDERLINE | BOLD | BRIGHT | (BG << 8) | FG
Color (const std::string&); // "red on bright black" Color (const std::string&); // "red on bright black"
Color (color_id); // fg.
Color (color_id, color_id); // fg, bg.
Color (color_id, color_id, bool, bool, bool); // fg, bg, underline, bold, bright Color (color_id, color_id, bool, bool, bool); // fg, bg, underline, bold, bright
~Color (); ~Color ();
Color& operator= (const Color&); Color& operator= (const Color&);

View file

@ -1,196 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
// task - a command line task list manager.
//
// Copyright 2006 - 2009, Paul Beckingham.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the
//
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA
// 02110-1301
// USA
//
////////////////////////////////////////////////////////////////////////////////
#include <string>
#include "Context.h"
#include "text.h"
#include "util.h"
#include "i18n.h"
#include "color.h"
extern Context context;
////////////////////////////////////////////////////////////////////////////////
namespace Text
{
static struct
{
color id;
int string_id;
std::string english_name;
std::string escape_sequence;
} allColors[] =
{
// Text::color i18n.h English vt220? xterm?
{ nocolor, 0, "", "" },
{ off, COLOR_OFF, "off", "" },
{ bold, COLOR_BOLD, "bold", "\033[1m" },
{ underline, COLOR_UL, "underline", "\033[4m" },
{ bold_underline, COLOR_B_UL, "bold_underline", "\033[1;4m" },
{ black, COLOR_BLACK, "black", "\033[30m" },
{ red, COLOR_RED, "red", "\033[31m" },
{ green, COLOR_GREEN, "green", "\033[32m" },
{ yellow, COLOR_YELLOW, "yellow", "\033[33m" },
{ blue, COLOR_BLUE, "blue", "\033[34m" },
{ magenta, COLOR_MAGENTA, "magenta", "\033[35m" },
{ cyan, COLOR_CYAN, "cyan", "\033[36m" },
{ white, COLOR_WHITE, "white", "\033[37m" },
{ bold_black, COLOR_B_BLACK, "bold_black", "\033[90m" },
{ bold_red, COLOR_B_RED, "bold_red", "\033[91m" },
{ bold_green, COLOR_B_GREEN, "bold_green", "\033[92m" },
{ bold_yellow, COLOR_B_YELLOW, "bold_yellow", "\033[93m" },
{ bold_blue, COLOR_B_BLUE, "bold_blue", "\033[94m" },
{ bold_magenta, COLOR_B_MAGENTA, "bold_magenta", "\033[95m" },
{ bold_cyan, COLOR_B_CYAN, "bold_cyan", "\033[96m" },
{ bold_white, COLOR_B_WHITE, "bold_white", "\033[97m" },
{ underline_black, COLOR_UL_BLACK, "underline_black", "\033[4;30m" },
{ underline_red, COLOR_UL_RED, "underline_red", "\033[4;31m" },
{ underline_green, COLOR_UL_GREEN, "underline_green", "\033[4;32m" },
{ underline_yellow, COLOR_UL_YELLOW, "underline_yellow", "\033[4;33m" },
{ underline_blue, COLOR_UL_BLUE, "underline_blue", "\033[4;34m" },
{ underline_magenta, COLOR_UL_MAGENTA, "underline_magenta", "\033[4;35m" },
{ underline_cyan, COLOR_UL_CYAN, "underline_cyan", "\033[4;36m" },
{ underline_white, COLOR_UL_WHITE, "underline_white", "\033[4;37m" },
{ bold_underline_black, COLOR_B_UL_BLACK, "bold_underline_black", "\033[1;4;30m" },
{ bold_underline_red, COLOR_B_UL_RED, "bold_underline_red", "\033[1;4;31m" },
{ bold_underline_green, COLOR_B_UL_GREEN, "bold_underline_green", "\033[1;4;32m" },
{ bold_underline_yellow, COLOR_B_UL_YELLOW, "bold_underline_yellow", "\033[1;4;33m" },
{ bold_underline_blue, COLOR_B_UL_BLUE, "bold_underline_blue", "\033[1;4;34m" },
{ bold_underline_magenta, COLOR_B_UL_MAGENTA, "bold_underline_magenta", "\033[1;4;35m" },
{ bold_underline_cyan, COLOR_B_UL_CYAN, "bold_underline_cyan", "\033[1;4;36m" },
{ bold_underline_white, COLOR_B_UL_WHITE, "bold_underline_white", "\033[1;4;37m" },
{ on_black, COLOR_ON_BLACK, "on_black", "\033[40m" },
{ on_red, COLOR_ON_RED, "on_red", "\033[41m" },
{ on_green, COLOR_ON_GREEN, "on_green", "\033[42m" },
{ on_yellow, COLOR_ON_YELLOW, "on_yellow", "\033[43m" },
{ on_blue, COLOR_ON_BLUE, "on_blue", "\033[44m" },
{ on_magenta, COLOR_ON_MAGENTA, "on_magenta", "\033[45m" },
{ on_cyan, COLOR_ON_CYAN, "on_cyan", "\033[46m" },
{ on_white, COLOR_ON_WHITE, "on_white", "\033[47m" },
{ on_bright_black, COLOR_ON_BRIGHT_BLACK, "on_bright_black", "\033[100m" },
{ on_bright_red, COLOR_ON_BRIGHT_RED, "on_bright_red", "\033[101m" },
{ on_bright_green, COLOR_ON_BRIGHT_GREEN, "on_bright_green", "\033[102m" },
{ on_bright_yellow, COLOR_ON_BRIGHT_YELLOW, "on_bright_yellow", "\033[103m" },
{ on_bright_blue, COLOR_ON_BRIGHT_BLUE, "on_bright_blue", "\033[104m" },
{ on_bright_magenta, COLOR_ON_BRIGHT_MAGENTA, "on_bright_magenta", "\033[105m" },
{ on_bright_cyan, COLOR_ON_BRIGHT_CYAN, "on_bright_cyan", "\033[106m" },
{ on_bright_white, COLOR_ON_BRIGHT_WHITE, "on_bright_white", "\033[107m" },
};
#define NUM_COLORS (sizeof (allColors) / sizeof (allColors[0]))
////////////////////////////////////////////////////////////////////////////////
std::string colorName (color c)
{
for (unsigned int i = 0; i < NUM_COLORS; ++i)
if (allColors[i].id == c)
return allColors[i].english_name;
throw context.stringtable.get (COLOR_UNKNOWN, "Unknown color value");
return "";
}
////////////////////////////////////////////////////////////////////////////////
color colorCode (const std::string& c)
{
for (unsigned int i = 0; i < NUM_COLORS; ++i)
if (context.stringtable.get (allColors[i].string_id, allColors[i].english_name) == c)
return allColors[i].id;
return nocolor;
}
////////////////////////////////////////////////////////////////////////////////
std::string decode (color c)
{
for (unsigned int i = 0; i < NUM_COLORS; ++i)
if (allColors[i].id == c)
return allColors[i].escape_sequence;
throw context.stringtable.get (COLOR_UNKNOWN, "Unknown color value");
return "";
}
////////////////////////////////////////////////////////////////////////////////
std::string colorize (color fg, color bg, const std::string& input)
{
if (input.length ())
if (fg != nocolor || bg != nocolor)
return decode (fg) + decode (bg) + input + decode (off);
return input;
}
////////////////////////////////////////////////////////////////////////////////
std::string colorize (color fg, color bg)
{
return decode (fg) + decode (bg);
}
////////////////////////////////////////////////////////////////////////////////
std::string colorize ()
{
return decode (off);
}
////////////////////////////////////////////////////////////////////////////////
std::string guessColor (const std::string& name)
{
std::vector <std::string> all;
for (unsigned int i = 0; i < NUM_COLORS; ++i)
all.push_back (context.stringtable.get (
allColors[i].string_id,
allColors[i].english_name));
std::vector <std::string> matches;
autoComplete (name, all, matches);
if (matches.size () == 0)
throw std::string ("Unrecognized color '") + name + "'";
else if (matches.size () != 1)
{
std::string error = "Ambiguous color '" + name + "' - could be either of "; // TODO i18n
std::string combined;
join (combined, ", ", matches);
throw error + combined;
}
return matches[0];
}
////////////////////////////////////////////////////////////////////////////////
}

View file

@ -1,58 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
// task - a command line task list manager.
//
// Copyright 2006 - 2009, Paul Beckingham.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the
//
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor,
// Boston, MA
// 02110-1301
// USA
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_COLOR
#define INCLUDED_COLOR
namespace Text
{
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 (color);
color colorCode (const std::string&);
std::string colorize (color, color, const std::string& string);
std::string colorize (color, color);
std::string colorize ();
std::string guessColor (const std::string&);
}
#endif
////////////////////////////////////////////////////////////////////////////////