From cdb33cbda2bef637055289a0e984a008bf68a46e Mon Sep 17 00:00:00 2001 From: lotherk Date: Wed, 11 Nov 2026 18:03:58 +0000 Subject: [PATCH] ui: input scrolls instead of crashing; history laid out once, word wrapped The prompt wrote into the bottom-right cell of a non-scrolling window (segfault). It now draws the tail of the line. The history pane computed height and drawing separately; it now builds visual lines once (word wrap, hanging indent, scroll in lines, reasoning clipped to its last lines). Co-Authored-By: Claude Sonnet 5.5 --- src/ui.c | 539 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 333 insertions(+), 206 deletions(-) diff --git a/src/ui.c b/src/ui.c index 6e9b8c3..a4b9273 100644 --- a/src/ui.c +++ b/src/ui.c @@ -45,6 +45,7 @@ #include "md.h" #include "theme.h" #include "ui.h" +#include "usage.h" /* set while a turn runs: widgets hold still instead of ticking under the answer */ static volatile int g_turn_busy; @@ -104,7 +105,6 @@ static WINDOW *ui_side; static WINDOW *ui_input; static WINDOW *ui_status; -static size_t align_utf8(const char *s, size_t len, size_t pos); enum { PAIR_MAIN = 1, /* fg on bg */ @@ -522,39 +522,6 @@ role_label(const char *role) */ size_t sp_ui_ascii_char(const char *s, char *out, size_t cap); -static size_t -text_rows(const char *text, int cols) -{ - size_t rows = 0, col = 0, i = 0; - const char *p = text != NULL ? text : ""; - - if (*p == '\0' || cols < 2) - return 0; - while (p[i] != '\0') { - char out[8]; - size_t n, w; - - if (p[i] == '\n') { - rows++; - col = 0; - i++; - continue; - } - if (col >= (size_t)cols) { - rows++; - col = 0; - } - n = sp_ui_ascii_char(p + i, out, sizeof(out)); - if (n == 0) - n = 1; - w = strlen(out); - col += w; - i += n; - } - rows++; - return rows; -} - /* * Rows an entry really occupies: the role prefix shortens the first row, so the * height is computed from exactly the string that gets drawn ("role: text"), @@ -580,14 +547,8 @@ entry_prefix(const struct entry *e, char *buf, size_t len) (void)localtime_r(&e->ts, &tm); strftime(clock, sizeof(clock), "%H:%M:%S", &tm); if (strcmp(e->role, "thinking") == 0) { - snprintf(who, sizeof(who), "%.40s", g_model); - { - char *dot = strstr(who, ".gguf"); - - if (dot != NULL) - *dot = '\0'; - } - snprintf(buf, len, "[%s] * %s is thinking: ", clock, who); + /* no '*': the markdown pass would eat it as emphasis */ + snprintf(buf, len, "[%s] ~ thinking: ", clock); } else if (strcmp(e->role, "you") == 0) { snprintf(who, sizeof(who), "%.40s", g_who); snprintf(buf, len, "[%s] <%s> ", clock, who); @@ -765,120 +726,223 @@ sp_ui_ascii_char(const char *s, char *out, size_t cap) return k; } -/* draw one prepared markdown string, wrapping at 'cols', for at most 'maxrows' */ + +/* + * The history pane, laid out once and drawn from that layout. + * + * The earlier version counted rows in one function and drew in another, and + * the two disagreed (the scroll offset was a byte count, the wrap a column + * count), which is why text landed in "zones" and scrolling jumped. Here an + * entry is turned into visual lines exactly once -- word wrapped, with the + * continuation lines hanging under the text -- and both the height and the + * drawing read that same list. + */ +struct vline { + size_t beg, end; /* bytes of the plain text */ + int indent; /* columns left of the text */ + size_t skipped; /* > 0: a "... N more lines" marker, no text */ +}; + +struct elayout { + char *plain; + unsigned char *attr; + struct vline *v; + size_t n, cap; + int blank; /* a gap before this entry */ +}; + +#define THINK_LINES 10 /* a reasoning stream shows its last lines */ + static int -md_draw(int y, int cols, int maxrows, const char *text, const unsigned char *attr) +vl_add(struct elayout *l, size_t beg, size_t end, int indent, size_t skipped) { - int row = 0, col = 0, x = 1; - size_t i; - attr_t base; - short pair; + if (l->n == l->cap) { + size_t nc = l->cap != 0 ? l->cap * 2 : 16; + struct vline *nv = realloc(l->v, nc * sizeof(*nv)); - /* - * The role colour the caller set is the base; markdown adds to it. The - * attributes used to go to stdscr while the text went to ui_hist, so bold, - * italic and code never showed. - */ - (void)wattr_get(ui_hist, &base, &pair, NULL); - base &= ~A_COLOR; - for (i = 0; text[i] != '\0' && row < maxrows; i++) { - char out[8]; - unsigned a = attr != NULL ? attr[i] : 0; - attr_t at = base; + if (nv == NULL) + return -1; + l->v = nv; + l->cap = nc; + } + l->v[l->n].beg = beg; + l->v[l->n].end = end; + l->v[l->n].indent = indent; + l->v[l->n].skipped = skipped; + l->n++; + return 0; +} - if (text[i] == '\n') { - row++; - col = 0; - x = 1; +/* Word wrap 'p' into 'l'; every line but the first hangs under 'ind'. */ +static void +wrap_text(struct elayout *l, const char *p, int cols, int ind) +{ + size_t i = 0; + int first = 1; + + for (;;) { + int indent = first ? 0 : ind; + size_t avail = (size_t)(cols - indent > 0 ? cols - indent : 1); + size_t beg = i, end, w = 0, brk = 0; + int haveb = 0, wrapped = 0; + + for (;;) { + char o[8]; + size_t n, cw; + + if (p[i] == '\0' || p[i] == '\n') { + end = i; + break; + } + n = sp_ui_ascii_char(p + i, o, sizeof(o)); + if (n == 0) + n = 1; + cw = strlen(o); + if (w + cw > avail && i > beg) { + wrapped = 1; + if (haveb && brk > beg) { + end = brk; + i = brk; + } else + end = i; + break; + } + if (p[i] == ' ' && !(i > beg && p[i - 1] == ' ')) { + brk = i; + haveb = 1; + } + w += cw; + i += n; + } + if (vl_add(l, beg, end, indent, 0) != 0) + return; + first = 0; + if (wrapped) { + while (p[i] == ' ') + i++; + if (p[i] == '\n') + i++; + if (p[i] == '\0') + break; continue; } - if (col >= cols) { - row++; - col = 0; - x = 1; - if (row >= maxrows) - break; - } - i += sp_ui_ascii_char(text + i, out, sizeof(out)) - 1; + if (p[i] == '\0') + break; + i++; /* the newline */ + } +} +static void +elayout_free(struct elayout *l) +{ + free(l->plain); + free(l->attr); + free(l->v); + memset(l, 0, sizeof(*l)); +} + +/* the visual lines of one entry, prefix included */ +static void +entry_layout(const struct entry *e, int cols, struct elayout *l) +{ + char prefix[256]; + char *render; + size_t len; + int ind; + const char *brk; + + memset(l, 0, sizeof(*l)); + entry_prefix(e, prefix, sizeof(prefix)); + /* a table that starts the answer must not start behind the prefix */ + brk = sp_md_table_starts(e->text) ? "\n" : ""; + len = strlen(prefix) + strlen(brk) + strlen(e->text) + 1; + render = malloc(len); + if (render == NULL) + return; + snprintf(render, len, "%s%s%s", prefix, brk, e->text); + if (md_prepare(render, &l->plain, &l->attr) != 0) { + free(l->plain); + free(l->attr); + l->plain = render; + l->attr = NULL; + } else + free(render); + /* + * Chat lines hang under their text. Command output, shell output and + * diffs are columns the author laid out: shifting them would break them. + */ + if (strcmp(e->role, "splinter") == 0 || strcmp(e->role, "shell") == 0 || + strcmp(e->role, "diff") == 0) + ind = 0; + else { + ind = (int)strlen(prefix); + if (ind > 24) + ind = 24; + if (ind > cols / 3) + ind = cols / 3; + } + wrap_text(l, l->plain, cols, ind); + /* blank lines at the end are not content */ + while (l->n > 1 && l->v[l->n - 1].beg == l->v[l->n - 1].end && + l->v[l->n - 1].skipped == 0) + l->n--; + if (strcmp(e->role, "thinking") == 0 && l->n > THINK_LINES) { + size_t keep = THINK_LINES - 2, drop = l->n - 1 - keep, k; + + l->v[1].skipped = drop; + l->v[1].beg = l->v[1].end = 0; + l->v[1].indent = ind; + for (k = 0; k < keep; k++) + l->v[2 + k] = l->v[l->n - keep + k]; + l->n = 2 + keep; + } +} + +static void +draw_vline(int y, const struct elayout *l, const struct vline *vl) +{ + attr_t base; + short pair; + size_t i; + int x = 1 + vl->indent; + + (void)wattr_get(ui_hist, &base, &pair, NULL); + base &= ~A_COLOR; + if (vl->skipped > 0) { + char m[64]; + + snprintf(m, sizeof(m), "... %zu more lines", vl->skipped); + (void)wattr_set(ui_hist, base, PAIR_DIM, NULL); + mvwaddstr(ui_hist, y, x, m); + (void)wattr_set(ui_hist, base, pair, NULL); + return; + } + for (i = vl->beg; i < vl->end;) { + char o[8]; + unsigned a = l->attr != NULL ? l->attr[i] : 0; + attr_t at = base; + size_t n = sp_ui_ascii_char(l->plain + i, o, sizeof(o)); + + if (n == 0) + n = 1; if (a & MD_BOLD) at |= A_BOLD; if (a & MD_ITALIC) at |= A_UNDERLINE; (void)wattr_set(ui_hist, at, (a & MD_CODE) ? PAIR_DIM : pair, NULL); - mvwaddstr(ui_hist, y + row, x, out); - x += (int)strlen(out); - col += (int)strlen(out); + mvwaddstr(ui_hist, y, x, o); + x += (int)strlen(o); + i += n; } (void)wattr_set(ui_hist, base, pair, NULL); - return row + 1; } -static int -entry_lines(int cols, const struct entry *e) -{ - size_t rows, len; - char *render; - - if (cols < 2) - return 1; - { - char prefix[256]; - const char *brk; - - entry_prefix(e, prefix, sizeof(prefix)); - /* a table that starts the answer must not start behind the prefix */ - brk = sp_md_table_starts(e->text) ? "\n" : ""; - len = strlen(prefix) + strlen(brk) + strlen(e->text) + 1; - render = malloc(len); - if (render == NULL) - return 1; - snprintf(render, len, "%s%s%s", prefix, brk, e->text); - } - { - char *plain = NULL; - unsigned char *attr = NULL; - - if (md_prepare(render, &plain, &attr) == 0) - rows = text_rows(plain, cols); - else - rows = text_rows(render, cols); - free(plain); - free(attr); - } - free(render); - if (rows == 0) - rows = 1; - return (int)rows; -} - -/* - * Byte offsets computed from column arithmetic can land inside a UTF-8 - * sequence, and half a sequence is not a character: it draws as a replacement - * glyph. Every slice therefore starts and ends on a sequence boundary. - */ -static size_t -align_utf8(const char *s, size_t len, size_t pos) -{ - if (s == NULL || pos >= len) - return pos; - while (pos < len && ((unsigned char)s[pos] & 0xc0) == 0x80) - pos++; - return pos; -} - -/* - * A real viewport, not a best effort: total height is computed first, the - * window shows the bottom of it, and an entry taller than the screen is shown - * from the offset that puts its end on screen -- never skipped, which would look - * like the answer was lost. - */ static void draw_history(struct ui_state *s, int reserve) { - int rows, cols, y = 0; - size_t idx, total = 0; - long skip = 0; + int rows, cols, y = 0; + size_t idx, total = 0, first, line = 0, maxscroll; + struct elayout *lay; getmaxyx(ui_hist, rows, cols); cols -= 2; /* padding left and right */ @@ -887,78 +951,54 @@ draw_history(struct ui_state *s, int reserve) /* while a turn runs the bottom lines belong to it */ if (reserve > 0 && reserve < rows) rows -= reserve; - - for (idx = 0; idx < s->n; idx++) - total += (size_t)entry_lines(cols, &s->entries[idx]); - if (total > (size_t)rows) - skip = (long)(total - (size_t)rows); - if ((long)s->scroll > 0) - skip -= (long)s->scroll; - if (skip < 0) - skip = 0; + if (s->n == 0 || cols < 2 || rows < 1) { + wnoutrefresh(ui_hist); + return; + } + lay = calloc(s->n, sizeof(*lay)); + if (lay == NULL) { + wnoutrefresh(ui_hist); + return; + } + for (idx = 0; idx < s->n; idx++) { + entry_layout(&s->entries[idx], cols, &lay[idx]); + /* a new question starts a new exchange: one line of air */ + lay[idx].blank = idx > 0 && strcmp(s->entries[idx].role, "you") == 0; + total += lay[idx].n + (size_t)lay[idx].blank; + } + maxscroll = total > (size_t)rows ? total - (size_t)rows : 0; + if (s->scroll > maxscroll) + s->scroll = maxscroll; + first = maxscroll - s->scroll; /* index of the top visible line */ for (idx = 0; idx < s->n && y < rows; idx++) { const struct entry *e = &s->entries[idx]; - int lines = entry_lines(cols, e); - char prefix[128]; - char *render; - size_t len; - long off; - int ny, nx; + size_t k; - if (skip >= lines) { - skip -= lines; - continue; - } - entry_prefix(e, prefix, sizeof(prefix)); - { - const char *brk = sp_md_table_starts(e->text) ? "\n" : ""; - - len = strlen(prefix) + strlen(brk) + strlen(e->text) + 1; - render = malloc(len); - if (render == NULL) - break; - snprintf(render, len, "%s%s%s", prefix, brk, e->text); - } - - - wmove(ui_hist, y, 1); if (strcmp(e->role, "you") == 0) - wattron(ui_hist, COLOR_PAIR(PAIR_ACCENT) | A_BOLD); + wattrset(ui_hist, COLOR_PAIR(PAIR_ACCENT) | A_BOLD); else if (strcmp(e->role, "agent") == 0) - wattron(ui_hist, COLOR_PAIR(PAIR_MAIN)); + wattrset(ui_hist, COLOR_PAIR(PAIR_MAIN)); else if (strcmp(e->role, "error") == 0) - wattron(ui_hist, COLOR_PAIR(PAIR_ACCENT) | A_BOLD); + wattrset(ui_hist, COLOR_PAIR(PAIR_ACCENT) | A_BOLD); else - wattron(ui_hist, COLOR_PAIR(PAIR_DIM)); - { - char *plain = NULL; - unsigned char *attr = NULL; - size_t plen = 0; - - if (md_prepare(render, &plain, &attr) == 0 && plain != NULL) { - plen = strlen(plain); - off = (long)align_utf8(plain, plen, - (size_t)(skip * cols)); - if (off > (long)plen) - off = (long)plen; - skip = 0; - (void)md_draw(y, cols, rows - y, - plain + (off > 0 ? off : 0), attr + - (off > 0 ? off : 0)); - } else { - skip = 0; - (void)md_draw(y, cols, rows - y, render, NULL); - } - free(plain); - free(attr); - getyx(ui_hist, ny, nx); - y = nx == 0 ? ny : ny + 1; - free(render); - /* the role colour must not leak into the next entry */ - wattrset(ui_hist, A_NORMAL); + wattrset(ui_hist, COLOR_PAIR(PAIR_DIM)); + if (lay[idx].blank) { + if (line >= first && y < rows) + y++; + line++; } + for (k = 0; k < lay[idx].n && y < rows; k++, line++) { + if (line < first) + continue; + draw_vline(y, &lay[idx], &lay[idx].v[k]); + y++; + } + wattrset(ui_hist, A_NORMAL); } + for (idx = 0; idx < s->n; idx++) + elayout_free(&lay[idx]); + free(lay); wnoutrefresh(ui_hist); } @@ -1098,6 +1138,50 @@ update_meter(struct ui_state *s, struct sp_host *host) sp_host_meter_update(host, set_ui_fields, s); } +/* + * What is left of each allowance, for the status line: the provider the run is + * on, then every delegate that has a limit or is benched. Read from the usage + * logs at most every few seconds -- the line repaints twice a second. + */ +static void +usage_line(struct ui_state *s, char *buf, size_t cap) +{ + static char cache[256]; + static time_t at; + time_t now = time(NULL); + + if (now - at >= 5 || cache[0] == '\0') { + const struct sp_config *cfg = s->conf; + const char *act = sp_usage_active(); + struct sp_usage u; + size_t i, n = 0; + char b[96]; + + at = now; + cache[0] = '\0'; + sp_usage_for(cfg, act, &u); + if (u.frac >= 0 || u.benched_until > 0) { + sp_usage_brief(&u, b, sizeof(b)); + n += (size_t)snprintf(cache + n, sizeof(cache) - n, + "%s %s", act, b); + } + for (i = 0; cfg != NULL && i < cfg->ndelegates && + n + 24 < sizeof(cache); i++) { + const struct sp_delegate *d = &cfg->delegate[i]; + + if (strcmp(d->id, act) == 0) + continue; + sp_usage_for(cfg, d->id, &u); + if (u.frac < 0 && u.benched_until == 0) + continue; + sp_usage_brief(&u, b, sizeof(b)); + n += (size_t)snprintf(cache + n, sizeof(cache) - n, + "%s%s %s", n > 0 ? " | " : "", d->id, b); + } + } + snprintf(buf, cap, "%s", cache); +} + static void draw_status(struct ui_state *s, const char *running) { @@ -1132,10 +1216,16 @@ draw_status(struct ui_state *s, const char *running) if (swbuf[0] != '\0' && at < cols - 2) mvwaddnstr(ui_status, 0, at, swbuf, cols - at - 1); { - char right[96]; + char right[96], use[256]; + int ulen, pos; snprintf(right, sizeof(right), "%s %ldms ", s->theme.name, s->last_ms); + usage_line(s, use, sizeof(use)); + ulen = (int)strlen(use); + pos = cols - (int)strlen(right) - 3 - ulen; + if (ulen > 0 && pos > at + 1) + mvwaddnstr(ui_status, 0, pos, use, ulen); if ((int)strlen(right) + 1 < cols) mvwaddnstr(ui_status, 0, cols - (int)strlen(right) - 1, right, cols - 1); @@ -1372,9 +1462,35 @@ redraw(struct ui_state *s, struct sp_agent *ag, struct sp_host *host) static void draw_prompt(const char *input) { + const char *mark = sp_t("ui.prompt_mark"); + char *ascii; + size_t n, avail, cap; + int w = getmaxx(ui_input); + werase(ui_input); - mvwaddstr(ui_input, 0, 0, sp_t("ui.prompt_mark")); - ui_add_text(ui_input, input, getmaxx(ui_input) - 3); + mvwaddstr(ui_input, 0, 0, mark); + /* + * The line is one row and never wraps: when the text is longer than the + * room, the end of it stays visible and the start scrolls out to the + * left. The last column stays empty -- writing into the bottom-right + * cell of a window that cannot scroll is an error in curses, and in + * this port a crash. + */ + avail = (size_t)(w - (int)strlen(mark) - 2); + if (input == NULL || w < 8 || avail < 1) + return; + cap = strlen(input) * 4 + 16; + ascii = malloc(cap); + if (ascii == NULL) + return; + if (sp_ui_ascii(input, ascii, cap) != 0) + snprintf(ascii, cap, "%s", input); + n = strlen(ascii); + if (n > avail) + waddnstr(ui_input, ascii + (n - avail), (int)avail); + else + waddnstr(ui_input, ascii, (int)n); + free(ascii); } /* @@ -1404,6 +1520,7 @@ slash_collect(struct sp_agent *ag, struct sp_host *host, char cdir[700], *p, *tok, *save; slash_add_match(matches, &n, max, "status"); + slash_add_match(matches, &n, max, "usage"); slash_add_match(matches, &n, max, "swarm"); if (host != NULL && sp_buf_init(&b) == 0) { if (sp_host_command_help(host, &b) > 0 && b.p != NULL) { @@ -1581,9 +1698,12 @@ ui_read_line(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, if (ch == KEY_BACKSPACE || ch == 127 || ch == 8) { if (locked) continue; if (len > 0) { - input[--len] = '\0'; - mvwaddch(ui_input, 0, (int)len + 2, ' '); - wmove(ui_input, 0, (int)len + 2); + /* drop a whole UTF-8 sequence, not half of one */ + do + len--; + while (len > 0 && ((unsigned char)input[len] & 0xc0) == 0x80); + input[len] = '\0'; + draw_prompt(input); wrefresh(ui_input); } continue; @@ -1642,7 +1762,7 @@ ui_read_line(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, if (ch >= 32 && ch < 256 && len + 1 < cap) { input[len++] = (char)ch; input[len] = '\0'; - waddch(ui_input, ch); + draw_prompt(input); wrefresh(ui_input); } } @@ -1811,6 +1931,13 @@ ui_slash(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, } if (sp_buf_init(&out) != 0) return 1; + if (strcmp(name, "usage") == 0) { + sp_usage_report(s->conf, &out); + snprintf(outbuf, sizeof(outbuf), "%s", out.p ? out.p : ""); + sp_buf_free(&out); + add_entry(s, "splinter", outbuf); + return 1; + } if (strcmp(name, "status") == 0) { size_t comp = 0, failed = 0; unsigned turns = 0; time_t now = time(NULL);