diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-13 01:24:16 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-13 01:34:46 -0300 |
| commit | 5a40dca5adb6e9e650f55f3ec6f254a13bf50493 (patch) | |
| tree | 5fdffebd75ed3fbac1eae5dec0a3f2d491450342 /gui/src/Sources.zig | |
| parent | 4b4113a0217ee27c78e7efddb66ff5a455d49083 (diff) | |
| download | computeranim-5a40dca5adb6e9e650f55f3ec6f254a13bf50493.tar.gz computeranim-5a40dca5adb6e9e650f55f3ec6f254a13bf50493.zip | |
debugger: align source and add rich Vim-style navigation
Diffstat (limited to 'gui/src/Sources.zig')
| -rw-r--r-- | gui/src/Sources.zig | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/gui/src/Sources.zig b/gui/src/Sources.zig new file mode 100644 index 0000000..33268c5 --- /dev/null +++ b/gui/src/Sources.zig @@ -0,0 +1,34 @@ +//! Owned, bounded source cache. No animation-global cache can outlive these bytes. +const std = @import("std"); +const canim = @import("canim"); +const zt = @import("ztrace"); +const Sources = @This(); +const File = struct { id: u64, path: []const u8, bytes: []const u8, text: ?canim.source.Text }; +files: std.ArrayList(File) = .empty, + +pub fn close(s: *Sources, gpa: std.mem.Allocator) void { + for (s.files.items) |file| { + if (file.text) |t| t.deinit(); + gpa.free(file.path); + gpa.free(file.bytes); + } + s.files.deinit(gpa); + s.* = .{}; +} + +pub fn get(s: *Sources, gpa: std.mem.Allocator, io: std.Io, m: zt.store.Moment, id: u64) ?*const File { + for (s.files.items) |*file| if (file.id == id) return file; + if (s.files.items.len == 32) return null; + var buffer: [4096]u8 = undefined; + const name = (m.string(id, &buffer) catch null) orelse return null; + const path = gpa.dupe(u8, name) catch return null; + const bytes = std.Io.Dir.cwd().readFileAlloc(io, path, gpa, .limited(8 * 1024 * 1024)) catch &.{}; + const text = if (bytes.len > 0) canim.source.prepare(bytes, path) catch null else null; + s.files.append(gpa, .{ .id = id, .path = path, .bytes = bytes, .text = text }) catch { + if (text) |t| t.deinit(); + gpa.free(path); + gpa.free(bytes); + return null; + }; + return &s.files.items[s.files.items.len - 1]; +} |
