summary refs log tree commit diff
path: root/ui.c
diff options
context:
space:
mode:
authorC. McEnroe2021-10-28 17:50:45 -0400
committerC. McEnroe2021-10-28 17:56:16 -0400
commit4363d4b53505708608b1a82f8bff45d7aefe18dc (patch)
tree6e61b5432d8eba3c824a27c89d57dd4d40cb3004 /ui.c
parent27b5fd0251276fce156ef066f11086cfba941804 (diff)
Parse IRC formatting in timestamp string
Strip formatting when calculating the timestamp width to avoid
moving a bunch of code around. Use styleAdd (now with an initial
style parameter) to show timestamps.

This allows changing the style of the timestamps from the default
gray using literal IRC formatting codes in the string. Not ideal,
but no new options needed.

Suggested by Hoël Bézier and Sebastian LaVine.
Diffstat (limited to 'ui.c')
-rw-r--r--ui.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/ui.c b/ui.c
index 1adcafe..2789bd7 100644
--- a/ui.c
+++ b/ui.c
@@ -276,10 +276,12 @@ void uiInitEarly(void) {
 	if (!main) err(EX_OSERR, "newwin");
 
 	int y;
+	char fmt[TimeCap];
 	char buf[TimeCap];
+	styleStrip(fmt, sizeof(fmt), uiTime.format);
 	struct tm *time = localtime(&(time_t) { -22100400 });
-	size_t len = strftime(buf, sizeof(buf), uiTime.format, time);
-	if (!len) errx(EX_CONFIG, "invalid timestamp format: %s", uiTime.format);
+	size_t len = strftime(buf, sizeof(buf), fmt, time);
+	if (!len) errx(EX_CONFIG, "invalid timestamp format: %s", fmt);
 	waddstr(main, buf);
 	waddch(main, ' ');
 	getyx(main, y, uiTime.width);
@@ -386,8 +388,8 @@ static short stylePair(struct Style style) {
 	return colorPair(Colors[style.fg], Colors[style.bg]);
 }
 
-static int styleAdd(WINDOW *win, const char *str) {
-	struct Style style = StyleDefault;
+static int styleAdd(WINDOW *win, struct Style init, const char *str) {
+	struct Style style = init;
 	while (*str) {
 		size_t len = styleParse(&style, &str);
 		wattr_set(win, styleAttr(style), stylePair(style), NULL);
@@ -433,7 +435,7 @@ static void statusUpdate(void) {
 		if (window->scroll) {
 			ptr = seprintf(ptr, end, "~%d ", window->scroll);
 		}
-		if (styleAdd(status, buf) < 0) break;
+		if (styleAdd(status, StyleDefault, buf) < 0) break;
 	}
 	wclrtoeol(status);
 
@@ -514,18 +516,14 @@ static void mainAdd(int y, bool time, const struct Line *line) {
 	if (time && line->time) {
 		char buf[TimeCap];
 		strftime(buf, sizeof(buf), uiTime.format, localtime(&line->time));
-		wattr_set(
-			main,
-			colorAttr(Colors[Gray]), colorPair(Colors[Gray], -1),
-			NULL
-		);
-		waddstr(main, buf);
+		struct Style init = { .fg = Gray, .bg = Default };
+		styleAdd(main, init, buf);
 		waddch(main, ' ');
 	} else if (time) {
 		whline(main, ' ', uiTime.width);
 		wmove(main, y, uiTime.width);
 	}
-	styleAdd(main, line->str);
+	styleAdd(main, StyleDefault, line->str);
 	getyx(main, ny, nx);
 	if (ny != y) return;
 	wclrtoeol(main);