# HG changeset patch # User Teemu Piippo # Date 1452452024 -7200 # Node ID b3a33bc2e48261964df470d2998abac88b4aa26e # Parent c062273efa33b365f629652a2d436e467987c2ff Added support for long-form color codes, such as \c[Green] diff -r c062273efa33 -r b3a33bc2e482 sources/basics.h --- a/sources/basics.h Sun Jan 10 20:05:27 2016 +0200 +++ b/sources/basics.h Sun Jan 10 20:53:44 2016 +0200 @@ -88,6 +88,10 @@ return (a < b) ? b : (a > c) ? c : a; } +template +char (&_ArraySizeHelper(T (&array)[N]))[N]; +#define countof(array) (sizeof(_ArraySizeHelper( array ))) + struct Exitception {}; END_ZFC_NAMESPACE diff -r c062273efa33 -r b3a33bc2e482 sources/coloredline.cpp --- a/sources/coloredline.cpp Sun Jan 10 20:05:27 2016 +0200 +++ b/sources/coloredline.cpp Sun Jan 10 20:53:44 2016 +0200 @@ -105,23 +105,48 @@ if (m_boldActive) m_data << RLINE_OFF_BOLD; + m_boldActive = false; + // Chars may be in uppercase if (ch >= 'A' and ch <= 'V') ch += 'a' - 'A'; if (ch >= 'a' and ch <= 'v' and ch != 'l') { - auto colorInfo = colorCodes[ch - 'a']; - m_activeColor = colorInfo.color; - m_boldActive = colorInfo.bold; - assert (m_activeColor < 8); - set_color (m_activeColor, true); - - if (m_boldActive) - m_data << RLINE_ON_BOLD; + const ColorCodeInfo& colorInfo = colorCodes[ch - 'a']; + activate_color(colorInfo.color, colorInfo.bold); } - m_colorCodeStage = 0; + if (ch == '[') + m_colorCodeStage = 2; + else + m_colorCodeStage = 0; + return; + } + else if (m_colorCodeStage == 2) + { + if (ch == ']') + { + String color = m_incomingColorName.to_lowercase(); + + for (size_t i = 0; i < countof(colorCodes); ++i) + { + const ColorCodeInfo& colorInfo = colorCodes[i]; + + if (String(colorInfo.name).to_lowercase() == color) + { + activate_color(colorInfo.color, colorInfo.bold); + m_colorCodeStage = 0; + break; + } + } + + m_incomingColorName = ""; + m_colorCodeStage = 0; + } + else if (isprint(ch)) + m_incomingColorName += ch; + return; } @@ -135,6 +160,22 @@ // ------------------------------------------------------------------------------------------------- // +void ColoredLine::activate_color (Color color, bool bold) +{ + if (m_boldActive) + m_data << RLINE_OFF_BOLD; + + m_activeColor = color; + m_boldActive = bold; + assert (m_activeColor < 8); + set_color (m_activeColor, true); + + if (m_boldActive) + m_data << RLINE_ON_BOLD; +} + +// ------------------------------------------------------------------------------------------------- +// void ColoredLine::set_color (Color a, bool on) { assert (a < 8); diff -r c062273efa33 -r b3a33bc2e482 sources/coloredline.h --- a/sources/coloredline.h Sun Jan 10 20:05:27 2016 +0200 +++ b/sources/coloredline.h Sun Jan 10 20:53:44 2016 +0200 @@ -73,6 +73,7 @@ int rows (int cols) const; private: + void activate_color (Color color, bool bold); void set_color (Color a, bool on); Vector m_data; @@ -82,6 +83,7 @@ bool m_boldActive; int m_colorCodeStage; String m_string; + String m_incomingColorName; }; END_ZFC_NAMESPACE