Measure timestamp width using ncurses

This allows for non-ASCII characters in timestamps, and simplifies
things by including the trailing space in the width.
master
C. McEnroe 2021-01-27 14:45:03 -05:00
parent d6ff9e53cf
commit b4c26a2cab
2 changed files with 16 additions and 14 deletions

2
chat.h
View File

@ -283,7 +283,7 @@ enum { TimeCap = 64 };
extern struct Time { extern struct Time {
bool enable; bool enable;
const char *format; const char *format;
size_t width; int width;
} uiTime; } uiTime;
extern struct Util uiNotifyUtil; extern struct Util uiNotifyUtil;
void uiInitEarly(void); void uiInitEarly(void);

28
ui.c
View File

@ -236,13 +236,6 @@ static void errExit(void) {
} }
void uiInitEarly(void) { void uiInitEarly(void) {
char buf[TimeCap];
struct tm *time = localtime(&(time_t) { -22100400 });
uiTime.width = strftime(buf, sizeof(buf), uiTime.format, time);
if (!uiTime.width) {
errx(EX_CONFIG, "invalid timestamp format: %s", uiTime.format);
}
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
@ -264,6 +257,16 @@ void uiInitEarly(void) {
main = newwin(MAIN_LINES, COLS, StatusLines, 0); main = newwin(MAIN_LINES, COLS, StatusLines, 0);
if (!main) err(EX_OSERR, "newwin"); if (!main) err(EX_OSERR, "newwin");
int y;
char buf[TimeCap];
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);
waddstr(main, buf);
waddch(main, ' ');
getyx(main, y, uiTime.width);
(void)y;
input = newpad(InputLines, InputCols); input = newpad(InputLines, InputCols);
if (!input) err(EX_OSERR, "newpad"); if (!input) err(EX_OSERR, "newpad");
keypad(input, true); keypad(input, true);
@ -476,8 +479,7 @@ static size_t windowBottom(const struct Window *window) {
} }
static int windowCols(const struct Window *window) { static int windowCols(const struct Window *window) {
if (!window->time) return COLS; return COLS - (window->time ? uiTime.width : 0);
return COLS - (uiTime.width + 1);
} }
static void mainAdd(int y, bool time, const struct Line *line) { static void mainAdd(int y, bool time, const struct Line *line) {
@ -498,8 +500,8 @@ static void mainAdd(int y, bool time, const struct Line *line) {
waddstr(main, buf); waddstr(main, buf);
waddch(main, ' '); waddch(main, ' ');
} else if (time) { } else if (time) {
whline(main, ' ', uiTime.width + 1); whline(main, ' ', uiTime.width);
wmove(main, y, uiTime.width + 1); wmove(main, y, uiTime.width);
} }
styleAdd(main, line->str); styleAdd(main, line->str);
getyx(main, ny, nx); getyx(main, ny, nx);
@ -752,8 +754,8 @@ static void inputUpdate(void) {
int y, x; int y, x;
wmove(input, 0, 0); wmove(input, 0, 0);
if (window->time && window->id != Network) { if (window->time && window->id != Network) {
whline(input, ' ', uiTime.width + 1); whline(input, ' ', uiTime.width);
wmove(input, 0, uiTime.width + 1); wmove(input, 0, uiTime.width);
} }
wattr_set(input, styleAttr(stylePrompt), stylePair(stylePrompt), NULL); wattr_set(input, styleAttr(stylePrompt), stylePair(stylePrompt), NULL);
waddstr(input, prefix); waddstr(input, prefix);