sources/coloredline.cpp

branch
protocol5
changeset 150
37db42ad451a
parent 141
d9073c13dc98
parent 145
d0aedc9be448
child 195
be953e1621d9
equal deleted inserted replaced
141:d9073c13dc98 150:37db42ad451a
37 Color color; 37 Color color;
38 bool bold; 38 bool bold;
39 }; 39 };
40 40
41 ColoredLine::ColoredLine() : 41 ColoredLine::ColoredLine() :
42 m_length (0), 42 m_length(0),
43 m_final (false), 43 m_final(false),
44 m_activeColor (DEFAULT), 44 m_activeColor(DEFAULT),
45 m_boldActive (false), 45 m_boldActive(false),
46 m_colorCodeStage (0) {} 46 m_colorCodeStage(0) {}
47 47
48 // ------------------------------------------------------------------------------------------------- 48 // -------------------------------------------------------------------------------------------------
49 // 49 //
50 void ColoredLine::finalize() 50 void ColoredLine::finalize()
51 { 51 {
52 if (m_activeColor != DEFAULT) 52 if (m_activeColor != DEFAULT)
53 set_color (m_activeColor, false); 53 setColor(m_activeColor, false);
54 54
55 if (m_boldActive) 55 if (m_boldActive)
56 m_data << RLINE_OFF_BOLD; 56 m_data << RLINE_OFF_BOLD;
57 57
58 m_final = true; 58 m_final = true;
59 } 59 }
60 60
61 // ------------------------------------------------------------------------------------------------- 61 // -------------------------------------------------------------------------------------------------
62 // 62 //
63 void ColoredLine::add_char (char ch) 63 void ColoredLine::addChar(char ch)
64 { 64 {
65 static const ColorCodeInfo colorCodes[] = 65 static const ColorCodeInfo colorCodes[] =
66 { 66 {
67 { "Brick", RED, true }, // a 67 { "Brick", RED, true }, // a
68 { "Tan", YELLOW, true }, // b 68 { "Tan", YELLOW, true }, // b
98 } 98 }
99 99
100 if (m_colorCodeStage == 1) 100 if (m_colorCodeStage == 1)
101 { 101 {
102 if (m_activeColor != DEFAULT) 102 if (m_activeColor != DEFAULT)
103 set_color (m_activeColor, false); 103 setColor(m_activeColor, false);
104 104
105 if (m_boldActive) 105 if (m_boldActive)
106 m_data << RLINE_OFF_BOLD; 106 m_data << RLINE_OFF_BOLD;
107 107
108 m_boldActive = false; 108 m_boldActive = false;
112 ch += 'a' - 'A'; 112 ch += 'a' - 'A';
113 113
114 if (ch >= 'a' and ch <= 'v' and ch != 'l') 114 if (ch >= 'a' and ch <= 'v' and ch != 'l')
115 { 115 {
116 const ColorCodeInfo& colorInfo = colorCodes[ch - 'a']; 116 const ColorCodeInfo& colorInfo = colorCodes[ch - 'a'];
117 activate_color(colorInfo.color, colorInfo.bold); 117 activateColor(colorInfo.color, colorInfo.bold);
118 } 118 }
119 119
120 if (ch == '[') 120 if (ch == '[')
121 m_colorCodeStage = 2; 121 m_colorCodeStage = 2;
122 else 122 else
125 } 125 }
126 else if (m_colorCodeStage == 2) 126 else if (m_colorCodeStage == 2)
127 { 127 {
128 if (ch == ']') 128 if (ch == ']')
129 { 129 {
130 String color = m_incomingColorName.to_lowercase(); 130 String color = m_incomingColorName.toLowerCase();
131 131
132 for (const ColorCodeInfo &colorInfo : colorCodes) 132 for (const ColorCodeInfo &colorInfo : colorCodes)
133 { 133 {
134 if (String(colorInfo.name).to_lowercase() == color) 134 if (String(colorInfo.name).toLowerCase() == color)
135 { 135 {
136 activate_color(colorInfo.color, colorInfo.bold); 136 activateColor(colorInfo.color, colorInfo.bold);
137 m_colorCodeStage = 0; 137 m_colorCodeStage = 0;
138 break; 138 break;
139 } 139 }
140 } 140 }
141 141
146 m_incomingColorName += ch; 146 m_incomingColorName += ch;
147 147
148 return; 148 return;
149 } 149 }
150 150
151 if (isprint (ch)) 151 if (isprint(ch))
152 { 152 {
153 m_string += ch; 153 m_string += ch;
154 m_data << int (ch); 154 m_data << int(ch);
155 ++m_length; 155 ++m_length;
156 } 156 }
157 } 157 }
158 158
159 // ------------------------------------------------------------------------------------------------- 159 // -------------------------------------------------------------------------------------------------
160 // 160 //
161 void ColoredLine::activate_color (Color color, bool bold) 161 void ColoredLine::activateColor(Color color, bool bold)
162 { 162 {
163 if (m_boldActive) 163 if (m_boldActive)
164 m_data << RLINE_OFF_BOLD; 164 m_data << RLINE_OFF_BOLD;
165 165
166 m_activeColor = color; 166 m_activeColor = color;
167 m_boldActive = bold; 167 m_boldActive = bold;
168 assert (m_activeColor < 8); 168 assert(m_activeColor < 8);
169 set_color (m_activeColor, true); 169 setColor(m_activeColor, true);
170 170
171 if (m_boldActive) 171 if (m_boldActive)
172 m_data << RLINE_ON_BOLD; 172 m_data << RLINE_ON_BOLD;
173 } 173 }
174 174
175 // ------------------------------------------------------------------------------------------------- 175 // -------------------------------------------------------------------------------------------------
176 // 176 //
177 void ColoredLine::add_string (const String& text) 177 void ColoredLine::addString(const String& text)
178 { 178 {
179 for (char a : text) 179 for (char a : text)
180 add_char (a); 180 addChar(a);
181 } 181 }
182 182
183 // ------------------------------------------------------------------------------------------------- 183 // -------------------------------------------------------------------------------------------------
184 // 184 //
185 void ColoredLine::set_color (Color a, bool on) 185 void ColoredLine::setColor(Color a, bool on)
186 { 186 {
187 assert (a < 8); 187 assert(a < 8);
188 m_data << (a + (on ? RLINE_ON_COLOR : RLINE_OFF_COLOR)); 188 m_data << (a +(on ? RLINE_ON_COLOR : RLINE_OFF_COLOR));
189 } 189 }
190 190
191 // ------------------------------------------------------------------------------------------------- 191 // -------------------------------------------------------------------------------------------------
192 // How many rows does this line take up? 192 // How many rows does this line take up?
193 // 193 //
194 int ColoredLine::rows (int cols) const 194 int ColoredLine::rows(int cols) const
195 { 195 {
196 int rows = length() / cols; 196 int rows = length() / cols;
197 197
198 if (length() % cols != 0) 198 if (length() % cols != 0)
199 rows++; 199 rows++;
200 200
201 return max (rows, 1); 201 return max(rows, 1);
202 } 202 }
203 203
204 END_ZFC_NAMESPACE 204 END_ZFC_NAMESPACE

mercurial