colornames.diff (2293B)
1 cio.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 1 file changed, 43 insertions(+), 3 deletions(-) 3 4 diff --git a/cio.c b/cio.c 5 index d0577a8..502506e 100644 6 --- a/cio.c 7 +++ b/cio.c 8 @@ -51,8 +51,8 @@ 9 #define INDENT 23 10 #define TABSTOP 8 11 #define DATEFMT "%H:%M" 12 -#define PFMT " %-12s < %s" 13 -#define PFMTHIGH "> %-12s < %s" 14 +#define PFMT " \x03%-12s\x03 < %s" 15 +#define PFMTHIGH "> \x03%-12s\x03 < %s" 16 #define SRV "irc.oftc.net" 17 #define PORT "6697" 18 19 @@ -164,6 +164,14 @@ empty(const char *str) { 20 return (str == NULL || str[0] == '\0'); 21 } 22 23 +static int 24 +nickhash(const char *s) 25 +{ 26 + unsigned int h = 5381; 27 + while (*s != '\x03') h = ((h << 5) + h) + (*s++); 28 + return (h % 6) + 2; /* Use pairs 2-7 */ 29 +} 30 + 31 static void 32 sndf(const char *fmt, ...) 33 { 34 @@ -375,6 +383,10 @@ pushl(struct Chan *c, char *p, char *e) 35 mbtowc(NULL, NULL, 0); 36 while (p < eol) { 37 int n = mbtowc(&wc, p, eol - p); 38 + if (wc == L'\x03') { 39 + p += n; 40 + continue; 41 + } 42 if (n <= 0) { wc = L'?'; n = 1; } 43 if (iswcntrl(wc)) { 44 p += n; 45 @@ -669,6 +681,12 @@ tinit(void) 46 start_color(); 47 use_default_colors(); 48 init_pair(1, COLOR_WHITE, COLOR_BLACK); 49 + init_pair(2, COLOR_RED, -1); 50 + init_pair(3, COLOR_GREEN, -1); 51 + init_pair(4, COLOR_YELLOW, -1); 52 + init_pair(5, COLOR_BLUE, -1); 53 + init_pair(6, COLOR_MAGENTA, -1); 54 + init_pair(7, COLOR_CYAN, -1); 55 wbkgd(scr.sw, COLOR_PAIR(1)); 56 } 57 } 58 @@ -722,7 +740,29 @@ tredraw(void) 59 for (int i = offset; i < (int)c->vis_n && i < offset + height; i++) { 60 if (c->vis[i].cont) 61 wmove(scr.mw, getcury(scr.mw), INDENT); 62 - waddnstr(scr.mw, c->buf + c->vis[i].offset, c->vis[i].len); 63 + char *line_ptr = c->buf + c->vis[i].offset; 64 + size_t remaining = c->vis[i].len; 65 + int color = 0; 66 + 67 + while (remaining > 0) { 68 + char *ctrl = memchr(line_ptr, '\x03', remaining); 69 + size_t chunk = ctrl ? (ctrl - line_ptr) : remaining; 70 + if (chunk > 0) 71 + waddnstr(scr.mw, line_ptr, chunk); 72 + if (ctrl) { 73 + if (color) { 74 + wattroff(scr.mw, COLOR_PAIR(color)); 75 + color = 0; 76 + } else { 77 + color = nickhash(ctrl + 1); 78 + wattron(scr.mw, COLOR_PAIR(color)); 79 + } 80 + chunk += 1; 81 + } 82 + line_ptr += chunk; 83 + remaining -= chunk; 84 + } 85 + 86 waddch(scr.mw, '\n'); 87 } 88 wnoutrefresh(scr.mw);