comparison mcabber/src/screen.c @ 1272:033576acac4c

Add configurable roster colors (Michal 'vorner' Vaner)
author Mikael Berthe <mikael@lilotux.net>
date Tue, 21 Aug 2007 20:49:03 +0200
parents dbc907b2d92f
children 8bbc764139ef
comparison
equal deleted inserted replaced
1271:f8a874ff453a 1272:033576acac4c
141 #define ASPELLBADCHAR 5 141 #define ASPELLBADCHAR 5
142 AspellConfig *spell_config; 142 AspellConfig *spell_config;
143 AspellSpeller *spell_checker; 143 AspellSpeller *spell_checker;
144 #endif 144 #endif
145 145
146 typedef struct {
147 char *status, *wildcard;
148 int color;
149 GPatternSpec *compiled;
150 } rostercolor;
151
152 GSList *rostercolrules = NULL;
153
146 /* Functions */ 154 /* Functions */
147 155
148 static int FindColor(const char *name) 156 static int color_conv_table[] = {
157 COLOR_BLACK,
158 COLOR_RED,
159 COLOR_GREEN,
160 COLOR_YELLOW,
161 COLOR_BLUE,
162 COLOR_MAGENTA,
163 COLOR_CYAN,
164 COLOR_WHITE
165 };
166
167 static int color_conv_table_fg[] = {
168 COLOR_BLACK_FG,
169 COLOR_RED_FG,
170 COLOR_GREEN_FG,
171 COLOR_YELLOW_FG,
172 COLOR_BLUE_FG,
173 COLOR_MAGENTA_FG,
174 COLOR_CYAN_FG,
175 COLOR_WHITE_FG
176 };
177
178 static int color_to_color_fg(int color)
179 {
180 unsigned i = 0;
181 for ( ; i < sizeof color_conv_table / sizeof *color_conv_table; i++)
182 if (color == color_conv_table[i])
183 return color_conv_table_fg[i];
184 return -1;
185 }
186
187 static int color_fg_to_color(int color)
188 {
189 unsigned i = 0;
190 if (color >= COLOR_BLACK_BOLD_FG)
191 color -= COLOR_BLACK_BOLD_FG - COLOR_BLACK_FG;
192 for ( ; i < sizeof color_conv_table_fg / sizeof *color_conv_table_fg; i++)
193 if (color == color_conv_table_fg[i])
194 return color_conv_table[i];
195 return -1;
196 }
197
198 static int FindColorInternal(const char *name)
149 { 199 {
150 if (!strcmp(name, "default")) 200 if (!strcmp(name, "default"))
151 return -1; 201 return -1;
152 if (!strcmp(name, "black")) 202 if (!strcmp(name, "black"))
153 return COLOR_BLACK; 203 return COLOR_BLACK;
164 if (!strcmp(name, "cyan")) 214 if (!strcmp(name, "cyan"))
165 return COLOR_CYAN; 215 return COLOR_CYAN;
166 if (!strcmp(name, "white")) 216 if (!strcmp(name, "white"))
167 return COLOR_WHITE; 217 return COLOR_WHITE;
168 218
219 return -2;
220 }
221
222 static int FindColor(const char *name)
223 {
224 int result = FindColorInternal(name);
225 if (result != -2)
226 return result;
227
169 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Wrong color: %s", name); 228 scr_LogPrint(LPRINT_LOGNORM, "ERROR: Wrong color: %s", name);
170 return -1; 229 return -1;
230 }
231
232 static void free_rostercolrule(rostercolor *col)
233 {
234 g_free(col->status);
235 g_free(col->wildcard);
236 g_pattern_spec_free(col->compiled);
237 g_free(col);
238 }
239
240 void scr_RosterClearColor(void)
241 {
242 GSList *head;
243 for (head = rostercolrules; head; head = g_slist_next(head)) {
244 free_rostercolrule(head->data);
245 }
246 g_slist_free(rostercolrules);
247 rostercolrules = NULL;
248 }
249
250 bool scr_RosterColor(const char *status, const char *wildcard,
251 const char *color)
252 {
253 GSList *head;
254 GSList *found = NULL;
255 for (head = rostercolrules; head; head = g_slist_next(head)) {
256 rostercolor *rc = head->data;
257 if ((!strcmp(status, rc->status)) && (!strcmp(wildcard, rc->wildcard))) {
258 found = head;
259 break;
260 }
261 }
262 if (!strcmp(color,"-")) {//Delete the rule
263 if (found) {
264 free_rostercolrule(found->data);
265 rostercolrules = g_slist_delete_link(rostercolrules, found);
266 return TRUE;
267 } else {
268 scr_LogPrint(LPRINT_NORMAL, "No such color rule, nothing removed");
269 return FALSE;
270 }
271 } else {
272 bool isbright = false;
273 int cl;
274 if (!strncmp(color, "bright", 6)) {
275 isbright = true;
276 color += 6;
277 }
278 cl = color_to_color_fg(FindColorInternal(color));
279 if (isbright)
280 cl += COLOR_BLACK_BOLD_FG - COLOR_BLACK_FG;
281 if (cl < 0 ) {
282 scr_LogPrint(LPRINT_NORMAL, "No such color name");
283 return FALSE;
284 }
285 if (found) {
286 rostercolor *rc = found->data;
287 rc->color = cl;
288 } else {
289 rostercolor *rc = g_new(rostercolor, 1);
290 rc->status = g_strdup(status);
291 rc->wildcard = g_strdup(wildcard);
292 rc->compiled = g_pattern_spec_new(wildcard);
293 rc->color = cl;
294 rostercolrules = g_slist_prepend(rostercolrules, rc);
295 }
296 return TRUE;
297 }
171 } 298 }
172 299
173 static void ParseColors(void) 300 static void ParseColors(void)
174 { 301 {
175 const char *colors[] = { 302 const char *colors[] = {
260 case COLOR_MSGIN: 387 case COLOR_MSGIN:
261 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE), 388 init_pair(i+1, ((color) ? FindColor(color) : COLOR_WHITE),
262 FindColor(background)); 389 FindColor(background));
263 break; 390 break;
264 } 391 }
392 }
393 for (i = COLOR_BLACK_FG; i < COLOR_max; i++) {
394 init_pair(i, color_fg_to_color(i), FindColor(background));
395 if (i >= COLOR_BLACK_BOLD_FG)
396 COLOR_ATTRIB[i] = A_BOLD;
265 } 397 }
266 } 398 }
267 399
268 static void init_keycodes(void) 400 static void init_keycodes(void)
269 { 401 {
1427 for (n = 0; n < maxx; n++) 1559 for (n = 0; n < maxx; n++)
1428 waddch(rosterWnd, ' '); 1560 waddch(rosterWnd, ' ');
1429 } else { 1561 } else {
1430 if (pending == '#') 1562 if (pending == '#')
1431 wattrset(rosterWnd, get_color(COLOR_ROSTERNMSG)); 1563 wattrset(rosterWnd, get_color(COLOR_ROSTERNMSG));
1432 else 1564 else {
1433 wattrset(rosterWnd, get_color(COLOR_ROSTER)); 1565 int color = get_color(COLOR_ROSTER);
1566 if ((!isspe) && (!isgrp)) {//Look for color rules
1567 GSList *head;
1568 const char *jid = buddy_getjid(BUDDATA(buddy));
1569 for (head = rostercolrules; head; head = g_slist_next(head)) {
1570 rostercolor *rc = head->data;
1571 if (g_pattern_match_string(rc->compiled, jid) &&
1572 (!strcmp("*", rc->status) || strchr(rc->status, status))) {
1573 color = get_color(rc->color);
1574 break;
1575 }
1576 }
1577 }
1578 wattrset(rosterWnd, color);
1579 }
1434 } 1580 }
1435 1581
1436 if (Roster_Width > 7) 1582 if (Roster_Width > 7)
1437 g_utf8_strncpy(name, buddy_getname(BUDDATA(buddy)), Roster_Width-7); 1583 g_utf8_strncpy(name, buddy_getname(BUDDATA(buddy)), Roster_Width-7);
1438 else 1584 else