xonecas.io
79113
Ma

mini-coding-agent / files

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

4.8 KB
1const std = @import("std");
2const styles = @import("styles.zig");
3const render = @import("render.zig");
4const diff_view = @import("diff_view.zig");
5
6const BodyLine = render.BodyLine;
7
8pub const PendingCall = struct { name: []const u8, summary: []const u8 };
9
10fn callHead(a: std.mem.Allocator, name: []const u8) []const u8 {
11    return styles.accent(a, std.fmt.allocPrint(a, "{s}", .{name}) catch "tool");
12}
13
14pub fn diffHead(a: std.mem.Allocator, path: []const u8) []const u8 {
15    return std.fmt.allocPrint(a, "{s}  {s}", .{ callHead(a, "diff"), path }) catch path;
16}
17
18pub fn relativize(cwd: []const u8, path: []const u8) []const u8 {
19    if (!std.fs.path.isAbsolute(path) or cwd.len == 0) return path;
20    if (!std.mem.startsWith(u8, path, cwd)) return path;
21    const rest = path[cwd.len..];
22    if (rest.len == 0) return ".";
23    if (rest[0] != '/') return path;
24    return rest[1..];
25}
26
27fn preview(a: std.mem.Allocator, command: []const u8) []const u8 {
28    const end = std.mem.indexOfScalar(u8, command, '\n') orelse return command;
29    if (std.mem.indexOfNone(u8, command[end..], " \t\r\n") == null) return command[0..end];
30    const first = std.mem.trimEnd(u8, command[0..end], " \t\r");
31    return std.fmt.allocPrint(a, "{s} …", .{first}) catch first;
32}
33
34pub fn callSummary(a: std.mem.Allocator, name: []const u8, args_json: []const u8) []const u8 {
35    const parsed = std.json.parseFromSliceLeaky(std.json.Value, a, args_json, .{}) catch return args_json;
36    if (parsed == .object) {
37        if (std.mem.eql(u8, name, "bash")) {
38            if (parsed.object.get("command")) |c| if (c == .string) return preview(a, c.string);
39        }
40        if (std.mem.eql(u8, name, "edit") or std.mem.eql(u8, name, "read")) {
41            if (parsed.object.get("path")) |p| if (p == .string) return p.string;
42        }
43    }
44    return args_json;
45}
46
47/// One line: the tool name, a clipped preview of its arguments, and the
48/// result summary when there is one.
49pub fn callLine(a: std.mem.Allocator, name: []const u8, summary: []const u8, meta: ?[]const u8, width: usize) []const u8 {
50    const head = std.fmt.allocPrint(a, "{s}  ", .{callHead(a, name)}) catch return summary;
51    var room = width -| render.displayWidth(head);
52    var tail: []const u8 = "";
53    if (meta) |m| {
54        const raw = std.fmt.allocPrint(a, "  {s}", .{m}) catch "";
55        tail = styles.comment(a, render.clip(a, raw, room));
56        room -|= render.displayWidth(tail);
57    }
58    const args = styles.styledWith(a, .{ .bold = true }, render.clip(a, render.expandTabs(a, summary, 4), room));
59    return std.fmt.allocPrint(a, "{s}{s}{s}", .{ head, args, tail }) catch summary;
60}
61
62pub const Result = struct {
63    lines: []const BodyLine,
64    footer: ?[]const u8 = null,
65};
66
67pub fn diffFooter(a: std.mem.Allocator, r: diff_view.Rendered) ?[]const u8 {
68    if (r.added == 0 and r.removed == 0) return null;
69    const add = std.fmt.allocPrint(a, "+{d}", .{r.added}) catch return null;
70    const del = std.fmt.allocPrint(a, "-{d}", .{r.removed}) catch return null;
71    return std.fmt.allocPrint(a, "{s}{s} {s}", .{
72        styles.comment(a, " └ "),
73        styles.styledWith(a, styles.roles.diff_add, add),
74        styles.styledWith(a, styles.roles.diff_delete, del),
75    }) catch null;
76}
77
78pub fn resultLines(a: std.mem.Allocator, name: []const u8, text: []const u8, max_diff_lines: usize) Result {
79    const trimmed = std.mem.trimEnd(u8, text, " \t\r\n");
80    var lines: std.ArrayList([]const u8) = .empty;
81    var it = std.mem.splitScalar(u8, trimmed, '\n');
82    while (it.next()) |l| lines.append(a, l) catch {};
83    if (std.mem.eql(u8, name, "bash")) {
84        if (lines.items.len > 0 and std.mem.startsWith(u8, lines.items[lines.items.len - 1], "exit code: ")) {
85            _ = lines.pop();
86        }
87    } else if (std.mem.eql(u8, name, "edit")) {
88        const verb = if (std.mem.startsWith(u8, lines.items[0], "edited "))
89            "edited "
90        else if (std.mem.startsWith(u8, lines.items[0], "created "))
91            "created "
92        else
93            "";
94        if (verb.len > 0) {
95            _ = lines.orderedRemove(0);
96            const r = diff_view.diffLines(a, lines.items, max_diff_lines);
97            return .{ .lines = r.lines, .footer = diffFooter(a, r) };
98        }
99    }
100    var out: std.ArrayList(BodyLine) = .empty;
101    for (lines.items) |l| out.append(a, .{ .text = l }) catch {};
102    return .{ .lines = out.items };
103}
104
105pub fn exitLine(a: std.mem.Allocator, text: []const u8) ?[]const u8 {
106    const trimmed = std.mem.trimEnd(u8, text, " \t\r\n");
107    const last = if (std.mem.lastIndexOfScalar(u8, trimmed, '\n')) |i| trimmed[i + 1 ..] else trimmed;
108    if (!std.mem.startsWith(u8, last, "exit code: ")) return null;
109    const label = std.fmt.allocPrint(a, " └ exit {s}", .{last["exit code: ".len..]}) catch return null;
110    return styles.comment(a, label);
111}