Artifact fb33aebed9bb9f4312e64238978164218cc658c8972ebaaa5faca7273c1aece5:
- File
src/tui/render.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: 6847) [annotate] [blame] [check-ins using]
/* Inline differential renderer; see render.h. * * Model: the live area is rows 0..nprev-1 below the point where it started. * at_row is the row the terminal cursor is on (column unknown, so every * cursor placement starts with "\r"). Each call builds its whole output in * memory and writes it once, wrapped in synchronized output. */ #include "tui/render.h" #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define SYNC_ON "\x1b[?2026h" #define SYNC_OFF "\x1b[?2026l" #define CURSOR_HIDE "\x1b[?25l" #define CURSOR_SHOW "\x1b[?25h" #define CLEAR_DOWN "\x1b[J" struct tui_render { int fd; char **prev; /* the live area as last drawn (clipped to the rows) */ size_t nprev; size_t at_row; /* terminal cursor row within the live area */ int crow, ccol; /* where the cursor should be (crow < 0: hidden) */ int invalid; /* redraw everything next time */ char *out; /* output being built */ size_t out_n, out_cap; int oom; }; static void out_add(tui_render *r, const char *s, size_t n) { if (r->oom) return; if (r->out_n + n > r->out_cap) { size_t cap = r->out_cap ? r->out_cap : 256; while (cap < r->out_n + n) cap *= 2; char *p = realloc(r->out, cap); if (!p) { r->oom = 1; return; } r->out = p, r->out_cap = cap; } memcpy(r->out + r->out_n, s, n); r->out_n += n; } static void out_str(tui_render *r, const char *s) { out_add(r, s, strlen(s)); } static void out_csi(tui_render *r, size_t n, char fin) { char b[32]; int len = snprintf(b, sizeof b, "\x1b[%zu%c", n, fin); out_add(r, b, (size_t)len); } /* Moves the cursor between rows of the live area. */ static void move_rows(tui_render *r, size_t from, size_t to) { if (to < from) out_csi(r, from - to, 'A'); else if (to > from) out_csi(r, to - from, 'B'); } /* Writes the buffer out (sync-wrapped by the caller); 0 or -1. */ static int out_flush(tui_render *r) { int rc = r->oom ? -1 : 0; size_t off = 0; while (rc == 0 && off < r->out_n) { ssize_t w = write(r->fd, r->out + off, r->out_n - off); if (w < 0) { if (errno == EINTR) continue; rc = -1; } else { off += (size_t)w; } } r->out_n = 0; r->oom = 0; return rc; } /* Puts the cursor where r->crow/ccol say, from end_row, and shows or leaves * it hidden; closes the synchronized update. */ static void finish_frame(tui_render *r, size_t end_row) { if (r->crow >= 0 && (size_t)r->crow < r->nprev) { move_rows(r, end_row, (size_t)r->crow); out_str(r, "\r"); if (r->ccol > 0) out_csi(r, (size_t)r->ccol + 1, 'G'); out_str(r, CURSOR_SHOW); r->at_row = (size_t)r->crow; } else { r->at_row = end_row; if (r->nprev == 0) out_str(r, CURSOR_SHOW); /* no live area: nothing to hide for */ } out_str(r, SYNC_OFF); } static void free_prev(tui_render *r) { for (size_t i = 0; i < r->nprev; i++) free(r->prev[i]); free(r->prev); r->prev = NULL; r->nprev = 0; } tui_render *tui_render_new(int fd) { tui_render *r = calloc(1, sizeof *r); if (r) r->fd = fd; return r; } void tui_render_free(tui_render *r) { if (!r) return; free_prev(r); free(r->out); free(r); } /* Writes lines[from..n) separated by "\r\n"; the cursor ends on row n-1. */ static void write_lines(tui_render *r, const char *const *lines, size_t from, size_t n) { for (size_t i = from; i < n; i++) { if (i > from) out_str(r, "\r\n"); out_str(r, lines[i]); } } int tui_render_draw(tui_render *r, const char *const *lines, size_t n, int crow, int ccol, int rows) { size_t max = rows > 1 ? (size_t)rows - 1 : 1; if (n > max) { size_t skip = n - max; lines += skip; n = max; crow = crow >= 0 && (size_t)crow >= skip ? crow - (int)skip : -1; } /* First row that differs from the last draw. */ size_t d = 0, common = r->nprev < n ? r->nprev : n; if (!r->invalid) while (d < common && strcmp(r->prev[d], lines[d]) == 0) d++; out_str(r, SYNC_ON); out_str(r, CURSOR_HIDE); size_t end_row = r->at_row; if (r->nprev == 0) { write_lines(r, lines, 0, n); /* starts where the cursor is */ end_row = n ? n - 1 : 0; } else if (!r->invalid && d == r->nprev && d == n) { /* identical: only the cursor may move */ } else if (!r->invalid && d == r->nprev) { /* Append: below the last line. */ move_rows(r, r->at_row, r->nprev - 1); out_str(r, "\r\n"); write_lines(r, lines, d, n); end_row = n - 1; } else if (d == n) { /* Shrink: clear from the first removed row, then back onto the last. */ move_rows(r, r->at_row, n); out_str(r, "\r" CLEAR_DOWN); if (n > 0) { out_csi(r, 1, 'A'); end_row = n - 1; } else { end_row = 0; } } else { move_rows(r, r->at_row, d); out_str(r, "\r" CLEAR_DOWN); write_lines(r, lines, d, n); end_row = n - 1; } { /* Remember what is on screen. */ char **copy = n ? calloc(n, sizeof *copy) : NULL; if (n && !copy) return -1; for (size_t i = 0; i < n; i++) { copy[i] = strdup(lines[i]); if (!copy[i]) { for (size_t j = 0; j < i; j++) free(copy[j]); free(copy); return -1; } } free_prev(r); r->prev = copy; r->nprev = n; } r->invalid = 0; r->crow = crow; r->ccol = ccol; finish_frame(r, end_row); return out_flush(r); } int tui_render_commit(tui_render *r, const char *const *lines, size_t n) { out_str(r, SYNC_ON); out_str(r, CURSOR_HIDE); if (r->nprev > 0) { move_rows(r, r->at_row, 0); out_str(r, "\r" CLEAR_DOWN); } for (size_t i = 0; i < n; i++) { out_str(r, lines[i]); out_str(r, "\r\n"); } size_t end_row = 0; if (r->nprev > 0) { write_lines(r, (const char *const *)r->prev, 0, r->nprev); end_row = r->nprev - 1; } r->invalid = 0; finish_frame(r, end_row); return out_flush(r); } void tui_render_invalidate(tui_render *r) { r->invalid = 1; } int tui_render_finish(tui_render *r) { if (r->nprev > 0) { move_rows(r, r->at_row, r->nprev - 1); out_str(r, "\r\n"); } out_str(r, CURSOR_SHOW); free_prev(r); r->at_row = 0; r->crow = -1; return out_flush(r); }