Added support for long-form color codes, such as \c[Green]

Sun, 10 Jan 2016 20:53:44 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 10 Jan 2016 20:53:44 +0200
changeset 113
b3a33bc2e482
parent 112
c062273efa33
child 114
0e7f3ecdf65a

Added support for long-form color codes, such as \c[Green]

sources/basics.h file | annotate | diff | comparison | revisions
sources/coloredline.cpp file | annotate | diff | comparison | revisions
sources/coloredline.h file | annotate | diff | comparison | revisions
--- 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 <typename T, size_t N>
+char (&_ArraySizeHelper(T (&array)[N]))[N];
+#define countof(array) (sizeof(_ArraySizeHelper( array )))
+
 struct Exitception {};
 
 END_ZFC_NAMESPACE
--- 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);
--- 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<int> m_data;
@@ -82,6 +83,7 @@
 	bool m_boldActive;
 	int m_colorCodeStage;
 	String m_string;
+	String m_incomingColorName;
 };
 
 END_ZFC_NAMESPACE

mercurial