Color: Fixed incorrect used of leading underscore in #define

This commit is contained in:
Paul Beckingham 2015-10-12 15:50:14 -04:00
parent da4fd02cc5
commit 5ed085a2f4
2 changed files with 102 additions and 134 deletions

View file

@ -25,12 +25,12 @@
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <cmake.h> #include <cmake.h>
#include <Color.h>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <Color.h>
#include <text.h> #include <text.h>
#include <i18n.h> #include <i18n.h>
@ -74,14 +74,12 @@ Color::Color (const Color& other)
Color::Color (unsigned int c) Color::Color (unsigned int c)
: _value (0) : _value (0)
{ {
#ifdef FEATURE_COLOR if (!(c & COLOR_HASFG)) _value &= ~COLOR_FG;
if (!(c & _COLOR_HASFG)) _value &= ~_COLOR_FG; if (!(c & COLOR_HASBG)) _value &= ~COLOR_BG;
if (!(c & _COLOR_HASBG)) _value &= ~_COLOR_BG;
_value = c & (_COLOR_256 | _COLOR_HASBG | _COLOR_HASFG |_COLOR_UNDERLINE | _value = c & (COLOR_256 | COLOR_HASBG | COLOR_HASFG |COLOR_UNDERLINE |
_COLOR_INVERSE | _COLOR_BOLD | _COLOR_BRIGHT | _COLOR_BG | COLOR_INVERSE | COLOR_BOLD | COLOR_BRIGHT | COLOR_BG |
_COLOR_FG); COLOR_FG);
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -99,7 +97,6 @@ Color::Color (unsigned int c)
Color::Color (const std::string& spec) Color::Color (const std::string& spec)
: _value (0) : _value (0)
{ {
#ifdef FEATURE_COLOR
// 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. We consider underscores to be // style of specifying background colors. We consider underscores to be
// deprecated. // deprecated.
@ -124,10 +121,10 @@ Color::Color (const std::string& spec)
{ {
word = lowerCase (trim (*it)); word = lowerCase (trim (*it));
if (word == "bold") fg_value |= _COLOR_BOLD; if (word == "bold") fg_value |= COLOR_BOLD;
else if (word == "bright") bg_value |= _COLOR_BRIGHT; else if (word == "bright") bg_value |= COLOR_BRIGHT;
else if (word == "underline") fg_value |= _COLOR_UNDERLINE; else if (word == "underline") fg_value |= COLOR_UNDERLINE;
else if (word == "inverse") fg_value |= _COLOR_INVERSE; else if (word == "inverse") fg_value |= COLOR_INVERSE;
else if (word == "on") bg = true; else if (word == "on") bg = true;
// X where X is one of black, red, blue ... // X where X is one of black, red, blue ...
@ -137,12 +134,12 @@ Color::Color (const std::string& spec)
{ {
if (bg) if (bg)
{ {
bg_value |= _COLOR_HASBG; bg_value |= COLOR_HASBG;
bg_value |= index << 8; bg_value |= index << 8;
} }
else else
{ {
fg_value |= _COLOR_HASFG; fg_value |= COLOR_HASFG;
fg_value |= index; fg_value |= index;
} }
} }
@ -158,15 +155,15 @@ Color::Color (const std::string& spec)
if (bg) if (bg)
{ {
bg_value |= _COLOR_HASBG; bg_value |= COLOR_HASBG;
bg_value |= (index + 232) << 8; bg_value |= (index + 232) << 8;
bg_value |= _COLOR_256; bg_value |= COLOR_256;
} }
else else
{ {
fg_value |= _COLOR_HASFG; fg_value |= COLOR_HASFG;
fg_value |= index + 232; fg_value |= index + 232;
fg_value |= _COLOR_256; fg_value |= COLOR_256;
} }
} }
@ -190,15 +187,15 @@ Color::Color (const std::string& spec)
if (bg) if (bg)
{ {
bg_value |= _COLOR_HASBG; bg_value |= COLOR_HASBG;
bg_value |= index << 8; bg_value |= index << 8;
bg_value |= _COLOR_256; bg_value |= COLOR_256;
} }
else else
{ {
fg_value |= _COLOR_HASFG; fg_value |= COLOR_HASFG;
fg_value |= index; fg_value |= index;
fg_value |= _COLOR_256; fg_value |= COLOR_256;
} }
} }
@ -213,15 +210,15 @@ Color::Color (const std::string& spec)
if (bg) if (bg)
{ {
bg_value |= _COLOR_HASBG; bg_value |= COLOR_HASBG;
bg_value |= index << 8; bg_value |= index << 8;
bg_value |= _COLOR_256; bg_value |= COLOR_256;
} }
else else
{ {
fg_value |= _COLOR_HASFG; fg_value |= COLOR_HASFG;
fg_value |= index; fg_value |= index;
fg_value |= _COLOR_256; fg_value |= COLOR_256;
} }
} }
else if (word != "") else if (word != "")
@ -231,62 +228,55 @@ Color::Color (const std::string& spec)
// Now combine the fg and bg into a single color. // Now combine the fg and bg into a single color.
_value = fg_value; _value = fg_value;
blend (Color (bg_value)); blend (Color (bg_value));
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Color::Color (color_id fg) Color::Color (color_id fg)
: _value (0) : _value (0)
{ {
#ifdef FEATURE_COLOR
if (fg != Color::nocolor) if (fg != Color::nocolor)
{ {
_value |= _COLOR_HASFG; _value |= COLOR_HASFG;
_value |= fg; _value |= fg;
} }
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Color::Color (color_id fg, color_id bg) Color::Color (color_id fg, color_id bg)
: _value (0) : _value (0)
{ {
#ifdef FEATURE_COLOR
if (bg != Color::nocolor) if (bg != Color::nocolor)
{ {
_value |= _COLOR_HASBG; _value |= COLOR_HASBG;
_value |= (bg << 8); _value |= (bg << 8);
} }
if (fg != Color::nocolor) if (fg != Color::nocolor)
{ {
_value |= _COLOR_HASFG; _value |= COLOR_HASFG;
_value |= fg; _value |= fg;
} }
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Color::Color (color_id fg, color_id bg, bool underline, bool bold, bool bright) Color::Color (color_id fg, color_id bg, bool underline, bool bold, bool bright)
: _value (0) : _value (0)
{ {
#ifdef FEATURE_COLOR
_value |= ((underline ? 1 : 0) << 18) _value |= ((underline ? 1 : 0) << 18)
| ((bold ? 1 : 0) << 17) | ((bold ? 1 : 0) << 17)
| ((bright ? 1 : 0) << 16); | ((bright ? 1 : 0) << 16);
if (bg != Color::nocolor) if (bg != Color::nocolor)
{ {
_value |= _COLOR_HASBG; _value |= COLOR_HASBG;
_value |= (bg << 8); _value |= (bg << 8);
} }
if (fg != Color::nocolor) if (fg != Color::nocolor)
{ {
_value |= _COLOR_HASFG; _value |= COLOR_HASFG;
_value |= fg; _value |= fg;
} }
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -307,28 +297,26 @@ Color& Color::operator= (const Color& other)
Color::operator std::string () const Color::operator std::string () const
{ {
std::string description; std::string description;
#ifdef FEATURE_COLOR 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_INVERSE) if (_value & COLOR_INVERSE)
description += std::string (description.length () ? " " : "") + "inverse"; description += std::string (description.length () ? " " : "") + "inverse";
if (_value & _COLOR_HASFG) if (_value & COLOR_HASFG)
description += std::string (description.length () ? " " : "") + fg (); description += std::string (description.length () ? " " : "") + fg ();
if (_value & _COLOR_HASBG) if (_value & COLOR_HASBG)
{ {
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 ();
} }
#endif
return description; return description;
} }
@ -344,33 +332,32 @@ Color::operator int () const
// other take precedence. // other take precedence.
void Color::blend (const Color& other) void Color::blend (const Color& other)
{ {
#ifdef FEATURE_COLOR
if (!other.nontrivial ()) if (!other.nontrivial ())
return; return;
Color c (other); Color c (other);
_value |= (c._value & _COLOR_UNDERLINE); // Always inherit underline. _value |= (c._value & COLOR_UNDERLINE); // Always inherit underline.
_value |= (c._value & _COLOR_INVERSE); // Always inherit inverse. _value |= (c._value & COLOR_INVERSE); // Always inherit inverse.
// 16 <-- 16. // 16 <-- 16.
if (!(_value & _COLOR_256) && if (!(_value & COLOR_256) &&
!(c._value & _COLOR_256)) !(c._value & COLOR_256))
{ {
_value |= (c._value & _COLOR_BOLD); // Inherit bold. _value |= (c._value & COLOR_BOLD); // Inherit bold.
_value |= (c._value & _COLOR_BRIGHT); // Inherit bright. _value |= (c._value & COLOR_BRIGHT); // Inherit bright.
if (c._value & _COLOR_HASFG) if (c._value & COLOR_HASFG)
{ {
_value |= _COLOR_HASFG; // There is now a color. _value |= COLOR_HASFG; // There is now a color.
_value &= ~_COLOR_FG; // Remove previous color. _value &= ~COLOR_FG; // Remove previous color.
_value |= (c._value & _COLOR_FG); // Apply other color. _value |= (c._value & COLOR_FG); // Apply other color.
} }
if (c._value & _COLOR_HASBG) if (c._value & COLOR_HASBG)
{ {
_value |= _COLOR_HASBG; // There is now a color. _value |= COLOR_HASBG; // There is now a color.
_value &= ~_COLOR_BG; // Remove previous color. _value &= ~COLOR_BG; // Remove previous color.
_value |= (c._value & _COLOR_BG); // Apply other color. _value |= (c._value & COLOR_BG); // Apply other color.
} }
return; return;
@ -378,54 +365,51 @@ void Color::blend (const Color& other)
else else
{ {
// Upgrade either color, if necessary. // Upgrade either color, if necessary.
if (!(_value & _COLOR_256)) upgrade (); if (!(_value & COLOR_256)) upgrade ();
if (!(c._value & _COLOR_256)) c.upgrade (); if (!(c._value & COLOR_256)) c.upgrade ();
// 256 <-- 256. // 256 <-- 256.
if (c._value & _COLOR_HASFG) if (c._value & COLOR_HASFG)
{ {
_value |= _COLOR_HASFG; // There is now a color. _value |= COLOR_HASFG; // There is now a color.
_value &= ~_COLOR_FG; // Remove previous color. _value &= ~COLOR_FG; // Remove previous color.
_value |= (c._value & _COLOR_FG); // Apply other color. _value |= (c._value & COLOR_FG); // Apply other color.
} }
if (c._value & _COLOR_HASBG) if (c._value & COLOR_HASBG)
{ {
_value |= _COLOR_HASBG; // There is now a color. _value |= COLOR_HASBG; // There is now a color.
_value &= ~_COLOR_BG; // Remove previous color. _value &= ~COLOR_BG; // Remove previous color.
_value |= (c._value & _COLOR_BG); // Apply other color. _value |= (c._value & COLOR_BG); // Apply other color.
} }
} }
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Color::upgrade () void Color::upgrade ()
{ {
#ifdef FEATURE_COLOR if (!(_value & COLOR_256))
if (!(_value & _COLOR_256))
{ {
if (_value & _COLOR_HASFG) if (_value & COLOR_HASFG)
{ {
bool bold = _value & _COLOR_BOLD; bool bold = _value & COLOR_BOLD;
unsigned int fg = _value & _COLOR_FG; unsigned int fg = _value & COLOR_FG;
_value &= ~_COLOR_FG; _value &= ~COLOR_FG;
_value &= ~_COLOR_BOLD; _value &= ~COLOR_BOLD;
_value |= (bold ? fg + 7 : fg - 1); _value |= (bold ? fg + 7 : fg - 1);
} }
if (_value & _COLOR_HASBG) if (_value & COLOR_HASBG)
{ {
bool bright = _value & _COLOR_BRIGHT; bool bright = _value & COLOR_BRIGHT;
unsigned int bg = (_value & _COLOR_BG) >> 8; unsigned int bg = (_value & COLOR_BG) >> 8;
_value &= ~_COLOR_BG; _value &= ~COLOR_BG;
_value &= ~_COLOR_BRIGHT; _value &= ~COLOR_BRIGHT;
_value |= (bright ? bg + 7 : bg - 1) << 8; _value |= (bright ? bg + 7 : bg - 1) << 8;
} }
_value |= _COLOR_256; _value |= COLOR_256;
} }
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -442,7 +426,6 @@ void Color::upgrade ()
// 256 bg \033[48;5;Nm // 256 bg \033[48;5;Nm
std::string Color::colorize (const std::string& input) std::string Color::colorize (const std::string& input)
{ {
#ifdef FEATURE_COLOR
if (!nontrivial ()) if (!nontrivial ())
return input; return input;
@ -450,31 +433,31 @@ 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)
{ {
bool needTerminator = false; bool needTerminator = false;
if (_value & _COLOR_UNDERLINE) if (_value & COLOR_UNDERLINE)
{ {
result << "\033[4m"; result << "\033[4m";
needTerminator = true; needTerminator = true;
} }
if (_value & _COLOR_INVERSE) if (_value & COLOR_INVERSE)
{ {
result << "\033[7m"; result << "\033[7m";
needTerminator = true; needTerminator = true;
} }
if (_value & _COLOR_HASFG) if (_value & COLOR_HASFG)
{ {
result << "\033[38;5;" << (_value & _COLOR_FG) << "m"; result << "\033[38;5;" << (_value & COLOR_FG) << "m";
needTerminator = true; needTerminator = true;
} }
if (_value & _COLOR_HASBG) if (_value & COLOR_HASBG)
{ {
result << "\033[48;5;" << ((_value & _COLOR_BG) >> 8) << "m"; result << "\033[48;5;" << ((_value & COLOR_BG) >> 8) << "m";
needTerminator = true; needTerminator = true;
} }
@ -490,40 +473,39 @@ 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_INVERSE) if (_value & COLOR_INVERSE)
{ {
if (count++) result << ";"; if (count++) result << ";";
result << "7"; result << "7";
} }
if (_value & _COLOR_HASFG) if (_value & COLOR_HASFG)
{ {
if (count++) result << ";"; if (count++) result << ";";
result << (29 + (_value & _COLOR_FG)); result << (29 + (_value & COLOR_FG));
} }
if (_value & _COLOR_HASBG) if (_value & COLOR_HASBG)
{ {
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));
} }
result << "m" << input << "\033[0m"; result << "m" << input << "\033[0m";
return result.str (); return result.str ();
} }
#endif
return input; return input;
} }
@ -532,7 +514,6 @@ std::string Color::colorize (const std::string& input)
// Remove color codes from a string. // Remove color codes from a string.
std::string Color::strip (const std::string& input) std::string Color::strip (const std::string& input)
{ {
#ifdef FEATURE_COLOR
int length = input.length (); int length = input.length ();
bool inside = false; bool inside = false;
std::string output; std::string output;
@ -553,20 +534,13 @@ std::string Color::strip (const std::string& input)
} }
return output; return output;
#else
return input;
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Color::colorize (const std::string& input, const std::string& spec) std::string Color::colorize (const std::string& input, const std::string& spec)
{ {
#ifdef FEATURE_COLOR
Color c (spec); Color c (spec);
return c.colorize (input); return c.colorize (input);
#else
return input;
#endif
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -588,15 +562,14 @@ int Color::find (const std::string& input)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Color::fg () const std::string Color::fg () const
{ {
#ifdef FEATURE_COLOR int index = _value & COLOR_FG;
int index = _value & _COLOR_FG;
if (_value & _COLOR_256) if (_value & COLOR_256)
{ {
if (_value & _COLOR_HASFG) if (_value & COLOR_HASFG)
{ {
std::stringstream s; std::stringstream s;
s << "color" << (_value & _COLOR_FG); s << "color" << (_value & COLOR_FG);
return s.str (); return s.str ();
} }
} }
@ -606,7 +579,6 @@ std::string Color::fg () const
if (allColors[i].index == index) if (allColors[i].index == index)
return allColors[i].english_name; return allColors[i].english_name;
} }
#endif
return ""; return "";
} }
@ -614,15 +586,14 @@ std::string Color::fg () const
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Color::bg () const std::string Color::bg () const
{ {
#ifdef FEATURE_COLOR int index = (_value & COLOR_BG) >> 8;
int index = (_value & _COLOR_BG) >> 8;
if (_value & _COLOR_256) if (_value & COLOR_256)
{ {
if (_value & _COLOR_HASBG) if (_value & COLOR_HASBG)
{ {
std::stringstream s; std::stringstream s;
s << "color" << ((_value & _COLOR_BG) >> 8); s << "color" << ((_value & COLOR_BG) >> 8);
return s.str (); return s.str ();
} }
} }
@ -632,7 +603,6 @@ std::string Color::bg () const
if (allColors[i].index == index) if (allColors[i].index == index)
return allColors[i].english_name; return allColors[i].english_name;
} }
#endif
return ""; return "";
} }

View file

@ -29,18 +29,16 @@
#include <string> #include <string>
#define FEATURE_COLOR 1
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#define _COLOR_INVERSE 0x00400000 // Inverse attribute. #define COLOR_INVERSE 0x00400000 // Inverse attribute.
#define _COLOR_256 0x00200000 // 256-color mode. #define COLOR_256 0x00200000 // 256-color mode.
#define _COLOR_HASBG 0x00100000 // Has background color (all values taken). #define COLOR_HASBG 0x00100000 // Has background color (all values taken).
#define _COLOR_HASFG 0x00080000 // Has foreground color (all values taken). #define COLOR_HASFG 0x00080000 // Has foreground color (all values taken).
#define _COLOR_UNDERLINE 0x00040000 // General underline attribute. #define COLOR_UNDERLINE 0x00040000 // General underline attribute.
#define _COLOR_BOLD 0x00020000 // 16-color bold attribute. #define COLOR_BOLD 0x00020000 // 16-color bold attribute.
#define _COLOR_BRIGHT 0x00010000 // 16-color bright background attribute. #define COLOR_BRIGHT 0x00010000 // 16-color bright background attribute.
#define _COLOR_BG 0x0000FF00 // 8-bit background color index. #define COLOR_BG 0x0000FF00 // 8-bit background color index.
#define _COLOR_FG 0x000000FF // 8-bit foreground color index. #define COLOR_FG 0x000000FF // 8-bit foreground color index.
class Color class Color
{ {