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.
master
C. McEnroe 2021-10-28 17:50:45 -04:00
parent 27b5fd0251
commit 4363d4b535
1 changed files with 10 additions and 12 deletions

22
ui.c
View File

@ -276,10 +276,12 @@ void uiInitEarly(void) {
if (!main) err(EX_OSERR, "newwin"); if (!main) err(EX_OSERR, "newwin");
int y; int y;
char fmt[TimeCap];
char buf[TimeCap]; char buf[TimeCap];
styleStrip(fmt, sizeof(fmt), uiTime.format);
struct tm *time = localtime(&(time_t) { -22100400 }); struct tm *time = localtime(&(time_t) { -22100400 });
size_t len = strftime(buf, sizeof(buf), uiTime.format, time); size_t len = strftime(buf, sizeof(buf), fmt, time);
if (!len) errx(EX_CONFIG, "invalid timestamp format: %s", uiTime.format); if (!len) errx(EX_CONFIG, "invalid timestamp format: %s", fmt);
waddstr(main, buf); waddstr(main, buf);
waddch(main, ' '); waddch(main, ' ');
getyx(main, y, uiTime.width); getyx(main, y, uiTime.width);
@ -386,8 +388,8 @@ static short stylePair(struct Style style) {
return colorPair(Colors[style.fg], Colors[style.bg]); return colorPair(Colors[style.fg], Colors[style.bg]);
} }
static int styleAdd(WINDOW *win, const char *str) { static int styleAdd(WINDOW *win, struct Style init, const char *str) {
struct Style style = StyleDefault; struct Style style = init;
while (*str) { while (*str) {
size_t len = styleParse(&style, &str); size_t len = styleParse(&style, &str);
wattr_set(win, styleAttr(style), stylePair(style), NULL); wattr_set(win, styleAttr(style), stylePair(style), NULL);
@ -433,7 +435,7 @@ static void statusUpdate(void) {
if (window->scroll) { if (window->scroll) {
ptr = seprintf(ptr, end, "~%d ", window->scroll); ptr = seprintf(ptr, end, "~%d ", window->scroll);
} }
if (styleAdd(status, buf) < 0) break; if (styleAdd(status, StyleDefault, buf) < 0) break;
} }
wclrtoeol(status); wclrtoeol(status);
@ -514,18 +516,14 @@ static void mainAdd(int y, bool time, const struct Line *line) {
if (time && line->time) { if (time && line->time) {
char buf[TimeCap]; char buf[TimeCap];
strftime(buf, sizeof(buf), uiTime.format, localtime(&line->time)); strftime(buf, sizeof(buf), uiTime.format, localtime(&line->time));
wattr_set( struct Style init = { .fg = Gray, .bg = Default };
main, styleAdd(main, init, buf);
colorAttr(Colors[Gray]), colorPair(Colors[Gray], -1),
NULL
);
waddstr(main, buf);
waddch(main, ' '); waddch(main, ' ');
} else if (time) { } else if (time) {
whline(main, ' ', uiTime.width); whline(main, ' ', uiTime.width);
wmove(main, y, uiTime.width); wmove(main, y, uiTime.width);
} }
styleAdd(main, line->str); styleAdd(main, StyleDefault, line->str);
getyx(main, ny, nx); getyx(main, ny, nx);
if (ny != y) return; if (ny != y) return;
wclrtoeol(main); wclrtoeol(main);