Artifact 5c8212dd6ed76ec5b9235f15588ed5bcd26f7486400c46298e70ad267cac09a1:
- File
src/tui/render.c
— part of check-in
[fc4294d0ea]
at
2026-11-19 13:19:32
on branch tui
— Harden the terminal interface after review
Quitting or pressing Esc again while a request will not stop now escalates from SIGINT to SIGTERM to SIGKILL instead of hanging; a terminated or crashed interface cancels its running request and puts the terminal back; a request that exits without its events is reported. A run is settled when the child exits, not when its output closes. Children run detached from the terminal and get none of the interface's descriptors. Text is sanitized before it reaches the screen: only colour escapes survive, other escapes and control characters are dropped. A streamed reply is committed early only by whole lines, so a resize cannot duplicate or lose text, and a cut-off reply that had lines committed says so. Split key sequences on slow links are kept for the rest; the renderer redraws fully after a resize even when output was committed first. The interface's unit tests run under Fil-C too. (user: dmestas, size: 7117) [annotate] [blame] [check-ins using] [more...]
/* 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"); } if (r->invalid) { /* The old lines were laid out for another width: leave them off * screen; the next draw rewrites the live area from scratch. */ free_prev(r); r->at_row = 0; out_str(r, CURSOR_SHOW SYNC_OFF); return out_flush(r); } 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; } 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); }