xonecas.io
7814
Ta

tiny-c-agent / commit 8cc899e

chore: refactor bad patterns in the TUI

xonecas2026-10-03 8cc899eee54e6ca0647a9568b3e064f98c355e31 parent 328a826

11 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

src/tui/commands.c+17−24
@@ -9,7 +9,6 @@
99 #include "complete.h"
1010 #include "resolve.h"
1111 #include "theme.h"
12-#include "util.h"
1312
1413 // The keybindings the TUI accepts, in the order /help prints them.
1514 static const char *const KEYBINDINGS[][2] = {
@@ -122,28 +121,26 @@ static const char *first_model(const tiny_config *cfg, const char *provider) {
122121 return NULL;
123122 }
124123
125-static void push_option(char ***ids, char ***labels, size_t *n,
126- const char *id, const char *label) {
127- *ids = realloc(*ids, (*n + 1) * sizeof **ids);
128- *labels = realloc(*labels, (*n + 1) * sizeof **labels);
129- if (!*ids || !*labels) abort();
130- (*ids)[*n] = strdup(id);
131- (*labels)[*n] = strdup(label && label[0] ? label : id);
132- if (!(*ids)[*n] || !(*labels)[*n]) abort();
133- (*n)++;
124+void tiny_choice_free(tiny_choice *c) {
125+ strv_free(&c->ids);
126+ strv_free(&c->labels);
134127 }
135128
136-size_t tiny_choose_list(const tiny_config *cfg, tiny_choose what,
137- const char *provider, char ***out_ids, char ***out_labels) {
138- char **ids = NULL, **labels = NULL;
139- size_t n = 0;
129+static void push_option(tiny_choice *c, const char *id, const char *label) {
130+ strv_push(&c->ids, strdup(id));
131+ strv_push(&c->labels, strdup(label && label[0] ? label : id));
132+}
133+
134+tiny_choice tiny_choose_list(const tiny_config *cfg, tiny_choose what,
135+ const char *provider) {
136+ tiny_choice c = {0};
140137
141138 if (what == TINY_CHOOSE_PROVIDER) {
142139 for (size_t i = 0; i < sizeof BUILTINS / sizeof BUILTINS[0]; i++)
143- push_option(&ids, &labels, &n, BUILTINS[i].id, BUILTINS[i].name);
140+ push_option(&c, BUILTINS[i].id, BUILTINS[i].name);
144141 for (size_t i = 0; i < cfg->custom_providers_len; i++) {
145142 const tiny_provider *p = &cfg->custom_providers[i];
146- push_option(&ids, &labels, &n, p->id, p->name);
143+ push_option(&c, p->id, p->name);
147144 }
148145 } else if (what == TINY_CHOOSE_MODEL) {
149146 const char *prov = provider ? provider : cfg->provider;
@@ -151,13 +148,13 @@ size_t tiny_choose_list(const tiny_config *cfg, tiny_choose what,
151148 const tiny_provider *custom = find_custom(cfg, prov);
152149 if (custom) {
153150 for (size_t i = 0; i < custom->models_len; i++)
154- push_option(&ids, &labels, &n, custom->models[i], custom->models[i]);
151+ push_option(&c, custom->models[i], custom->models[i]);
155152 } else {
156153 const char *canonical = tiny_provider_canonical(prov);
157154 for (size_t i = 0; i < tiny_catalog_count(); i++) {
158155 const tiny_model_info *e = tiny_catalog_get(i);
159156 if (strcmp(e->provider, canonical) == 0)
160- push_option(&ids, &labels, &n, e->id, e->name);
157+ push_option(&c, e->id, e->name);
161158 }
162159 }
163160 }
@@ -168,14 +165,12 @@ size_t tiny_choose_list(const tiny_config *cfg, tiny_choose what,
168165 if (info) {
169166 for (size_t i = 0; i < info->effort_len; i++) {
170167 const char *v = strcmp(info->effort[i], "none") == 0 ? "off" : info->effort[i];
171- push_option(&ids, &labels, &n, v, v);
168+ push_option(&c, v, v);
172169 }
173170 }
174171 }
175172
176- *out_ids = ids;
177- *out_labels = labels;
178- return n;
173+ return c;
179174 }
180175
181176 static const char *provider_name(const tiny_config *cfg, const char *id) {
@@ -318,14 +313,12 @@ char *tiny_complete_command(const char *draft) {
318313 buf_addc(&b, '/');
319314 buf_adds(&b, match->name);
320315 result = b.data ? buf_steal(&b) : NULL;
321- buf_free(&b);
322316 } else if (nmatch > 1 && strlen(shared) > tn) {
323317 tui_buf b;
324318 buf_init(&b);
325319 buf_addc(&b, '/');
326320 buf_adds(&b, shared);
327321 result = b.data ? buf_steal(&b) : NULL;
328- buf_free(&b);
329322 }
330323 free(shared);
331324 return result;
src/tui/commands.h+15−6
@@ -6,6 +6,7 @@
66 #include <stddef.h>
77
88 #include "config.h"
9+#include "util.h"
910
1011 // The option sets a command asks the TUI to present.
1112 typedef enum {
@@ -14,6 +15,15 @@ typedef enum {
1415 TINY_CHOOSE_EFFORT,
1516 } tiny_choose;
1617
18+// A chooser's options: `ids` are the values applied, `labels` are shown. The
19+// two lists run parallel and have equal length. Free with bl_choice_free.
20+typedef struct {
21+ tiny_strv ids;
22+ tiny_strv labels;
23+} tiny_choice;
24+
25+void tiny_choice_free(tiny_choice *c);
26+
1727 // What a command may reach. The TUI provides the callbacks.
1828 typedef struct {
1929 void *user; // opaque, the TUI
@@ -30,12 +40,11 @@ typedef struct {
3040 const char *effort, char **err);
3141 } tiny_command_ctx;
3242
33-// Builds the options a chooser presents as malloc'd parallel arrays: `*ids`
34-// are the values applied, `*labels` are shown. `provider` overrides
35-// cfg->provider for TINY_CHOOSE_MODEL. Returns the count, 0 when there is
36-// nothing to offer; the caller frees each string and both arrays.
37-size_t tiny_choose_list(const tiny_config *cfg, tiny_choose what,
38- const char *provider, char ***ids, char ***labels);
43+// Builds the options a chooser presents. `provider` overrides
44+// cfg->provider for TINY_CHOOSE_MODEL. Returns an empty choice when there is
45+// nothing to offer.
46+tiny_choice tiny_choose_list(const tiny_config *cfg, tiny_choose what,
47+ const char *provider);
3948
4049 // The one-line heading above a chooser's options, malloc'd. `provider`
4150 // overrides cfg->provider for TINY_CHOOSE_MODEL, naming the provider chosen.
src/tui/complete.c+0−2
@@ -103,7 +103,6 @@ char *tiny_complete_path(const char *word, const char *cwd) {
103103 buf_add(&b, word, keep);
104104 buf_adds(&b, cands[0]);
105105 result = b.data ? buf_steal(&b) : NULL;
106- buf_free(&b);
107106 }
108107 } else if (n > 1) {
109108 char *shared = strdup(cands[0]);
@@ -118,7 +117,6 @@ char *tiny_complete_path(const char *word, const char *cwd) {
118117 buf_add(&b, word, keep);
119118 buf_adds(&b, shared);
120119 result = b.data ? buf_steal(&b) : NULL;
121- buf_free(&b);
122120 }
123121 free(shared);
124122 }
src/tui/editor.c+37−82
@@ -3,7 +3,6 @@
33 #include <stdlib.h>
44 #include <string.h>
55
6-#include "util.h"
76 #include "util.h"
87
98 #define TAB 4
@@ -129,7 +128,6 @@ const char *tiny_editor_text(tiny_editor *e) {
129128 }
130129 free(e->text);
131130 e->text = buf_steal(&b);
132- buf_free(&b);
133131 e->text_dirty = false;
134132 return e->text;
135133 }
@@ -147,29 +145,11 @@ void tiny_editor_clear(tiny_editor *e) {
147145
148146 void tiny_editor_set_text(tiny_editor *e, const char *text) {
149147 for (size_t i = 0; i < e->nlines; i++) free(e->lines[i]);
150- // Split on newline.
151- size_t cap = 8, n = 0;
152- char **lines = xmalloc(cap * sizeof *lines);
153- const char *p = text;
154- for (;;) {
155- const char *nl = strchr(p, '\n');
156- size_t len = nl ? (size_t)(nl - p) : strlen(p);
157- char *line = xmalloc(len + 1);
158- memcpy(line, p, len);
159- line[len] = '\0';
160- if (n == cap) {
161- cap *= 2;
162- lines = realloc(lines, cap * sizeof *lines);
163- if (!lines) abort();
164- }
165- lines[n++] = line;
166- if (!nl) break;
167- p = nl + 1;
168- }
148+ tiny_strv lines = tiny_split_lines(text);
169149 free(e->lines);
170- e->lines = lines;
171- e->nlines = n;
172- e->row = n - 1;
150+ e->lines = lines.v;
151+ e->nlines = lines.n;
152+ e->row = lines.n - 1;
173153 e->col = cp_count(e->lines[e->row]);
174154 e->scroll = 0;
175155 e->text_dirty = true;
@@ -213,22 +193,20 @@ static size_t col_at_cell(const char *line, int cell) {
213193 static void caret(tiny_editor *e, int *row_out, int *col_out) {
214194 char *line = xstrdup(e->lines[e->row]);
215195 char *expanded = tiny_expand_tabs(line, TAB);
216- size_t nrows;
217- char **chunks = tiny_wrap_line(expanded, e->width, &nrows);
196+ tiny_strv chunks = tiny_wrap_line(expanded, e->width);
218197 size_t ncells;
219198 int *cells = cells_of(line, &ncells);
220199 int cell = cells[e->col < ncells ? e->col : ncells - 1];
221200 free(cells);
222201 int row = cell / e->width;
223- if (row >= (int)nrows) {
224- *row_out = (int)nrows - 1;
225- *col_out = tiny_display_width(chunks[nrows - 1]);
202+ if (row >= (int)chunks.n) {
203+ *row_out = (int)chunks.n - 1;
204+ *col_out = tiny_display_width(chunks.v[chunks.n - 1]);
226205 } else {
227206 *row_out = row;
228207 *col_out = cell - row * e->width;
229208 }
230- for (size_t i = 0; i < nrows; i++) free(chunks[i]);
231- free(chunks);
209+ strv_free(&chunks);
232210 free(expanded);
233211 free(line);
234212 }
@@ -247,17 +225,11 @@ static void line_splice(tiny_editor *e, const char *line, size_t start, size_t e
247225 free(after);
248226 free(e->lines[e->row]);
249227 e->lines[e->row] = buf_steal(&b);
250- buf_free(&b);
251228 e->text_dirty = true;
252229 }
253230
254231 static void insert_text(tiny_editor *e, const char *text) {
255- // Split `text` on newlines.
256- size_t nparts = 1;
257- for (const char *p = text; *p; p++)
258- if (*p == '\n') nparts++;
259-
260- if (nparts == 1) {
232+ if (!strchr(text, '\n')) {
261233 line_splice(e, e->lines[e->row], e->col, e->col, text);
262234 e->col += cp_count(text);
263235 return;
@@ -268,30 +240,28 @@ static void insert_text(tiny_editor *e, const char *text) {
268240 char *after = cp_slice(line, e->col, cp_count(line));
269241
270242 // Build the replacement lines.
271- size_t n;
272- char **parts = tiny_split_lines(text, &n);
243+ tiny_strv parts = tiny_split_lines(text);
244+ size_t n = parts.n;
273245
274246 tui_buf head;
275247 buf_init(&head);
276248 buf_adds(&head, before);
277- buf_adds(&head, parts[0]);
249+ buf_adds(&head, parts.v[0]);
278250 tui_buf tail;
279251 buf_init(&tail);
280- buf_adds(&tail, parts[n - 1]);
252+ buf_adds(&tail, parts.v[n - 1]);
281253 buf_adds(&tail, after);
282- size_t caret_col = cp_count(parts[n - 1]);
283- free(parts[0]);
284- free(parts[n - 1]);
254+ size_t caret_col = cp_count(parts.v[n - 1]);
255+ free(parts.v[0]);
256+ free(parts.v[n - 1]);
285257
286258 // New line array: [0, row) + head + middle parts + tail + (row, end).
287259 char **nl = xmalloc((e->nlines + 1) * sizeof *nl);
288260 size_t k = 0;
289261 for (size_t i = 0; i < e->row; i++) nl[k++] = e->lines[i];
290262 nl[k++] = buf_steal(&head);
291- buf_free(&head);
292- for (size_t i = 1; i + 1 < n; i++) nl[k++] = parts[i];
263+ for (size_t i = 1; i + 1 < n; i++) nl[k++] = parts.v[i];
293264 nl[k++] = buf_steal(&tail);
294- buf_free(&tail);
295265 for (size_t i = e->row + 1; i < e->nlines; i++) nl[k++] = e->lines[i];
296266 free(line);
297267 free(e->lines);
@@ -299,7 +269,7 @@ static void insert_text(tiny_editor *e, const char *text) {
299269 e->nlines = k;
300270 e->row += n - 1;
301271 e->col = caret_col;
302- free(parts);
272+ free(parts.v);
303273 free(before);
304274 free(after);
305275 e->text_dirty = true;
@@ -319,7 +289,6 @@ static void backspace(tiny_editor *e) {
319289 buf_adds(&b, e->lines[e->row]);
320290 free(e->lines[e->row - 1]);
321291 e->lines[e->row - 1] = buf_steal(&b);
322- buf_free(&b);
323292 free(e->lines[e->row]);
324293 for (size_t i = e->row; i + 1 < e->nlines; i++) e->lines[i] = e->lines[i + 1];
325294 e->nlines--;
@@ -342,7 +311,6 @@ static void delete_forward(tiny_editor *e) {
342311 buf_adds(&b, e->lines[e->row + 1]);
343312 free(e->lines[e->row]);
344313 e->lines[e->row] = buf_steal(&b);
345- buf_free(&b);
346314 free(e->lines[e->row + 1]);
347315 for (size_t i = e->row + 1; i + 1 < e->nlines; i++) e->lines[i] = e->lines[i + 1];
348316 e->nlines--;
@@ -423,32 +391,28 @@ tiny_edit_result tiny_editor_handle(tiny_editor *e, const tiny_key *key) {
423391 if (e->row == 0) return TINY_EDIT_CHANGED;
424392 e->row--;
425393 char *expanded = tiny_expand_tabs(e->lines[e->row], TAB);
426- size_t nrows;
427- char **chunks = tiny_wrap_line(expanded, e->width, &nrows);
428- int last_row = (int)nrows - 1;
429- for (size_t i = 0; i < nrows; i++) free(chunks[i]);
430- free(chunks);
394+ tiny_strv chunks = tiny_wrap_line(expanded, e->width);
431395 free(expanded);
432- e->col = col_at_cell(e->lines[e->row], last_row * e->width + ccol);
396+ e->col = col_at_cell(e->lines[e->row], ((int)chunks.n - 1) * e->width + ccol);
397+ strv_free(&chunks);
433398 return TINY_EDIT_CHANGED;
434399 }
435400 case TINY_KEY_DOWN: {
436401 int crow, ccol;
437402 caret(e, &crow, &ccol);
438403 char *expanded = tiny_expand_tabs(e->lines[e->row], TAB);
439- size_t nrows;
440- char **chunks = tiny_wrap_line(expanded, e->width, &nrows);
441- for (size_t i = 0; i < nrows; i++) free(chunks[i]);
442- free(chunks);
404+ tiny_strv chunks = tiny_wrap_line(expanded, e->width);
443405 free(expanded);
444- if (crow < (int)nrows - 1) {
406+ if (crow < (int)chunks.n - 1) {
445407 size_t n;
446408 int *cells = cells_of(e->lines[e->row], &n);
447409 int cell = cells[e->col < n ? e->col : n - 1] + e->width;
448410 free(cells);
449411 e->col = col_at_cell(e->lines[e->row], cell);
412+ strv_free(&chunks);
450413 return TINY_EDIT_CHANGED;
451414 }
415+ strv_free(&chunks);
452416 if (e->row + 1 >= e->nlines) return TINY_EDIT_CHANGED;
453417 e->row++;
454418 e->col = col_at_cell(e->lines[e->row], ccol);
@@ -494,50 +458,41 @@ bool tiny_editor_complete_word(tiny_editor *e, char *(*step)(const char *word, v
494458
495459 tiny_editor_view tiny_editor_render(tiny_editor *e, int width, int max_rows) {
496460 e->width = width > 0 ? width : 1;
497- size_t cap = 16, n = 0;
498- char **rows = xmalloc(cap * sizeof *rows);
461+ tiny_strv rows = {0};
499462 int cursor_row = 0, cursor_col = 0;
500463 for (size_t line = 0; line < e->nlines; line++) {
501464 char *disp = xstrdup(e->lines[line]);
502465 char *expanded = tiny_expand_tabs(disp, TAB);
503- size_t nchunks;
504- char **chunks = tiny_wrap_line(expanded, e->width, &nchunks);
466+ tiny_strv chunks = tiny_wrap_line(expanded, e->width);
505467 free(expanded);
506468 free(disp);
507469 if (line == e->row) {
508470 int cr, cc;
509471 caret(e, &cr, &cc);
510- cursor_row = (int)n + cr;
472+ cursor_row = (int)rows.n + cr;
511473 cursor_col = cc;
512474 }
513- for (size_t i = 0; i < nchunks; i++) {
514- if (n == cap) {
515- cap *= 2;
516- rows = realloc(rows, cap * sizeof *rows);
517- if (!rows) abort();
518- }
519- rows[n++] = chunks[i];
520- }
521- free(chunks);
475+ for (size_t i = 0; i < chunks.n; i++) strv_push(&rows, chunks.v[i]);
476+ free(chunks.v);
522477 }
523478
524479 int view = max_rows > 0 ? max_rows : 1;
525480 if (cursor_row < e->scroll) e->scroll = cursor_row;
526481 else if (cursor_row >= e->scroll + view) e->scroll = cursor_row - view + 1;
527- int max_scroll = (int)n - view;
482+ int max_scroll = (int)rows.n - view;
528483 if (max_scroll < 0) max_scroll = 0;
529484 if (e->scroll > max_scroll) e->scroll = max_scroll;
530485 if (e->scroll < 0) e->scroll = 0;
531486
532- size_t shown = n > (size_t)e->scroll ? n - (size_t)e->scroll : 0;
487+ size_t shown = rows.n > (size_t)e->scroll ? rows.n - (size_t)e->scroll : 0;
533488 if (shown > (size_t)view) shown = (size_t)view;
534489 char **out = xmalloc((shown ? shown : 1) * sizeof *out);
535490 // Free rows scrolled off and after the window.
536- for (size_t i = 0; i < n; i++) {
537- if (i < (size_t)e->scroll || i >= (size_t)e->scroll + shown) free(rows[i]);
538- else out[i - (size_t)e->scroll] = rows[i];
491+ for (size_t i = 0; i < rows.n; i++) {
492+ if (i < (size_t)e->scroll || i >= (size_t)e->scroll + shown) free(rows.v[i]);
493+ else out[i - (size_t)e->scroll] = rows.v[i];
539494 }
540- free(rows);
495+ free(rows.v);
541496
542497 tiny_editor_view v = {
543498 .rows = out,
src/tui/stream.c+59−95
@@ -5,41 +5,23 @@
55
66 #include "util.h"
77 #include "highlight.h"
8-#include "util.h"
9-
10-static void strv_clear(char **v, size_t *n) {
11- for (size_t i = 0; i < *n; i++) free(v[i]);
12- *n = 0;
13-}
14-
15-static void strv_free(char **v, size_t n) {
16- for (size_t i = 0; i < n; i++) free(v[i]);
17- free(v);
18-}
198
209 // ---- body line lists -------------------------------------------------------
2110
2211 void bl_push(tiny_blist *l, char *text, tiny_fg_fn style, const char *bg) {
2312 if (l->n == l->cap) {
2413 l->cap = l->cap ? l->cap * 2 : 16;
25- l->v = realloc(l->v, l->cap * sizeof *l->v);
26- if (!l->v) abort();
14+ l->v = xrealloc(l->v, l->cap * sizeof *l->v);
2715 }
2816 l->v[l->n++] = (tiny_body_line){text, style, bg};
2917 }
3018
31-tiny_body_line *bl_take(tiny_blist *l, size_t *count) {
32- *count = l->n;
33- if (l->n == 0) {
34- free(l->v);
35- return NULL;
36- }
37- return l->v;
38-}
39-
40-void tiny_body_lines_free(tiny_body_line *lines, size_t count) {
41- for (size_t i = 0; i < count; i++) free(lines[i].text);
42- free(lines);
19+void bl_free(tiny_blist *l) {
20+ for (size_t i = 0; i < l->n; i++) free(l->v[i].text);
21+ free(l->v);
22+ l->v = NULL;
23+ l->n = 0;
24+ l->cap = 0;
4325 }
4426
4527 // ---- fence parsing ---------------------------------------------------------
@@ -96,10 +78,8 @@ void tiny_mdstream_free(tiny_mdstream *s) {
9678 free(s->rest);
9779 free(s->fence_marker);
9880 free(s->fence_info);
99- strv_clear(s->fence_lines, &s->fence_n);
100- free(s->fence_lines);
101- strv_clear(s->prose, &s->prose_n);
102- free(s->prose);
81+ strv_free(&s->fence_lines);
82+ strv_free(&s->prose);
10383 memset(s, 0, sizeof *s);
10484 }
10585
@@ -110,29 +90,26 @@ void tiny_mdstream_reset(tiny_mdstream *s) {
11090 s->fence_marker = NULL;
11191 free(s->fence_info);
11292 s->fence_info = NULL;
113- strv_clear(s->fence_lines, &s->fence_n);
114- strv_clear(s->prose, &s->prose_n);
93+ strv_free(&s->fence_lines);
94+ strv_free(&s->prose);
11595 }
11696
11797 // Emits the held prose as one highlighted block.
11898 static void release_prose(tiny_mdstream *s, const char *tail, tiny_blist *out) {
119- char **prose = s->prose;
120- size_t n = s->prose_n;
121- s->prose = NULL;
122- s->prose_n = 0;
123- s->prose_cap = 0;
99+ tiny_strv prose = s->prose;
100+ memset(&s->prose, 0, sizeof s->prose);
124101
125102 tui_buf joined;
126103 buf_init(&joined);
127- for (size_t i = 0; i < n; i++) {
104+ for (size_t i = 0; i < prose.n; i++) {
128105 if (i) buf_addc(&joined, '\n');
129- buf_adds(&joined, prose[i]);
106+ buf_adds(&joined, prose.v[i]);
130107 }
131108 if (tail[0]) {
132- if (n) buf_addc(&joined, '\n');
109+ if (prose.n) buf_addc(&joined, '\n');
133110 buf_adds(&joined, tail);
134111 }
135- strv_free(prose, n);
112+ strv_free(&prose);
136113 if (joined.len == 0) {
137114 buf_free(&joined);
138115 return;
@@ -142,49 +119,45 @@ static void release_prose(tiny_mdstream *s, const char *tail, tiny_blist *out) {
142119 buf_addc(&joined, '\n');
143120 char *colored = tiny_highlight_markdown(joined.data);
144121 buf_free(&joined);
145- size_t nlines;
146- char **lines = tiny_split_lines(colored, &nlines);
122+ tiny_strv lines = tiny_split_lines(colored);
147123 // The split adds a trailing line holding the closing reset; attach it.
148- if (nlines > 1) {
149- char *reset = lines[nlines - 1];
124+ if (lines.n > 1) {
125+ char *reset = lines.v[lines.n - 1];
150126 tui_buf last;
151127 buf_init(&last);
152- buf_adds(&last, lines[nlines - 2]);
128+ buf_adds(&last, lines.v[lines.n - 2]);
153129 buf_adds(&last, reset);
154- free(lines[nlines - 2]);
130+ free(lines.v[lines.n - 2]);
155131 free(reset);
156- lines[nlines - 2] = buf_steal(&last);
157- buf_free(&last);
158- nlines--;
132+ lines.v[lines.n - 2] = buf_steal(&last);
133+ lines.n--;
159134 }
160- for (size_t i = 0; i < nlines; i++) bl_push(out, lines[i], NULL, NULL);
161- free(lines);
135+ for (size_t i = 0; i < lines.n; i++) bl_push(out, lines.v[i], NULL, NULL);
136+ free(lines.v);
162137 free(colored);
163138 }
164139
165140 static void release_fence(tiny_mdstream *s, const char *closing, tiny_blist *out) {
166141 // The opening fence line, colored as markdown.
167- char *first = s->fence_lines[0];
142+ char *first = s->fence_lines.v[0];
168143 char *first_col = tiny_highlight_markdown(first);
169144 bl_push(out, first_col, NULL, NULL);
170- size_t n = s->fence_n;
145+ size_t n = s->fence_lines.n;
171146 if (n > 1) {
172147 tui_buf body;
173148 buf_init(&body);
174149 for (size_t i = 1; i < n; i++) {
175150 if (i > 1) buf_addc(&body, '\n');
176- buf_adds(&body, s->fence_lines[i]);
151+ buf_adds(&body, s->fence_lines.v[i]);
177152 }
178153 char *colored = tiny_highlight_code(s->fence_info, body.data);
179154 buf_free(&body);
180- size_t nlines;
181- char **lines = tiny_split_lines(colored, &nlines);
182- for (size_t i = 0; i < nlines; i++) bl_push(out, lines[i], NULL, NULL);
183- free(lines);
155+ tiny_strv lines = tiny_split_lines(colored);
156+ for (size_t i = 0; i < lines.n; i++) bl_push(out, lines.v[i], NULL, NULL);
157+ free(lines.v);
184158 free(colored);
185159 }
186- for (size_t i = 0; i < n; i++) free(s->fence_lines[i]);
187- s->fence_n = 0;
160+ strv_free(&s->fence_lines);
188161 free(s->fence_marker);
189162 s->fence_marker = NULL;
190163 free(s->fence_info);
@@ -205,14 +178,14 @@ static void commit_line(tiny_mdstream *s, const char *line, tiny_blist *out) {
205178 return;
206179 }
207180 free(close);
208- tiny_strv_push(&s->fence_lines, &s->fence_n, &s->fence_cap, xstrdup(line));
181+ strv_push(&s->fence_lines, xstrdup(line));
209182 return;
210183 }
211184 char *marker = NULL, *info = NULL;
212185 if (fence_open(line, &marker, &info)) {
213186 s->fence_marker = marker;
214187 s->fence_info = info;
215- tiny_strv_push(&s->fence_lines, &s->fence_n, &s->fence_cap, xstrdup(line));
188+ strv_push(&s->fence_lines, xstrdup(line));
216189 release_prose(s, "", out);
217190 return;
218191 }
@@ -223,10 +196,10 @@ static void commit_line(tiny_mdstream *s, const char *line, tiny_blist *out) {
223196 bl_push(out, xstrdup(line), NULL, NULL);
224197 return;
225198 }
226- tiny_strv_push(&s->prose, &s->prose_n, &s->prose_cap, xstrdup(line));
199+ strv_push(&s->prose, xstrdup(line));
227200 }
228201
229-tiny_body_line *tiny_mdstream_feed(tiny_mdstream *s, const char *delta, size_t *count) {
202+tiny_blist tiny_mdstream_feed(tiny_mdstream *s, const char *delta) {
230203 tiny_blist out = {0};
231204 size_t rest_len = strlen(s->rest);
232205 size_t dl = strlen(delta);
@@ -235,29 +208,27 @@ tiny_body_line *tiny_mdstream_feed(tiny_mdstream *s, const char *delta, size_t *
235208 memcpy(joined + rest_len, delta, dl + 1);
236209 free(s->rest);
237210
238- size_t nlines;
239- char **lines = tiny_split_lines(joined, &nlines);
211+ tiny_strv lines = tiny_split_lines(joined);
240212 // The last split part is the still-incomplete tail.
241- s->rest = lines[nlines - 1];
242- for (size_t i = 0; i + 1 < nlines; i++) {
243- commit_line(s, lines[i], &out);
244- free(lines[i]);
213+ s->rest = lines.v[lines.n - 1];
214+ for (size_t i = 0; i + 1 < lines.n; i++) {
215+ commit_line(s, lines.v[i], &out);
216+ free(lines.v[i]);
245217 }
246- free(lines);
218+ free(lines.v);
247219 free(joined);
248- return bl_take(&out, count);
220+ return out;
249221 }
250222
251-tiny_body_line *tiny_mdstream_pending(tiny_mdstream *s, size_t *count) {
223+tiny_blist tiny_mdstream_pending(tiny_mdstream *s) {
252224 tiny_blist out = {0};
253- char **held = s->fence_marker ? s->fence_lines : s->prose;
254- size_t n = s->fence_marker ? s->fence_n : s->prose_n;
255- for (size_t i = 0; i < n; i++) bl_push(&out, xstrdup(held[i]), NULL, NULL);
225+ tiny_strv *held = s->fence_marker ? &s->fence_lines : &s->prose;
226+ for (size_t i = 0; i < held->n; i++) bl_push(&out, xstrdup(held->v[i]), NULL, NULL);
256227 if (s->rest[0]) bl_push(&out, xstrdup(s->rest), NULL, NULL);
257- return bl_take(&out, count);
228+ return out;
258229 }
259230
260-tiny_body_line *tiny_mdstream_flush(tiny_mdstream *s, size_t *count) {
231+tiny_blist tiny_mdstream_flush(tiny_mdstream *s) {
261232 tiny_blist out = {0};
262233 if (!s->fence_marker) {
263234 release_prose(s, s->rest, &out);
@@ -266,15 +237,14 @@ tiny_body_line *tiny_mdstream_flush(tiny_mdstream *s, size_t *count) {
266237 if (s->rest[0]) {
267238 char *c = tiny_highlight_markdown(s->rest);
268239 // Split the possibly multi-line remainder.
269- size_t nlines;
270- char **lines = tiny_split_lines(c, &nlines);
271- for (size_t i = 0; i < nlines; i++) bl_push(&out, lines[i], NULL, NULL);
272- free(lines);
240+ tiny_strv lines = tiny_split_lines(c);
241+ for (size_t i = 0; i < lines.n; i++) bl_push(&out, lines.v[i], NULL, NULL);
242+ free(lines.v);
273243 free(c);
274244 }
275245 }
276246 tiny_mdstream_reset(s);
277- return bl_take(&out, count);
247+ return out;
278248 }
279249
280250 // ---- tail stream -----------------------------------------------------------
@@ -293,7 +263,7 @@ void tiny_tailstream_reset(tiny_tailstream *s) {
293263 s->rest = xstrdup("");
294264 }
295265
296-tiny_body_line *tiny_tailstream_feed(tiny_tailstream *s, const char *delta, size_t *count) {
266+tiny_blist tiny_tailstream_feed(tiny_tailstream *s, const char *delta) {
297267 size_t rl = strlen(s->rest), dl = strlen(delta);
298268 char *text = xmalloc(rl + dl + 1);
299269 memcpy(text, s->rest, rl);
@@ -307,18 +277,12 @@ tiny_body_line *tiny_tailstream_feed(tiny_tailstream *s, const char *delta, size
307277 text = NULL;
308278 }
309279 free(text);
310- *count = 0;
311- return NULL;
280+ return (tiny_blist){0};
312281 }
313282
314-tiny_body_line *tiny_tailstream_pending(tiny_tailstream *s, size_t *count) {
315- if (s->rest[0] == '\0') {
316- *count = 0;
317- return NULL;
318- }
319- tiny_body_line *v = xmalloc(sizeof *v);
320- v[0] = (tiny_body_line){xstrdup(s->rest), fg_dim, NULL};
321- *count = 1;
322- return v;
283+tiny_blist tiny_tailstream_pending(tiny_tailstream *s) {
284+ tiny_blist out = {0};
285+ if (s->rest[0] != '\0') bl_push(&out, xstrdup(s->rest), fg_dim, NULL);
286+ return out;
323287 }
324288
src/tui/stream.h+10−19
@@ -17,9 +17,7 @@ typedef struct {
1717 const char *bg; // row background, or NULL for Normal
1818 } tiny_body_line;
1919
20-void tiny_body_lines_free(tiny_body_line *lines, size_t count);
21-
22-// A growable list of body lines, used while assembling a block.
20+// An owned, growable list of owned body lines. Free with bl_free.
2321 typedef struct {
2422 tiny_body_line *v;
2523 size_t n;
@@ -27,29 +25,22 @@ typedef struct {
2725 } tiny_blist;
2826
2927 void bl_push(tiny_blist *l, char *text, tiny_fg_fn style, const char *bg);
30-
31-// Hands the list's array to the caller (NULL when empty) and resets the count.
32-tiny_body_line *bl_take(tiny_blist *l, size_t *count);
28+void bl_free(tiny_blist *l);
3329
3430 typedef struct {
3531 char *rest;
3632 char *fence_marker;
3733 char *fence_info;
38- char **fence_lines;
39- size_t fence_n;
40- size_t fence_cap;
41- char **prose;
42- size_t prose_n;
43- size_t prose_cap;
34+ tiny_strv fence_lines;
35+ tiny_strv prose;
4436 } tiny_mdstream;
4537
4638 void tiny_mdstream_init(tiny_mdstream *s);
4739 void tiny_mdstream_free(tiny_mdstream *s);
48-// feed/pending/flush return a heap array the caller frees with
49-// tiny_body_lines_free; *count receives its length.
50-tiny_body_line *tiny_mdstream_feed(tiny_mdstream *s, const char *delta, size_t *count);
51-tiny_body_line *tiny_mdstream_pending(tiny_mdstream *s, size_t *count);
52-tiny_body_line *tiny_mdstream_flush(tiny_mdstream *s, size_t *count);
40+// feed/pending/flush return a list the caller frees with bl_free.
41+tiny_blist tiny_mdstream_feed(tiny_mdstream *s, const char *delta);
42+tiny_blist tiny_mdstream_pending(tiny_mdstream *s);
43+tiny_blist tiny_mdstream_flush(tiny_mdstream *s);
5344 void tiny_mdstream_reset(tiny_mdstream *s);
5445
5546 typedef struct {
@@ -58,8 +49,8 @@ typedef struct {
5849
5950 void tiny_tailstream_init(tiny_tailstream *s);
6051 void tiny_tailstream_free(tiny_tailstream *s);
61-tiny_body_line *tiny_tailstream_feed(tiny_tailstream *s, const char *delta, size_t *count);
62-tiny_body_line *tiny_tailstream_pending(tiny_tailstream *s, size_t *count);
52+tiny_blist tiny_tailstream_feed(tiny_tailstream *s, const char *delta);
53+tiny_blist tiny_tailstream_pending(tiny_tailstream *s);
6354 void tiny_tailstream_reset(tiny_tailstream *s);
6455
6556 #endif
src/tui/term.c+5−18
@@ -190,17 +190,13 @@ char *tiny_expand_tabs(const char *text, int size) {
190190 return buf_steal(&out);
191191 }
192192
193-char **tiny_wrap_line(const char *text, int width, size_t *count) {
194- size_t cap = 8, n = 0;
195- char **rows = malloc(cap * sizeof *rows);
193+tiny_strv tiny_wrap_line(const char *text, int width) {
194+ tiny_strv rows = {0};
196195 tui_buf cur;
197196 buf_init(&cur);
198197 int used = 0;
199198 if (width <= 0 || text[0] == '\0') {
200- if (cap == 0) cap = 1;
201- rows[0] = strdup("");
202- *count = 1;
203- buf_free(&cur);
199+ strv_push(&rows, strdup(""));
204200 return rows;
205201 }
206202 size_t len = strlen(text);
@@ -218,11 +214,7 @@ char **tiny_wrap_line(const char *text, int width, size_t *count) {
218214 if (nb <= 0) break;
219215 int w = tiny_char_width(cp);
220216 if (used + w > width && cur.len > 0) {
221- if (n == cap) {
222- cap *= 2;
223- rows = realloc(rows, cap * sizeof *rows);
224- }
225- rows[n++] = buf_steal(&cur);
217+ strv_push(&rows, buf_steal(&cur));
226218 buf_init(&cur);
227219 used = 0;
228220 }
@@ -230,12 +222,7 @@ char **tiny_wrap_line(const char *text, int width, size_t *count) {
230222 used += w;
231223 i += (size_t)nb;
232224 }
233- if (n == cap) {
234- cap += 1;
235- rows = realloc(rows, cap * sizeof *rows);
236- }
237- rows[n++] = buf_steal(&cur);
238- *count = n;
225+ strv_push(&rows, buf_steal(&cur));
239226 return rows;
240227 }
241228
src/tui/term.h+5−3
@@ -11,6 +11,8 @@
1111 #include <stddef.h>
1212 #include <stdint.h>
1313
14+#include "util.h"
15+
1416 // Decodes one UTF-8 code point; returns its byte length, or 1 with *cp = the
1517 // raw byte on malformed input.
1618 int tiny_utf8_decode(const char *s, size_t len, uint32_t *cp);
@@ -25,9 +27,9 @@ int tiny_display_width(const char *text);
2527 char *tiny_sanitize(const char *text);
2628 // Expands tabs to `size`-cell stops. Returns malloc'd.
2729 char *tiny_expand_tabs(const char *text, int size);
28-// Splits one logical line into rows no wider than `width`. Returns a malloc'd
29-// array of malloc'd rows; *count receives the row count.
30-char **tiny_wrap_line(const char *text, int width, size_t *count);
30+// Splits one logical line into rows no wider than `width`. Returns an owned
31+// list of owned rows.
32+tiny_strv tiny_wrap_line(const char *text, int width);
3133
3234 typedef enum {
3335 TINY_KEY_TEXT,
src/tui/tui.c+149−248
@@ -73,11 +73,6 @@ static bool starts_with(const char *s, const char *prefix) {
7373 return strncmp(s, prefix, strlen(prefix)) == 0;
7474 }
7575
76-static void free_rows(char **rows, size_t n) {
77- for (size_t i = 0; i < n; i++) free(rows[i]);
78- free(rows);
79-}
80-
8176 // ---- tui state -------------------------------------------------------------
8277
8378 typedef struct {
@@ -137,9 +132,7 @@ typedef struct {
137132 bool choice_active;
138133 tiny_choose choice_kind;
139134 char *choice_provider;
140- char **choice_ids;
141- char **choice_labels;
142- size_t choice_n;
135+ tiny_choice choice;
143136
144137 pthread_mutex_t lock;
145138 pthread_cond_t cv;
@@ -166,66 +159,49 @@ static double monotonic_now(void) {
166159
167160 // Wraps plain text, then styles each row. Styling after the break lets a
168161 // continuation row inherit its source line's style.
169-static char **render_rows(const tiny_body_line *lines, size_t n, int width, size_t *out_n) {
170- size_t cap = 16, m = 0;
171- char **out = xmalloc(cap * sizeof *out);
162+static tiny_strv render_rows(const tiny_body_line *lines, size_t n, int width) {
163+ tiny_strv out = {0};
172164 for (size_t i = 0; i < n; i++) {
173165 char *clean = tiny_sanitize(lines[i].text);
174166 char *expanded = tiny_expand_tabs(clean, 4);
175- size_t nrows;
176- char **rows = tiny_wrap_line(expanded, width, &nrows);
167+ tiny_strv rows = tiny_wrap_line(expanded, width);
177168 free(clean);
178169 free(expanded);
179- for (size_t r = 0; r < nrows; r++) {
180- if (m == cap) {
181- cap *= 2;
182- out = realloc(out, cap * sizeof *out);
183- if (!out) abort();
184- }
185- if (rows[r][0] == '\0') {
186- out[m++] = rows[r];
170+ for (size_t r = 0; r < rows.n; r++) {
171+ if (rows.v[r][0] == '\0') {
172+ strv_push(&out, rows.v[r]);
187173 continue;
188174 }
189175 tui_buf b;
190176 buf_init(&b);
191177 const char *bg = lines[i].bg ? lines[i].bg : tiny_normal_bg();
192178 sgr_bg(&b, bg);
193- if (lines[i].style) lines[i].style(&b, rows[r]);
194- else buf_adds(&b, rows[r]);
179+ if (lines[i].style) lines[i].style(&b, rows.v[r]);
180+ else buf_adds(&b, rows.v[r]);
195181 sgr_bg(&b, bg);
196- out[m++] = buf_steal(&b);
197- buf_free(&b);
198- free(rows[r]);
182+ strv_push(&out, buf_steal(&b));
183+ free(rows.v[r]);
199184 }
200- free(rows);
185+ free(rows.v);
201186 }
202- *out_n = m;
203187 return out;
204188 }
205189
206190 // render_rows plus the display-only elision for long tool bodies.
207-static char **body_rows(const tiny_body_line *lines, size_t n, int width, size_t *out_n) {
208- size_t nrows;
209- char **rows = render_rows(lines, n, width, &nrows);
210- if (nrows <= MAX_BODY_ROWS) {
211- *out_n = nrows;
212- return rows;
213- }
214- size_t hidden = nrows - ELIDED_HEAD - ELIDED_TAIL;
215- size_t m = ELIDED_HEAD + 1 + ELIDED_TAIL;
216- char **out = xmalloc(m * sizeof *out);
217- size_t k = 0;
218- for (size_t i = 0; i < ELIDED_HEAD; i++) out[k++] = rows[i], rows[i] = NULL;
191+static tiny_strv body_rows(const tiny_body_line *lines, size_t n, int width) {
192+ tiny_strv rows = render_rows(lines, n, width);
193+ if (rows.n <= MAX_BODY_ROWS) return rows;
194+ size_t hidden = rows.n - ELIDED_HEAD - ELIDED_TAIL;
195+ tiny_strv out = {0};
196+ for (size_t i = 0; i < ELIDED_HEAD; i++) strv_push(&out, rows.v[i]);
219197 tui_buf b;
220198 buf_init(&b);
221199 char note[64];
222200 snprintf(note, sizeof note, "... %zu lines not shown ...", hidden);
223201 fg_dim(&b, note);
224- out[k++] = buf_steal(&b);
225- buf_free(&b);
226- for (size_t i = nrows - ELIDED_TAIL; i < nrows; i++) out[k++] = rows[i], rows[i] = NULL;
227- free_rows(rows, nrows);
228- *out_n = m;
202+ strv_push(&out, buf_steal(&b));
203+ for (size_t i = rows.n - ELIDED_TAIL; i < rows.n; i++) strv_push(&out, rows.v[i]);
204+ free(rows.v);
229205 return out;
230206 }
231207
@@ -255,23 +231,21 @@ static void push(tui *t, const char *row) {
255231 }
256232 }
257233
258-static void commit_lines(tui *t, tiny_body_line *lines, size_t n) {
259- size_t nrows;
260- char **rows = render_rows(lines, n, t->width > 0 ? t->width : 1, &nrows);
261- for (size_t i = 0; i < nrows; i++) push(t, rows[i]);
262- free_rows(rows, nrows);
234+static void commit_lines(tui *t, const tiny_body_line *lines, size_t n) {
235+ tiny_strv rows = render_rows(lines, n, t->width > 0 ? t->width : 1);
236+ for (size_t i = 0; i < rows.n; i++) push(t, rows.v[i]);
237+ strv_free(&rows);
263238 }
264239
265240 static void commit_user(tui *t, const char *text) {
266241 t->separator = true;
267- size_t n;
268- char **parts = tiny_split_lines(text, &n);
269- tiny_body_line *lines = xmalloc(n * sizeof *lines);
270- for (size_t i = 0; i < n; i++)
271- lines[i] = (tiny_body_line){parts[i], fg_blue, NULL};
272- free(parts);
273- commit_lines(t, lines, n);
274- for (size_t i = 0; i < n; i++) free(lines[i].text);
242+ tiny_strv parts = tiny_split_lines(text);
243+ tiny_body_line *lines = xmalloc(parts.n * sizeof *lines);
244+ for (size_t i = 0; i < parts.n; i++)
245+ lines[i] = (tiny_body_line){parts.v[i], fg_blue, NULL};
246+ free(parts.v);
247+ commit_lines(t, lines, parts.n);
248+ for (size_t i = 0; i < parts.n; i++) free(lines[i].text);
275249 free(lines);
276250 t->separator = true;
277251 }
@@ -295,17 +269,16 @@ static tiny_body_line diff_line(const char *line) {
295269
296270 // Splits a tool result into its display lines, rewriting the trailing exit code
297271 // for bash and stripping the repeated diff header for edit.
298-static tiny_body_line *result_lines(const char *name, const char *text, bool is_error, size_t *out_n) {
272+static tiny_blist result_lines(const char *name, const char *text, bool is_error) {
299273 char *copy = tiny_trim_end(text);
300- size_t raw_n;
301- char **raw = tiny_split_lines(copy, &raw_n);
302- size_t n = raw_n;
274+ tiny_strv raw = tiny_split_lines(copy);
275+ size_t n = raw.n;
303276
304277 tiny_blist out = {0};
305278 bool diff = false;
306279 size_t start = 0;
Cut short: this commit's diff is over 1000 lines.
src/util.c+25−23
Cut short: this commit's diff is over 1000 lines.
src/util.h+16−9
Cut short: this commit's diff is over 1000 lines.