xonecas.io
79113
Ma

mini-coding-agent / files

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

5.0 KB
1const std = @import("std");
2const styles = @import("styles.zig");
3const platform = @import("../platform.zig");
4const render = @import("render.zig");
5
6pub const LiveRegion = struct {
7    widths: std.ArrayList(usize) = .empty,
8    caret_line: usize = 0,
9    caret_cell: usize = 0,
10    drawn: bool = false,
11
12    fn remember(self: *LiveRegion, lines: []const []const u8, caret_line: usize, caret_cell: usize) void {
13        self.widths.clearRetainingCapacity();
14        for (lines) |line| self.widths.append(platform.gpa, render.displayWidth(line)) catch {};
15        self.caret_line = caret_line;
16        self.caret_cell = caret_cell;
17        self.drawn = true;
18    }
19
20    fn caretCell(self: *LiveRegion, width: usize) usize {
21        const w = if (self.caret_line < self.widths.items.len) self.widths.items[self.caret_line] else 0;
22        if (self.caret_cell > 0 and self.caret_cell == w and self.caret_cell % width == 0) return self.caret_cell - 1;
23        return self.caret_cell;
24    }
25
26    fn caretRow(self: *LiveRegion, width: usize) usize {
27        var row: usize = 0;
28        const lines = @min(self.caret_line, self.widths.items.len);
29        for (self.widths.items[0..lines]) |cells| row += render.rowsForCells(cells, width);
30        return row + self.caretCell(width) / width;
31    }
32
33    fn totalRows(self: *LiveRegion, width: usize) usize {
34        var row: usize = 0;
35        for (self.widths.items) |cells| row += render.rowsForCells(cells, width);
36        return row;
37    }
38
39    pub fn erase(self: *LiveRegion, a: std.mem.Allocator, width: usize) []const u8 {
40        if (!self.drawn) return "";
41        const up = self.caretRow(width);
42        var out: std.ArrayList(u8) = .empty;
43        if (up > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}A", .{up}) catch "") catch {};
44        out.appendSlice(a, "\r") catch {};
45        out.appendSlice(a, styles.SGR_PLAIN) catch {};
46        out.appendSlice(a, "\x1b[J") catch {};
47        return out.items;
48    }
49
50    pub fn draw(self: *LiveRegion, a: std.mem.Allocator, width: usize, lines: []const []const u8, caret_line: usize, caret_cell: usize, above: []const u8) []const u8 {
51        var out: std.ArrayList(u8) = .empty;
52        out.appendSlice(a, self.erase(a, width)) catch {};
53        out.appendSlice(a, above) catch {};
54        for (lines, 0..) |line, i| {
55            if (i > 0) out.appendSlice(a, "\r\n") catch {};
56            out.appendSlice(a, render.paintRow(a, line)) catch {};
57        }
58        self.remember(lines, caret_line, caret_cell);
59        const end_row = self.totalRows(width) - 1;
60        const caret_row = self.caretRow(width);
61        const up = if (end_row > caret_row) end_row - caret_row else 0;
62        if (up > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}A", .{up}) catch "") catch {};
63        out.appendSlice(a, "\r") catch {};
64        const col = self.caretCell(width) % width;
65        if (col > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}C", .{col}) catch "") catch {};
66        return out.items;
67    }
68};
69
70pub const Scrollback = struct {
71    a: std.mem.Allocator = undefined,
72    scratch: std.mem.Allocator = undefined,
73    buf: std.ArrayList(u8) = .empty,
74    wrote: bool = false,
75    last_blank: bool = false,
76    separator: bool = false,
77
78    pub fn clear(self: *Scrollback) void {
79        self.buf.clearRetainingCapacity();
80    }
81
82    fn appendBytes(self: *Scrollback, bytes: []const u8) void {
83        self.buf.appendSlice(self.a, bytes) catch {};
84    }
85
86    fn appendRow(self: *Scrollback, bytes: []const u8) void {
87        self.appendBytes(bytes);
88        self.appendBytes("\r\n");
89        self.wrote = true;
90        self.last_blank = false;
91    }
92
93    pub fn push(self: *Scrollback, line: []const u8) void {
94        const clean = render.sanitize(self.scratch, line);
95        const blank = clean.len == 0 or (self.separator and self.wrote);
96        self.separator = false;
97        if (blank and self.wrote and !self.last_blank) {
98            self.appendBytes(render.paintRow(self.scratch, ""));
99            self.appendBytes("\r\n");
100            self.last_blank = true;
101        }
102        if (clean.len != 0) self.appendRow(render.paintRow(self.scratch, clean));
103    }
104
105    pub fn pushRaw(self: *Scrollback, bytes: []const u8) void {
106        if (bytes.len == 0) return;
107        self.separator = false;
108        self.appendRow(bytes);
109    }
110
111    pub fn commitLines(self: *Scrollback, lines: []const render.BodyLine) void {
112        for (lines) |line| self.push(render.styleLine(self.scratch, line, .{}));
113    }
114
115    pub fn note(self: *Scrollback, line: []const u8) void {
116        self.separator = true;
117        self.push(line);
118        self.separator = true;
119    }
120
121    pub fn commitUser(self: *Scrollback, text: []const u8) void {
122        self.separator = true;
123        var it = std.mem.splitScalar(u8, text, '\n');
124        var lines: std.ArrayList(render.BodyLine) = .empty;
125        while (it.next()) |l| lines.append(self.scratch, .{ .text = l, .style = styles.roles.prompt }) catch {};
126        self.commitLines(lines.items);
127        self.separator = true;
128    }
129};