xonecas.io
79113
Ma

mini-coding-agent / files

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

2.7 KB
1const std = @import("std");
2const filesystem = @import("../filesystem.zig");
3
4pub const Store = struct {
5    path: []const u8,
6    max: usize,
7    entries: std.ArrayList([]const u8) = .empty,
8    a: std.mem.Allocator,
9
10    pub fn init(a: std.mem.Allocator, path: []const u8, max: usize) Store {
11        var self = Store{ .path = path, .max = @max(max, 1), .a = a };
12        self.load();
13        return self;
14    }
15
16    fn load(self: *Store) void {
17        const raw = filesystem.readFileAlloc(self.a, self.path, 1 << 20) catch return;
18        var it = std.mem.splitScalar(u8, raw, '\n');
19        while (it.next()) |line| {
20            const trimmed = std.mem.trim(u8, line, "\r");
21            if (trimmed.len == 0) continue;
22            const decoded = self.unescape(trimmed) catch continue;
23            if (decoded.len == 0) continue;
24            self.entries.append(self.a, self.a.dupe(u8, decoded) catch return) catch return;
25        }
26    }
27
28    pub fn add(self: *Store, text: []const u8) void {
29        const trimmed = std.mem.trim(u8, text, " \t\r\n");
30        if (trimmed.len == 0) return;
31        for (self.entries.items, 0..) |e, i| {
32            if (std.mem.eql(u8, e, trimmed)) {
33                _ = self.entries.orderedRemove(i);
34                break;
35            }
36        }
37        self.entries.append(self.a, self.a.dupe(u8, trimmed) catch return) catch return;
38        while (self.entries.items.len > self.max) _ = self.entries.orderedRemove(0);
39        self.flush();
40    }
41
42    fn escape(self: *Store, e: []const u8) []const u8 {
43        var out: std.ArrayList(u8) = .empty;
44        for (e) |c| {
45            if (c == '\\' or c == '\n') out.append(self.a, '\\') catch return e;
46            out.append(self.a, if (c == '\n') 'n' else c) catch return e;
47        }
48        return out.items;
49    }
50
51    fn unescape(self: *Store, s: []const u8) ![]const u8 {
52        if (std.mem.indexOfScalar(u8, s, '\\') == null) return s;
53        var out: std.ArrayList(u8) = .empty;
54        var i: usize = 0;
55        while (i < s.len) {
56            if (s[i] == '\\' and i + 1 < s.len) {
57                i += 1;
58                out.append(self.a, if (s[i] == 'n') '\n' else s[i]) catch return s;
59            } else {
60                out.append(self.a, s[i]) catch return s;
61            }
62            i += 1;
63        }
64        return out.items;
65    }
66
67    fn flush(self: *Store) void {
68        const start = if (self.entries.items.len > self.max) self.entries.items.len - self.max else 0;
69        var out: std.ArrayList(u8) = .empty;
70        for (self.entries.items[start..]) |e| {
71            out.appendSlice(self.a, self.escape(e)) catch return;
72            out.append(self.a, '\n') catch return;
73        }
74        filesystem.writeFile(self.path, out.items) catch {};
75    }
76};