xonecas.io
79113
Ma

mini-coding-agent / files

mini-coding-agent / src / tui / stream.zig

5.0 KB
1const std = @import("std");
2const format = @import("format.zig");
3const highlight = @import("highlight.zig");
4const render = @import("render.zig");
5const text = @import("../text.zig");
6
7const BodyLine = render.BodyLine;
8
9pub const MarkdownStream = struct {
10    arena: std.heap.ArenaAllocator,
11    a: std.mem.Allocator = undefined,
12    rest: std.ArrayList(u8) = .empty,
13    fence: ?struct { marker: []const u8, lines: std.ArrayList([]const u8) } = null,
14    prose: std.ArrayList([]const u8) = .empty,
15    norm: format.Normalizer = .{},
16
17    pub fn init(backing: std.mem.Allocator) MarkdownStream {
18        return .{ .arena = std.heap.ArenaAllocator.init(backing) };
19    }
20
21    pub fn bind(self: *MarkdownStream) void {
22        self.a = self.arena.allocator();
23    }
24
25    pub fn feed(self: *MarkdownStream, out_a: std.mem.Allocator, delta: []const u8) []BodyLine {
26        self.rest.appendSlice(self.a, delta) catch {};
27        var out: std.ArrayList(BodyLine) = .empty;
28        var i: usize = 0;
29        while (std.mem.indexOfScalarPos(u8, self.rest.items, i, '\n')) |nl| {
30            const line = self.a.dupe(u8, self.rest.items[i..nl]) catch "";
31            self.commit(out_a, line, &out);
32            i = nl + 1;
33        }
34        if (i > 0) {
35            const keep = self.rest.items[i..];
36            std.mem.copyForwards(u8, self.rest.items[0..keep.len], keep);
37            self.rest.items.len = keep.len;
38        }
39        return out.items;
40    }
41
42    fn commit(self: *MarkdownStream, out_a: std.mem.Allocator, raw: []const u8, out: *std.ArrayList(BodyLine)) void {
43        if (self.fence) |*f| {
44            if (format.fenceClose(raw, f.marker)) {
45                self.releaseFence(out_a, self.norm.line(self.a, raw), out);
46            } else {
47                f.lines.append(self.a, raw) catch {};
48            }
49            return;
50        }
51        const line = self.norm.line(self.a, raw);
52        if (format.fenceRun(line)) |marker| {
53            self.releaseProse(out_a, "", out);
54            var fl: std.ArrayList([]const u8) = .empty;
55            fl.append(self.a, line) catch {};
56            self.fence = .{ .marker = marker, .lines = fl };
57            return;
58        }
59        if (std.mem.trim(u8, line, " \t\r").len == 0) {
60            self.releaseProse(out_a, "", out);
61            out.append(out_a, .{ .text = "" }) catch {};
62            return;
63        }
64        self.prose.append(self.a, line) catch {};
65    }
66
67    fn releaseProse(self: *MarkdownStream, out_a: std.mem.Allocator, tail: []const u8, out: *std.ArrayList(BodyLine)) void {
68        var prose = self.prose;
69        self.prose = .empty;
70        if (tail.len > 0) prose.append(self.a, tail) catch {};
71        if (prose.items.len == 0) return;
72        const joined = std.fmt.allocPrint(out_a, "{s}\n", .{text.join(out_a, prose.items, "\n") catch ""}) catch "";
73        emitHighlighted(out_a, highlight.highlightMarkdown(out_a, format.formatTables(out_a, joined)), out);
74    }
75
76    fn releaseFence(self: *MarkdownStream, out_a: std.mem.Allocator, closing: ?[]const u8, out: *std.ArrayList(BodyLine)) void {
77        const f = self.fence.?;
78        self.fence = null;
79        out.append(out_a, .{ .text = highlight.highlightMarkdown(out_a, f.lines.items[0]) }) catch {};
80        if (f.lines.items.len > 1) {
81            const body = text.join(out_a, f.lines.items[1..], "\n") catch "";
82            var it = std.mem.splitScalar(u8, highlight.highlightCode(out_a, body), '\n');
83            while (it.next()) |l| out.append(out_a, .{ .text = l }) catch {};
84        }
85        if (closing) |c| out.append(out_a, .{ .text = highlight.highlightMarkdown(out_a, c) }) catch {};
86    }
87
88    pub fn flush(self: *MarkdownStream, out_a: std.mem.Allocator) []BodyLine {
89        var out: std.ArrayList(BodyLine) = .empty;
90        if (self.fence) |*f| {
91            if (self.rest.items.len > 0) f.lines.append(self.a, self.rest.items) catch {};
92            self.releaseFence(out_a, null, &out);
93        } else {
94            self.releaseProse(out_a, self.norm.line(self.a, self.rest.items), &out);
95        }
96        self.reset();
97        return out.items;
98    }
99
100    pub fn reset(self: *MarkdownStream) void {
101        self.rest = .empty;
102        self.fence = null;
103        self.prose = .empty;
104        self.norm.reset();
105        _ = self.arena.reset(.retain_capacity);
106        self.a = self.arena.allocator();
107    }
108};
109
110fn emitHighlighted(a: std.mem.Allocator, source: []const u8, out: *std.ArrayList(BodyLine)) void {
111    const nl = std.mem.lastIndexOfScalar(u8, source, '\n') orelse {
112        out.append(a, .{ .text = source }) catch {};
113        return;
114    };
115    const reset = source[nl + 1 ..];
116    var lines: std.ArrayList([]const u8) = .empty;
117    var it = std.mem.splitScalar(u8, source[0..nl], '\n');
118    while (it.next()) |l| lines.append(a, l) catch {};
119    if (lines.items.len > 0) {
120        const last = lines.items.len - 1;
121        lines.items[last] = std.fmt.allocPrint(a, "{s}{s}", .{ lines.items[last], reset }) catch lines.items[last];
122    }
123    for (lines.items) |l| out.append(a, .{ .text = l }) catch {};
124}