xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / tui / stream.c

8.3 KB
1#include "stream.h"
2
3#include <stdlib.h>
4#include <string.h>
5
6#include "util.h"
7#include "highlight.h"
8
9// ---- body line lists -------------------------------------------------------
10
11void bl_push(tiny_blist *l, char *text, tiny_fg_fn style, const char *bg) {
12    if (l->n == l->cap) {
13        l->cap = l->cap ? l->cap * 2 : 16;
14        l->v = xrealloc(l->v, l->cap * sizeof *l->v);
15    }
16    l->v[l->n++] = (tiny_body_line){text, style, bg};
17}
18
19void 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;
25}
26
27// ---- fence parsing ---------------------------------------------------------
28
29// `^ {0,3}(`{3,}|~{3,})\s*(\S*)`: returns the marker and info when it matches.
30static bool fence_open(const char *line, char **marker, char **info) {
31    size_t i = 0;
32    while (i < 3 && line[i] == ' ') i++;
33    char c = line[i];
34    if (c != '`' && c != '~') return false;
35    size_t start = i;
36    while (line[i] == c) i++;
37    if (i - start < 3) return false;
38    char *m = xmalloc(i - start + 1);
39    memcpy(m, line + start, i - start);
40    m[i - start] = '\0';
41    while (line[i] == ' ' || line[i] == '\t') i++;
42    size_t s = i;
43    while (line[i] && line[i] != ' ' && line[i] != '\t') i++;
44    char *inf = xmalloc(i - s + 1);
45    memcpy(inf, line + s, i - s);
46    inf[i - s] = '\0';
47    *marker = m;
48    *info = inf;
49    return true;
50}
51
52// `^ {0,3}(`{3,}|~{3,})\s*$`: the marker run, or NULL.
53static char *fence_close(const char *line) {
54    size_t i = 0;
55    while (i < 3 && line[i] == ' ') i++;
56    char c = line[i];
57    if (c != '`' && c != '~') return NULL;
58    size_t start = i;
59    while (line[i] == c) i++;
60    if (i - start < 3) return NULL;
61    size_t j = i;
62    while (line[j] == ' ' || line[j] == '\t') j++;
63    if (line[j] != '\0') return NULL;
64    char *m = xmalloc(i - start + 1);
65    memcpy(m, line + start, i - start);
66    m[i - start] = '\0';
67    return m;
68}
69
70// ---- markdown stream -------------------------------------------------------
71
72void tiny_mdstream_init(tiny_mdstream *s) {
73    memset(s, 0, sizeof *s);
74    s->rest = xstrdup("");
75}
76
77void tiny_mdstream_free(tiny_mdstream *s) {
78    free(s->rest);
79    free(s->fence_marker);
80    free(s->fence_info);
81    strv_free(&s->fence_lines);
82    strv_free(&s->prose);
83    memset(s, 0, sizeof *s);
84}
85
86void tiny_mdstream_reset(tiny_mdstream *s) {
87    free(s->rest);
88    s->rest = xstrdup("");
89    free(s->fence_marker);
90    s->fence_marker = NULL;
91    free(s->fence_info);
92    s->fence_info = NULL;
93    strv_free(&s->fence_lines);
94    strv_free(&s->prose);
95}
96
97// Emits the held prose as one highlighted block.
98static void release_prose(tiny_mdstream *s, const char *tail, tiny_blist *out) {
99    tiny_strv prose = s->prose;
100    memset(&s->prose, 0, sizeof s->prose);
101
102    tui_buf joined;
103    buf_init(&joined);
104    for (size_t i = 0; i < prose.n; i++) {
105        if (i) buf_addc(&joined, '\n');
106        buf_adds(&joined, prose.v[i]);
107    }
108    if (tail[0]) {
109        if (prose.n) buf_addc(&joined, '\n');
110        buf_adds(&joined, tail);
111    }
112    strv_free(&prose);
113    if (joined.len == 0) {
114        buf_free(&joined);
115        return;
116    }
117    // A block is only a block once its last line ends, so the text needs a
118    // terminating newline; the closing reset belongs to the last line.
119    buf_addc(&joined, '\n');
120    char *colored = tiny_highlight_markdown(joined.data);
121    buf_free(&joined);
122    tiny_strv lines = tiny_split_lines(colored);
123    // The split adds a trailing line holding the closing reset; attach it.
124    if (lines.n > 1) {
125        char *reset = lines.v[lines.n - 1];
126        tui_buf last;
127        buf_init(&last);
128        buf_adds(&last, lines.v[lines.n - 2]);
129        buf_adds(&last, reset);
130        free(lines.v[lines.n - 2]);
131        free(reset);
132        lines.v[lines.n - 2] = buf_steal(&last);
133        lines.n--;
134    }
135    for (size_t i = 0; i < lines.n; i++) bl_push(out, lines.v[i], NULL, NULL);
136    free(lines.v);
137    free(colored);
138}
139
140static void release_fence(tiny_mdstream *s, const char *closing, tiny_blist *out) {
141    // The opening fence line, colored as markdown.
142    char *first = s->fence_lines.v[0];
143    char *first_col = tiny_highlight_markdown(first);
144    bl_push(out, first_col, NULL, NULL);
145    size_t n = s->fence_lines.n;
146    if (n > 1) {
147        tui_buf body;
148        buf_init(&body);
149        for (size_t i = 1; i < n; i++) {
150            if (i > 1) buf_addc(&body, '\n');
151            buf_adds(&body, s->fence_lines.v[i]);
152        }
153        char *colored = tiny_highlight_code(s->fence_info, body.data);
154        buf_free(&body);
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);
158        free(colored);
159    }
160    strv_free(&s->fence_lines);
161    free(s->fence_marker);
162    s->fence_marker = NULL;
163    free(s->fence_info);
164    s->fence_info = NULL;
165    if (closing) {
166        char *cc = tiny_highlight_markdown(closing);
167        bl_push(out, cc, NULL, NULL);
168    }
169}
170
171static void commit_line(tiny_mdstream *s, const char *line, tiny_blist *out) {
172    if (s->fence_marker) {
173        char *close = fence_close(line);
174        if (close && close[0] == s->fence_marker[0] &&
175            strlen(close) >= strlen(s->fence_marker)) {
176            release_fence(s, line, out);
177            free(close);
178            return;
179        }
180        free(close);
181        strv_push(&s->fence_lines, xstrdup(line));
182        return;
183    }
184    char *marker = NULL, *info = NULL;
185    if (fence_open(line, &marker, &info)) {
186        s->fence_marker = marker;
187        s->fence_info = info;
188        strv_push(&s->fence_lines, xstrdup(line));
189        release_prose(s, "", out);
190        return;
191    }
192    const char *t = line;
193    while (*t == ' ' || *t == '\t') t++;
194    if (*t == '\0') {
195        release_prose(s, "", out);
196        bl_push(out, xstrdup(line), NULL, NULL);
197        return;
198    }
199    strv_push(&s->prose, xstrdup(line));
200}
201
202tiny_blist tiny_mdstream_feed(tiny_mdstream *s, const char *delta) {
203    tiny_blist out = {0};
204    size_t rest_len = strlen(s->rest);
205    size_t dl = strlen(delta);
206    char *joined = xmalloc(rest_len + dl + 1);
207    memcpy(joined, s->rest, rest_len);
208    memcpy(joined + rest_len, delta, dl + 1);
209    free(s->rest);
210
211    tiny_strv lines = tiny_split_lines(joined);
212    // The last split part is the still-incomplete tail.
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]);
217    }
218    free(lines.v);
219    free(joined);
220    return out;
221}
222
223tiny_blist tiny_mdstream_pending(tiny_mdstream *s) {
224    tiny_blist out = {0};
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);
227    if (s->rest[0]) bl_push(&out, xstrdup(s->rest), NULL, NULL);
228    return out;
229}
230
231tiny_blist tiny_mdstream_flush(tiny_mdstream *s) {
232    tiny_blist out = {0};
233    if (!s->fence_marker) {
234        release_prose(s, s->rest, &out);
235    } else {
236        release_fence(s, NULL, &out);
237        if (s->rest[0]) {
238            char *c = tiny_highlight_markdown(s->rest);
239            // Split the possibly multi-line remainder.
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);
243            free(c);
244        }
245    }
246    tiny_mdstream_reset(s);
247    return out;
248}
249
250// ---- tail stream -----------------------------------------------------------
251
252void tiny_tailstream_init(tiny_tailstream *s) {
253    s->rest = xstrdup("");
254}
255
256void tiny_tailstream_free(tiny_tailstream *s) {
257    free(s->rest);
258    s->rest = NULL;
259}
260
261void tiny_tailstream_reset(tiny_tailstream *s) {
262    free(s->rest);
263    s->rest = xstrdup("");
264}
265
266tiny_blist tiny_tailstream_feed(tiny_tailstream *s, const char *delta) {
267    size_t rl = strlen(s->rest), dl = strlen(delta);
268    char *text = xmalloc(rl + dl + 1);
269    memcpy(text, s->rest, rl);
270    memcpy(text + rl, delta, dl + 1);
271    free(s->rest);
272    const char *nl = strrchr(text, '\n');
273    if (nl) {
274        s->rest = xstrdup(nl + 1);
275    } else {
276        s->rest = text;
277        text = NULL;
278    }
279    free(text);
280    return (tiny_blist){0};
281}
282
283tiny_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;
287}
288