Artifact 33915f285bf07a32486bff5b7f84f449e969494cff8b0d99c268a7e89e6172b4:
- File
src/tui/width.h
— 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: 1976) [annotate] [blame] [check-ins using]
/* UTF-8 text measurement and wrapping for the terminal (width.c). * Pure functions: no I/O, no globals. Text may contain ANSI escape * sequences (CSI "ESC [ ... final" and OSC "ESC ] ... BEL or ESC \"); they * take no columns and are never split. */ #ifndef PIC_TUI_WIDTH_H #define PIC_TUI_WIDTH_H #include <stddef.h> #include <stdint.h> /* Decodes one code point at s (len > 0). Returns the bytes used (1..4); * invalid or truncated UTF-8 decodes as U+FFFD using 1 byte. */ size_t tui_utf8_decode(const char *s, size_t len, uint32_t *cp); /* Columns a code point takes: 0 for combining marks, zero-width * characters (ZWJ, variation selectors) and C0/C1 controls; 2 for East Asian * wide and fullwidth characters and emoji (U+1F300..U+1FAFF and the like); * 1 otherwise. */ int tui_cp_width(uint32_t cp); /* Display columns of s[0..len), escape sequences excluded. A tab counts 4. */ size_t tui_width(const char *s, size_t len); /* The longest prefix of s[0..len) (in bytes) that fits in cols columns, * never splitting a code point or an escape sequence. */ size_t tui_fit(const char *s, size_t len, size_t cols); /* A growable list of malloc'd, NUL-terminated lines. */ typedef struct { char **lines; size_t n, cap; } tui_lines; int tui_lines_add(tui_lines *l, const char *s, size_t len); /* copies; 0 or -1 */ void tui_lines_clear(tui_lines *l); /* frees the lines, keeps nothing */ /* Appends text wrapped to cols (>= 1) columns to out: a line per "\n", each * broken at the last space that fits, or hard-broken inside a word longer * than cols. Tabs become 4 spaces. Trailing spaces at a break are dropped. * SGR state (ESC [ ... m) carries across breaks: a broken line ends with * ESC [0m when a style is active and the next line starts by re-opening it. * Every produced line has tui_width(line) <= cols. 0 or -1 (out of memory). */ int tui_wrap(const char *text, size_t len, size_t cols, tui_lines *out); #endif