Artifact b06876bb702957bd6f4a9698bdbf6eb12c4f4d20ef985cb3634e999e4a701876:
- File
src/tui/render.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: 1798) [annotate] [blame] [check-ins using] [more...]
/* Inline drawing (render.c). The screen is the terminal's own scrollback: * finished output is committed once and left there; only a small live * area at the bottom (the reply being written, status, editor, footer) is * redrawn, and only from its first changed line. Every write is wrapped in * synchronized output (CSI ?2026h ... CSI ?2026l). */ #ifndef PIC_TUI_RENDER_H #define PIC_TUI_RENDER_H #include <stddef.h> typedef struct tui_render tui_render; /* Writes to fd (the terminal). The live area starts empty at the cursor. */ tui_render *tui_render_new(int fd); void tui_render_free(tui_render *r); /* Replaces the live area with lines[0..n) (each already fits in the width, * no newlines; may contain SGR) and puts the cursor at line crow, column * ccol of it (crow < 0: cursor hidden). Lines equal to the previous draw's * are not rewritten: the cursor moves up to the first line that differs, * clears to the end of the screen and writes from there. More lines than * the terminal has rows: only the last rows-1 are drawn. 0, or -1 on a * write error. */ int tui_render_draw(tui_render *r, const char *const *lines, size_t n, int crow, int ccol, int rows); /* Writes lines[0..n) permanently above the live area (they scroll into the * terminal's history and are never redrawn), then redraws the live area * as last drawn below them. */ int tui_render_commit(tui_render *r, const char *const *lines, size_t n); /* After a resize: the next draw rewrites the whole live area. The old live * area is cleared from its first row down (reflow may leave stray lines * above it in the history; they are not chased). */ void tui_render_invalidate(tui_render *r); /* Leaves the cursor on a fresh line below the live area, shown. */ int tui_render_finish(tui_render *r); #endif