|
|
|
|
@@ -32,6 +32,7 @@
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <main.h>
|
|
|
|
|
#include <Color.h>
|
|
|
|
|
#include <text.h>
|
|
|
|
|
#include <i18n.h>
|
|
|
|
|
@@ -76,12 +77,14 @@ Color::Color (const Color& other)
|
|
|
|
|
Color::Color (unsigned int c)
|
|
|
|
|
: _value (0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
if (!(c & _COLOR_HASFG)) _value &= ~_COLOR_FG;
|
|
|
|
|
if (!(c & _COLOR_HASBG)) _value &= ~_COLOR_BG;
|
|
|
|
|
|
|
|
|
|
_value = c & (_COLOR_256 | _COLOR_HASBG | _COLOR_HASFG |_COLOR_UNDERLINE |
|
|
|
|
|
_COLOR_INVERSE | _COLOR_BOLD | _COLOR_BRIGHT | _COLOR_BG |
|
|
|
|
|
_COLOR_FG);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@@ -99,6 +102,7 @@ Color::Color (unsigned int c)
|
|
|
|
|
Color::Color (const std::string& spec)
|
|
|
|
|
: _value (0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
// By converting underscores to spaces, we inherently support the old "on_red"
|
|
|
|
|
// style of specifying background colors. We consider underscores to be
|
|
|
|
|
// deprecated.
|
|
|
|
|
@@ -230,23 +234,27 @@ Color::Color (const std::string& spec)
|
|
|
|
|
// Now combine the fg and bg into a single color.
|
|
|
|
|
_value = fg_value;
|
|
|
|
|
blend (Color (bg_value));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
Color::Color (color_id fg)
|
|
|
|
|
: _value (0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
if (fg != Color::nocolor)
|
|
|
|
|
{
|
|
|
|
|
_value |= _COLOR_HASFG;
|
|
|
|
|
_value |= fg;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
Color::Color (color_id fg, color_id bg)
|
|
|
|
|
: _value (0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
if (bg != Color::nocolor)
|
|
|
|
|
{
|
|
|
|
|
_value |= _COLOR_HASBG;
|
|
|
|
|
@@ -258,12 +266,14 @@ Color::Color (color_id fg, color_id bg)
|
|
|
|
|
_value |= _COLOR_HASFG;
|
|
|
|
|
_value |= fg;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
Color::Color (color_id fg, color_id bg, bool underline, bool bold, bool bright)
|
|
|
|
|
: _value (0)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
_value |= ((underline ? 1 : 0) << 18)
|
|
|
|
|
| ((bold ? 1 : 0) << 17)
|
|
|
|
|
| ((bright ? 1 : 0) << 16);
|
|
|
|
|
@@ -279,6 +289,7 @@ Color::Color (color_id fg, color_id bg, bool underline, bool bold, bool bright)
|
|
|
|
|
_value |= _COLOR_HASFG;
|
|
|
|
|
_value |= fg;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@@ -299,6 +310,7 @@ Color& Color::operator= (const Color& other)
|
|
|
|
|
Color::operator std::string () const
|
|
|
|
|
{
|
|
|
|
|
std::string description;
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
if (_value & _COLOR_BOLD) description += "bold";
|
|
|
|
|
|
|
|
|
|
if (_value & _COLOR_UNDERLINE)
|
|
|
|
|
@@ -319,6 +331,7 @@ Color::operator std::string () const
|
|
|
|
|
|
|
|
|
|
description += " " + bg ();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return description;
|
|
|
|
|
}
|
|
|
|
|
@@ -334,6 +347,7 @@ Color::operator int () const
|
|
|
|
|
// other take precedence.
|
|
|
|
|
void Color::blend (const Color& other)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
Color c (other);
|
|
|
|
|
_value |= (c._value & _COLOR_UNDERLINE); // Always inherit underline.
|
|
|
|
|
_value |= (c._value & _COLOR_INVERSE); // Always inherit inverse.
|
|
|
|
|
@@ -382,11 +396,13 @@ void Color::blend (const Color& other)
|
|
|
|
|
_value |= (c._value & _COLOR_BG); // Apply other color.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
void Color::upgrade ()
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
if (!(_value & _COLOR_256))
|
|
|
|
|
{
|
|
|
|
|
if (_value & _COLOR_HASFG)
|
|
|
|
|
@@ -409,6 +425,7 @@ void Color::upgrade ()
|
|
|
|
|
|
|
|
|
|
_value |= _COLOR_256;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@@ -425,6 +442,7 @@ void Color::upgrade ()
|
|
|
|
|
// 256 bg \033[48;5;Nm
|
|
|
|
|
std::string Color::colorize (const std::string& input)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
if (_value == 0)
|
|
|
|
|
return input;
|
|
|
|
|
|
|
|
|
|
@@ -505,6 +523,7 @@ std::string Color::colorize (const std::string& input)
|
|
|
|
|
result << "m" << input << "\033[0m";
|
|
|
|
|
return result.str ();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
@@ -513,6 +532,7 @@ std::string Color::colorize (const std::string& input)
|
|
|
|
|
// Remove color codes from a string.
|
|
|
|
|
std::string Color::strip (const std::string& input)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
int length = input.length ();
|
|
|
|
|
bool inside = false;
|
|
|
|
|
std::string output;
|
|
|
|
|
@@ -533,13 +553,20 @@ std::string Color::strip (const std::string& input)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
#else
|
|
|
|
|
return input;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
std::string Color::colorize (const std::string& input, const std::string& spec)
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
Color c (spec);
|
|
|
|
|
return c.colorize (input);
|
|
|
|
|
#else
|
|
|
|
|
return input;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
@@ -561,6 +588,7 @@ int Color::find (const std::string& input)
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
std::string Color::fg () const
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
int index = _value & _COLOR_FG;
|
|
|
|
|
|
|
|
|
|
if (_value & _COLOR_256)
|
|
|
|
|
@@ -578,6 +606,7 @@ std::string Color::fg () const
|
|
|
|
|
if (allColors[i].index == index)
|
|
|
|
|
return allColors[i].english_name;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
@@ -585,6 +614,7 @@ std::string Color::fg () const
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
std::string Color::bg () const
|
|
|
|
|
{
|
|
|
|
|
#ifdef FEATURE_COLOR
|
|
|
|
|
int index = (_value & _COLOR_BG) >> 8;
|
|
|
|
|
|
|
|
|
|
if (_value & _COLOR_256)
|
|
|
|
|
@@ -602,6 +632,7 @@ std::string Color::bg () const
|
|
|
|
|
if (allColors[i].index == index)
|
|
|
|
|
return allColors[i].english_name;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|