Artifact 58f73eaf03602929f71ab828328bf8fbd5bed0974c4a5e2c3b198e02679fe5b0:
- File
src/tui/width.c
— part of check-in
[cb2ad5fbe9]
at
2026-11-19 13:09:42
on branch tui
— Add a terminal interface: plain pic in a terminal
A module in the pic binary (src/tui) that runs each request as a child pic --json process and knows it only through its events, so a fault in the interface cannot touch a run. Finished output is committed once to the terminal's scrollback; a small live area (the reply being streamed, a running tool call, status, queued messages, the editor and a footer with the directory, conversation, tokens, context and model) is redrawn from its first changed line, inside synchronized output. A reply that fails mid-stream only ever lived in the live area and simply goes away.
Editor: multi-line, history, bracketed paste with large pastes shown as atomic markers. Keys: Enter sends (queues while a reply runs), Ctrl+J or Alt+Enter adds a line, Esc interrupts, Ctrl+C clears and twice quits, Ctrl+D quits, Ctrl+O expands later tool output. Commands: new, resume with a picker, name, fork, compact, model, help, quit; commands typed during a reply wait for it. pic tui -c continues the latest conversation and shows its history; pic chat remains the plain prompt.
For clients, agent_start now carries the conversation, model and context window and message_end the usage; the conversation list and a conversation's entries are available as JSON. Tested with unit tests for the width, key, render and editor modules and end to end on a pseudo-terminal. (user: dmestas, size: 10670) [annotate] [blame] [check-ins using]
/* UTF-8 measurement and wrapping; see width.h. */ #include "tui/width.h" #include <stdlib.h> #include <string.h> #define ESC "\x1b" size_t tui_utf8_decode(const char *s, size_t len, uint32_t *cp) { const unsigned char *p = (const unsigned char *)s; uint32_t c = p[0]; size_t need; uint32_t min; if (c < 0x80) { *cp = c; return 1; } if (c >= 0xC2 && c <= 0xDF) { need = 2, min = 0x80, c &= 0x1F; } else if (c >= 0xE0 && c <= 0xEF) { need = 3, min = 0x800, c &= 0x0F; } else if (c >= 0xF0 && c <= 0xF4) { need = 4, min = 0x10000, c &= 0x07; } else { goto bad; } if (len < need) goto bad; for (size_t i = 1; i < need; i++) { if ((p[i] & 0xC0) != 0x80) goto bad; c = (c << 6) | (p[i] & 0x3F); } if (c < min || c > 0x10FFFF || (c >= 0xD800 && c <= 0xDFFF)) goto bad; *cp = c; return need; bad: *cp = 0xFFFD; return 1; } typedef struct { uint32_t lo, hi; } range; /* Sorted, non-overlapping. Zero width: combining marks, ZWSP..RLM (incl. * ZWJ), variation selectors, line/paragraph separators, BOM. */ static const range zero_ranges[] = { {0x0300, 0x036F}, {0x0483, 0x0489}, {0x0591, 0x05BD}, {0x0610, 0x061A}, {0x064B, 0x065F}, {0x1AB0, 0x1AFF}, {0x1DC0, 0x1DFF}, {0x200B, 0x200F}, {0x2028, 0x202E}, {0x2060, 0x2064}, {0x20D0, 0x20FF}, {0xFE00, 0xFE0F}, {0xFE20, 0xFE2F}, {0xFEFF, 0xFEFF}, {0xE0100, 0xE01EF}, }; /* Width 2: East Asian wide/fullwidth and emoji presentation blocks. The * U+2600..27BF dingbat/symbol block is mostly text-presentation (width 1 in * wcwidth), so only the common emoji-presentation members are listed. */ static const range wide_ranges[] = { {0x1100, 0x115F}, {0x231A, 0x231B}, {0x23E9, 0x23EC}, {0x23F0, 0x23F0}, {0x23F3, 0x23F3}, {0x25FD, 0x25FE}, {0x2614, 0x2615}, {0x2648, 0x2653}, {0x267F, 0x267F}, {0x2693, 0x2693}, {0x26A1, 0x26A1}, {0x26AA, 0x26AB}, {0x26BD, 0x26BE}, {0x26C4, 0x26C5}, {0x26CE, 0x26CE}, {0x26D4, 0x26D4}, {0x26EA, 0x26EA}, {0x26F2, 0x26F3}, {0x26F5, 0x26F5}, {0x26FA, 0x26FA}, {0x26FD, 0x26FD}, {0x2705, 0x2705}, {0x270A, 0x270B}, {0x2728, 0x2728}, {0x274C, 0x274C}, {0x274E, 0x274E}, {0x2753, 0x2755}, {0x2757, 0x2757}, {0x2795, 0x2797}, {0x27B0, 0x27B0}, {0x27BF, 0x27BF}, {0x2B1B, 0x2B1C}, {0x2B50, 0x2B50}, {0x2B55, 0x2B55}, {0x2E80, 0x303E}, {0x3041, 0xA4CF}, {0xAC00, 0xD7A3}, {0xF900, 0xFAFF}, {0xFE30, 0xFE4F}, {0xFF00, 0xFF60}, {0xFFE0, 0xFFE6}, {0x1F300, 0x1F64F}, {0x1F680, 0x1F6FF}, {0x1F900, 0x1F9FF}, {0x1FA70, 0x1FAFF}, {0x20000, 0x3FFFD}, }; static int in_ranges(const range *t, size_t n, uint32_t cp) { size_t lo = 0, hi = n; while (lo < hi) { size_t mid = lo + (hi - lo) / 2; if (cp < t[mid].lo) hi = mid; else if (cp > t[mid].hi) lo = mid + 1; else return 1; } return 0; } #define COUNT(a) (sizeof(a) / sizeof((a)[0])) int tui_cp_width(uint32_t cp) { if (cp < 0x20 || (cp >= 0x7F && cp < 0xA0)) return 0; if (cp < 0x300) return 1; if (in_ranges(zero_ranges, COUNT(zero_ranges), cp)) return 0; if (in_ranges(wide_ranges, COUNT(wide_ranges), cp)) return 2; return 1; } /* Bytes of the escape sequence at s[0] == ESC. CSI ends at a final byte * 0x40..0x7E, OSC at BEL or ESC \; any other ESC x is two bytes. A * truncated sequence takes the rest of the text. */ static size_t esc_len(const char *s, size_t len) { if (len < 2) return len; if (s[1] == '[') { for (size_t i = 2; i < len; i++) { unsigned char c = (unsigned char)s[i]; if (c >= 0x40 && c <= 0x7E) return i + 1; } return len; } if (s[1] == ']') { for (size_t i = 2; i < len; i++) { if (s[i] == '\a') return i + 1; if (s[i] == 0x1b && i + 1 < len && s[i + 1] == '\\') return i + 2; } return len; } return 2; } size_t tui_width(const char *s, size_t len) { size_t w = 0, i = 0; while (i < len) { if (s[i] == 0x1b) { i += esc_len(s + i, len - i); } else if (s[i] == '\t') { w += 4, i++; } else { uint32_t cp; i += tui_utf8_decode(s + i, len - i, &cp); w += (size_t)tui_cp_width(cp); } } return w; } size_t tui_fit(const char *s, size_t len, size_t cols) { size_t w = 0, i = 0; while (i < len) { size_t n, cw; if (s[i] == 0x1b) { n = esc_len(s + i, len - i), cw = 0; } else if (s[i] == '\t') { n = 1, cw = 4; } else { uint32_t cp; n = tui_utf8_decode(s + i, len - i, &cp); cw = (size_t)tui_cp_width(cp); } if (w + cw > cols) break; w += cw, i += n; } return i; } int tui_lines_add(tui_lines *l, const char *s, size_t len) { if (l->n == l->cap) { size_t cap = l->cap ? l->cap * 2 : 8; char **p = realloc(l->lines, cap * sizeof *p); if (!p) return -1; l->lines = p, l->cap = cap; } char *c = malloc(len + 1); if (!c) return -1; memcpy(c, s, len); c[len] = 0; l->lines[l->n++] = c; return 0; } void tui_lines_clear(tui_lines *l) { for (size_t i = 0; i < l->n; i++) free(l->lines[i]); free(l->lines); l->lines = NULL; l->n = l->cap = 0; } /* ---- wrapping ---------------------------------------------------------- */ typedef struct { char *p; size_t n, cap; } buf; static int buf_add(buf *b, const char *s, size_t n) { if (b->n + n > b->cap) { size_t cap = b->cap ? b->cap : 64; while (cap < b->n + n) cap *= 2; char *p = realloc(b->p, cap); if (!p) return -1; b->p = p, b->cap = cap; } if (n) memcpy(b->p + b->n, s, n); b->n += n; return 0; } #define SGR_MAX 512 /* The line being built. brk is the byte offset in cur where the last run of * spaces starts (-1: none; leading indentation is not a break point) and * sgr_brk the style active there. prev_space: the last visible character was * a space (escapes do not interrupt a run). */ typedef struct { tui_lines *out; size_t cols; buf cur, sgr, sgr_brk; size_t curw; long brk; int prev_space, has_text; int dropping; /* just broke a line: spaces up to the next word are dropped */ } wrap; static int emit(wrap *w, const char *s, size_t n, const buf *style) { buf line = {0}; int rc = buf_add(&line, s, n); if (rc == 0 && style->n) rc = buf_add(&line, ESC "[0m", 4); if (rc == 0) rc = tui_lines_add(w->out, line.p ? line.p : "", line.n); free(line.p); return rc; } /* Ends the current line at byte pos (everything after it moves to the next * line, minus the spaces at its front); style is the SGR state at pos. */ static int break_at(wrap *w, size_t pos, buf style) { buf rest = {0}; size_t i = pos; int skipping = 1, rc = 0; while (i < w->cur.n && rc == 0) { if (w->cur.p[i] == 0x1b) { size_t n = esc_len(w->cur.p + i, w->cur.n - i); rc = buf_add(&rest, w->cur.p + i, n); i += n; } else if (skipping && w->cur.p[i] == ' ') { i++; } else { skipping = 0; rc = buf_add(&rest, w->cur.p + i, 1); i++; } } if (rc == 0) rc = emit(w, w->cur.p, pos, &style); if (rc == 0) { w->cur.n = 0; rc = buf_add(&w->cur, style.p, style.n); if (rc == 0) rc = buf_add(&w->cur, rest.p, rest.n); w->curw = tui_width(rest.p ? rest.p : "", rest.n); } free(rest.p); w->brk = -1; w->prev_space = 0; w->has_text = w->curw > 0; w->dropping = 1; return rc; } static int sgr_update(wrap *w, const char *e, size_t n) { /* e is ESC [ params m */ if (n == 3 || (n == 4 && e[2] == '0')) { w->sgr.n = 0; return 0; } if (w->sgr.n + n > SGR_MAX) return 0; return buf_add(&w->sgr, e, n); } static int add_space(wrap *w) { if (w->dropping) return 0; if (w->curw + 1 > w->cols) { /* The space would hang past the edge: break here, dropping it. */ if (w->prev_space && w->brk >= 0) return break_at(w, (size_t)w->brk, w->sgr_brk); return break_at(w, w->cur.n, w->sgr); } if (!w->prev_space && w->has_text) { w->brk = (long)w->cur.n; w->sgr_brk.n = 0; if (buf_add(&w->sgr_brk, w->sgr.p, w->sgr.n)) return -1; } w->prev_space = w->has_text; w->curw++; return buf_add(&w->cur, " ", 1); } static int add_char(wrap *w, const char *s, size_t n, size_t cw) { while (w->curw > 0 && w->curw + cw > w->cols) { int rc = w->brk >= 0 ? break_at(w, (size_t)w->brk, w->sgr_brk) : break_at(w, w->cur.n, w->sgr); if (rc) return rc; } w->prev_space = 0; w->has_text = 1; w->dropping = 0; w->curw += cw; return buf_add(&w->cur, s, n); } int tui_wrap(const char *text, size_t len, size_t cols, tui_lines *out) { wrap w = {.out = out, .cols = cols ? cols : 1, .brk = -1}; size_t i = 0; int rc = 0; while (i < len && rc == 0) { unsigned char c = (unsigned char)text[i]; if (c == 0x1b) { size_t n = esc_len(text + i, len - i); if (n >= 3 && text[i + 1] == '[' && text[i + n - 1] == 'm') rc = sgr_update(&w, text + i, n); if (rc == 0) rc = buf_add(&w.cur, text + i, n); i += n; } else if (c == '\n') { rc = emit(&w, w.cur.p, w.cur.n, &w.sgr); w.cur.n = 0; if (rc == 0) rc = buf_add(&w.cur, w.sgr.p, w.sgr.n); w.curw = 0, w.brk = -1, w.prev_space = w.has_text = w.dropping = 0; i++; } else if (c == ' ' || c == '\t') { for (int k = 0; k < (c == '\t' ? 4 : 1) && rc == 0; k++) rc = add_space(&w); i++; } else { uint32_t cp; size_t n = tui_utf8_decode(text + i, len - i, &cp); rc = add_char(&w, text + i, n, (size_t)tui_cp_width(cp)); i += n; } } if (rc == 0) rc = tui_lines_add(out, w.cur.p ? w.cur.p : "", w.cur.n); free(w.cur.p); free(w.sgr.p); free(w.sgr_brk.p); return rc; }