sources/coloredline.cpp

branch
protocol5
changeset 131
4996c8684b93
parent 106
7b156b764d11
parent 113
b3a33bc2e482
child 133
4d8fa5394d67
equal deleted inserted replaced
130:9f54db6f9922 131:4996c8684b93
1 /* 1 /*
2 Copyright 2014, 2015 Teemu Piippo 2 Copyright 2014 - 2016 Teemu Piippo
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions 6 modification, are permitted provided that the following conditions
7 are met: 7 are met:
103 set_color (m_activeColor, false); 103 set_color (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;
109
108 // Chars may be in uppercase 110 // Chars may be in uppercase
109 if (ch >= 'A' and ch <= 'V') 111 if (ch >= 'A' and ch <= 'V')
110 ch += 'a' - 'A'; 112 ch += 'a' - 'A';
111 113
112 if (ch >= 'a' and ch <= 'v' and ch != 'l') 114 if (ch >= 'a' and ch <= 'v' and ch != 'l')
113 { 115 {
114 auto colorInfo = colorCodes[ch - 'a']; 116 const ColorCodeInfo& colorInfo = colorCodes[ch - 'a'];
115 m_activeColor = colorInfo.color; 117 activate_color(colorInfo.color, colorInfo.bold);
116 m_boldActive = colorInfo.bold;
117 assert (m_activeColor < 8);
118 set_color (m_activeColor, true);
119
120 if (m_boldActive)
121 m_data << RLINE_ON_BOLD;
122 } 118 }
123 119
124 m_colorCodeStage = 0; 120 if (ch == '[')
121 m_colorCodeStage = 2;
122 else
123 m_colorCodeStage = 0;
124 return;
125 }
126 else if (m_colorCodeStage == 2)
127 {
128 if (ch == ']')
129 {
130 String color = m_incomingColorName.to_lowercase();
131
132 for (size_t i = 0; i < countof(colorCodes); ++i)
133 {
134 const ColorCodeInfo& colorInfo = colorCodes[i];
135
136 if (String(colorInfo.name).to_lowercase() == color)
137 {
138 activate_color(colorInfo.color, colorInfo.bold);
139 m_colorCodeStage = 0;
140 break;
141 }
142 }
143
144 m_incomingColorName = "";
145 m_colorCodeStage = 0;
146 }
147 else if (isprint(ch))
148 m_incomingColorName += ch;
149
125 return; 150 return;
126 } 151 }
127 152
128 if (isprint (ch)) 153 if (isprint (ch))
129 { 154 {
141 add_char (a); 166 add_char (a);
142 } 167 }
143 168
144 // ------------------------------------------------------------------------------------------------- 169 // -------------------------------------------------------------------------------------------------
145 // 170 //
171 void ColoredLine::activate_color (Color color, bool bold)
172 {
173 if (m_boldActive)
174 m_data << RLINE_OFF_BOLD;
175
176 m_activeColor = color;
177 m_boldActive = bold;
178 assert (m_activeColor < 8);
179 set_color (m_activeColor, true);
180
181 if (m_boldActive)
182 m_data << RLINE_ON_BOLD;
183 }
184
185 // -------------------------------------------------------------------------------------------------
186 //
146 void ColoredLine::set_color (Color a, bool on) 187 void ColoredLine::set_color (Color a, bool on)
147 { 188 {
148 assert (a < 8); 189 assert (a < 8);
149 m_data << (a + (on ? RLINE_ON_COLOR : RLINE_OFF_COLOR)); 190 m_data << (a + (on ? RLINE_ON_COLOR : RLINE_OFF_COLOR));
150 } 191 }

mercurial