summary refs log tree commit diff
path: root/ui.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-01 21:57:11 -0500
committerJune McEnroe <june@causal.agency>2020-02-01 21:57:11 -0500
commitcd3dc4ef4caaad3a696ad731c197f50105119b31 (patch)
tree6ccf332a263c06236c654ad9b6f00347e3410107 /ui.c
parentGenerate tags file (diff)
downloadcatgirl-cd3dc4ef4caaad3a696ad731c197f50105119b31.tar.gz
catgirl-cd3dc4ef4caaad3a696ad731c197f50105119b31.zip
Parse IRC styling in UI
Wow the colorPair thing actually works. Have I finally cracked curses
colors?
Diffstat (limited to '')
-rw-r--r--ui.c75
1 files changed, 73 insertions, 2 deletions
diff --git a/ui.c b/ui.c
index 0295c8d..83c4bc7 100644
--- a/ui.c
+++ b/ui.c
@@ -15,12 +15,14 @@
  */
 
 #include <assert.h>
+#include <ctype.h>
 #include <curses.h>
 #include <err.h>
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sysexits.h>
 #include <time.h>
 
@@ -56,7 +58,7 @@ static short colorPair(short fg, short bg) {
 		pair_content(pair, &f, &b);
 		if (f == fg && b == bg) return pair;
 	}
-	init_pair(colorPairs, fg, bg);
+	init_pair(colorPairs, fg % COLORS, bg % COLORS);
 	return colorPairs++;
 }
 
@@ -154,11 +156,80 @@ void uiDraw(void) {
 	doupdate();
 }
 
+struct Style {
+	attr_t attr;
+	enum Color fg, bg;
+};
+static const struct Style Reset = { A_NORMAL, Default, Default };
+
+static short mapColor(enum Color color) {
+	switch (color) {
+		break; case White:      return 8 + COLOR_WHITE;
+		break; case Black:      return 0 + COLOR_BLACK;
+		break; case Blue:       return 0 + COLOR_BLUE;
+		break; case Green:      return 0 + COLOR_GREEN;
+		break; case Red:        return 8 + COLOR_RED;
+		break; case Brown:      return 0 + COLOR_RED;
+		break; case Magenta:    return 0 + COLOR_MAGENTA;
+		break; case Orange:     return 0 + COLOR_YELLOW;
+		break; case Yellow:     return 8 + COLOR_YELLOW;
+		break; case LightGreen: return 8 + COLOR_GREEN;
+		break; case Cyan:       return 0 + COLOR_CYAN;
+		break; case LightCyan:  return 8 + COLOR_CYAN;
+		break; case LightBlue:  return 8 + COLOR_BLUE;
+		break; case Pink:       return 8 + COLOR_MAGENTA;
+		break; case Gray:       return 8 + COLOR_BLACK;
+		break; case LightGray:  return 0 + COLOR_WHITE;
+		break; default:         return -1;
+	}
+}
+
+static void styleParse(struct Style *style, const char **str, size_t *len) {
+	switch (**str) {
+		break; case '\2': (*str)++; style->attr ^= A_BOLD;
+		break; case '\17': (*str)++; *style = Reset;
+		break; case '\26': (*str)++; style->attr ^= A_REVERSE;
+		break; case '\35': (*str)++; style->attr ^= A_ITALIC;
+		break; case '\37': (*str)++; style->attr ^= A_UNDERLINE;
+		break; case '\3': {
+			(*str)++;
+			if (!isdigit(**str)) {
+				style->fg = Default;
+				style->bg = Default;
+				break;
+			}
+			style->fg = *(*str)++ - '0';
+			if (isdigit(**str)) style->fg = style->fg * 10 + *(*str)++ - '0';
+			if ((*str)[0] != ',' || !isdigit((*str)[1])) break;
+			(*str)++;
+			style->bg = *(*str)++ - '0';
+			if (isdigit(**str)) style->bg = style->bg * 10 + *(*str)++ - '0';
+		}
+	}
+	*len = strcspn(*str, "\2\3\17\26\35\37");
+}
+
+static void styleAdd(WINDOW *win, const char *str) {
+	size_t len;
+	struct Style style = Reset;
+	while (*str) {
+		styleParse(&style, &str, &len);
+		wattr_set(
+			win,
+			style.attr | colorAttr(mapColor(style.fg)),
+			colorPair(mapColor(style.fg), mapColor(style.bg)),
+			NULL
+		);
+		waddnstr(win, str, len);
+		str += len;
+	}
+}
+
 void uiWrite(size_t id, enum Heat heat, const struct tm *time, const char *str) {
 	(void)time;
 	struct Window *window = windowFor(id);
 	waddch(window->pad, '\n');
-	waddstr(window->pad, str);
+	styleAdd(window->pad, str);
 }
 
 void uiFormat(
eader'>2017-08-25Install ddateJune McEnroe 2017-08-19Move nethack options to envJune McEnroe 2017-08-17Remove scala syntax fileJune McEnroe 2017-08-06Add wakeJune McEnroe That payload can appear anywhere within an ethernet frame. Wake-on-LAN is funny. 2017-08-04Use $() in install.shJune McEnroe Something gave me the impression that sh did not like this, but it's specified by POSIX. 2017-08-02Create Code Tarmak 3 layoutJune McEnroe 2017-07-31Add tupJune McEnroe 2017-07-31Use designated initializer for hnel tableJune McEnroe I did not know this syntax worked! 2017-07-30Add juneJune McEnroe 2017-07-30Play nethack as ValkyrieJune McEnroe 2017-07-28Add toggle to hnelJune McEnroe 2017-07-28Install slJune McEnroe 2017-07-25Add up, supJune McEnroe 2017-07-24Autopickup ringsJune McEnroe 2017-07-24Name dogJune McEnroe 2017-07-23Add nethackrcJune McEnroe 2017-07-23Remove useless setuid in briJune McEnroe Don't you think it would be better if the setuid bit only gave you permission to do it and didn't do it for you? 2017-07-23Clean up hnel a tiny bitJune McEnroe 2017-07-21Set window size in hnelJune McEnroe 2017-07-21Add hnelJune McEnroe 2017-07-19chmod 600 in dtchJune McEnroe