Artifact 6553a42b284848c4db59cae507bcea1257847350a8750e3320cd10a6c2cb6362:
- File
src/tui/editor.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: 2210) [annotate] [blame] [check-ins using] [more...]
/* The message editor (editor.c): multi-line text, history, collapsed * pastes. No I/O: keys in, lines out. */ #ifndef PIC_TUI_EDITOR_H #define PIC_TUI_EDITOR_H #include "term.h" #include "width.h" typedef struct tui_editor tui_editor; enum { TUI_EDIT_NONE, /* the key did nothing */ TUI_EDIT_CHANGED, /* text or cursor changed: redraw */ TUI_EDIT_SUBMIT, /* Enter on non-empty text: take it with tui_editor_take */ }; tui_editor *tui_editor_new(void); void tui_editor_free(tui_editor *e); /* Applies one key: * CHAR inserts; NEWLINE inserts "\n"; ENTER submits (nothing when the text * is only whitespace); BACKSPACE / DELETE remove one character, or a whole * paste marker; LEFT/RIGHT move by character (across lines), UP/DOWN by * line, keeping the column, and on the first/last line recall the * previous/next history entry (the unsent text is kept and restored past * the newest); HOME/END go to the start/end of the line; WORD_LEFT/RIGHT * by word; DELETE_WORD deletes the word before the cursor; KILL_LINE and * KILL_START delete to the end/start of the line; PASTE inserts the text, * except that a paste over 10 lines or 1000 characters is shown as one * atomic marker "[paste #N +L lines]" (or "[paste #N C chars]") and * expanded on submit. Other keys: TUI_EDIT_NONE (the caller handles them). */ int tui_editor_key(tui_editor *e, const tui_key *k); /* The submitted text with markers expanded (malloc'd); the editor is left * empty and the text is added to the history (no repeat of the last entry, * at most 200 entries). */ char *tui_editor_take(tui_editor *e); int tui_editor_empty(const tui_editor *e); void tui_editor_clear(tui_editor *e); /* Replaces the text (e.g. queued messages restored), cursor at the end. */ void tui_editor_set(tui_editor *e, const char *text); /* The text as lines for cols columns, appended to out: the first line * starts with "> ", later ones with " ", wrapped (tui_wrap rules) within * cols; the cursor's row (counted in out from the first line appended) and * column in *crow, *ccol. 0 or -1. */ int tui_editor_render(const tui_editor *e, size_t cols, tui_lines *out, int *crow, int *ccol); #endif