summaryrefslogtreecommitdiff
path: root/src/edit_cmd.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 12:40:11 -0300
committerGabriel Schneider <[email protected]>2026-11-06 14:56:36 -0300
commita66a9eb6e0c69cfcb63717bc27704cdc96f382b4 (patch)
tree400e3034a6c71c1d15d0ad302e1293a4290071b9 /src/edit_cmd.zig
parent36a51ddc67d0beb7d9851fb2425cf780a47fed36 (diff)
downloadpardes-a66a9eb6e0c69cfcb63717bc27704cdc96f382b4.tar.gz
pardes-a66a9eb6e0c69cfcb63717bc27704cdc96f382b4.zip
idioms #11, the command layer: exec, builtins, edit_cmd, Messages, Dir, Output, Jump and Pipe say their messages through buffers proven big enough (or the long argument cut) instead of placeholder text, replId takes its 48-byte buffer, a failed Save path, Get or Incl path, Repl send, Tutor, +Unsaved, pipe or Edit output on OOM is reported (reportError) rather than silently dropped, Edit's why and zonWhy cut a long text instead of losing it, and writeFailure names its set
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/edit_cmd.zig')
-rw-r--r--src/edit_cmd.zig28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/edit_cmd.zig b/src/edit_cmd.zig
index 207dd7cc..1a99e76c 100644
--- a/src/edit_cmd.zig
+++ b/src/edit_cmd.zig
@@ -125,7 +125,9 @@ const World = struct {
}
fn said(comptime T: type, why: *sam.Why, comptime fmt: []const u8, args: anytype) ?T {
- why.len = if (std.fmt.bufPrint(&why.buf, fmt, args)) |s| s.len else |_| 0;
+ var w: std.Io.Writer = .fixed(&why.buf);
+ w.print(fmt, args) catch {}; // a long path is cut at the room's end
+ why.len = w.end;
return null;
}
};
@@ -176,7 +178,7 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void {
var why: sam.Why = .{};
var text: [260]u8 = undefined;
const res = sam.run(arena, files.items, cur, command, .{ .ctx = &world, .check = World.check, .open = World.open, .read = World.read, .refuseGet = World.refuseGet }, &why) catch |err| switch (err) {
- error.Edit => return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: {s}", .{why.text()}) catch "Edit: failed"),
+ error.Edit => return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: {s}", .{why.text()}) catch unreachable), // why's 200 fit 260
error.OutOfMemory => return p.reportError(id, "Edit", err),
};
// acme's Edit blocks the editor while its commands run; here the editor
@@ -192,7 +194,7 @@ pub fn run(p: *Pardes, id: usize, command: []const u8) void {
if (!touches) continue;
const q = p.panes[f.id] orelse continue;
for (running.res.files, 0..) |g, j| if (g.id == f.id and running.serials[j] == q.serial and changes(g))
- return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: busy: {s}: an Edit's commands are running on it", .{f.name}) catch "Edit: busy");
+ return p.reportFailure(id, std.fmt.bufPrint(&text, "Edit: busy: {s}: an Edit's commands are running on it", .{f.name[0..@min(f.name.len, 200)]}) catch unreachable); // 200 and 50 fit 260
}
}
if (res.undo != 0) {
@@ -328,7 +330,7 @@ pub fn finish(p: *Pardes, success: bool, outputs: []const []const u8, failure: ?
const why_at = out.written().len;
Pipe.writeFailure(w, failure) catch break :report;
const why = std.mem.sliceTo(out.written()[why_at..], '\n');
- const said = std.fmt.bufPrint(&line, "Edit: {c} {s}: {s}", .{ job.c, job.command[0..@min(job.command.len, 120)], why }) catch "Edit: a command failed";
+ const said = std.fmt.bufPrint(&line, "Edit: {c} {s}: {s}", .{ job.c, job.command[0..@min(job.command.len, 120)], why[0..@min(why.len, 120)] }) catch unreachable; // 240 and 10 fit 256
const was = p.view().active;
_ = ninep_pane.appendErrors(p, at, out.written());
if (p.panes[was] != null) p.view().active = was;
@@ -345,10 +347,12 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void {
var text: [320]u8 = undefined;
for (res.files, 0..) |f, i| {
if (f.ops.items.len == 0 and !f.close and !f.renamed and !f.moved and f.get == null) continue;
- const pane = p.panes[f.id] orelse return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{f.name}) catch "Edit: a pane closed");
- if (pane.serial != pd.serials[i]) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{f.name}) catch "Edit: a pane closed");
+ // unreachable: 256 of the name and 56 fit 320.
+ const name = f.name[0..@min(f.name.len, 256)];
+ const pane = p.panes[f.id] orelse return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{name}) catch unreachable);
+ if (pane.serial != pd.serials[i]) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} closed while its commands ran; nothing changed", .{name}) catch unreachable);
if ((f.ops.items.len > 0 or f.get != null) and pane.file.?.revision != pd.revisions[i])
- return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} changed while its commands ran; nothing changed", .{f.name}) catch "Edit: a file changed");
+ return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: {s} changed while its commands ran; nothing changed", .{name}) catch unreachable);
}
// e first, Get's way: read before anything is changed, so one that
// cannot be read changes nothing; its own undo step puts the old name
@@ -357,7 +361,7 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void {
const path = f.get orelse continue;
var failed: anyerror = error.Unexpected;
const errno = @import("ninep/ctl.zig").getFile(p, p.panes[f.id].?, path, &failed);
- if (errno != 0) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: e {s}: {s}", .{ path, if (errno == @import("ninep/tree.zig").E.NOENT) "no such file" else "cannot be read" }) catch "Edit: e failed");
+ if (errno != 0) return p.reportFailure(at, std.fmt.bufPrint(&text, "Edit: e {s}: {s}", .{ path[0..@min(path.len, 256)], if (errno == @import("ninep/tree.zig").E.NOENT) "no such file" else "cannot be read" }) catch unreachable); // 256 and 24 fit 320
}
// `w`'s bytes, written after this step, before anything else is. To
// its own file changed on disk since it was read, as Save is: refused
@@ -422,11 +426,11 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void {
var from: usize = 0;
for (res.jobs, 0..) |job, j| {
if (job.c != '>') continue;
- out.appendSlice(p.gpa, res.printed[from..job.at]) catch return;
- out.appendSlice(p.gpa, outputs[j]) catch return;
+ out.appendSlice(p.gpa, res.printed[from..job.at]) catch |err| return p.reportError(at, "Edit", err);
+ out.appendSlice(p.gpa, outputs[j]) catch |err| return p.reportError(at, "Edit", err);
from = job.at;
}
- out.appendSlice(p.gpa, res.printed[from..]) catch return;
+ out.appendSlice(p.gpa, res.printed[from..]) catch |err| return p.reportError(at, "Edit", err);
// Printed, the keyboard stays where it was: a +Errors made for it
// does not take it, so the next Edit lands where this one did.
const was = p.view().active;
@@ -435,7 +439,7 @@ fn commit(p: *Pardes, pd: *Pending, outputs: []const []const u8) void {
}
if (refused.written().len > 0) {
var said: [320]u8 = undefined;
- p.reportFailure(at, std.fmt.bufPrint(&said, "Edit: w: {s} modified on disk since read (w again to overwrite)", .{refused.written()[0..@min(refused.written().len, 200)]}) catch "Edit: w: modified on disk since read (w again to overwrite)");
+ p.reportFailure(at, std.fmt.bufPrint(&said, "Edit: w: {s} modified on disk since read (w again to overwrite)", .{refused.written()[0..@min(refused.written().len, 200)]}) catch unreachable); // 200 and 66 fit 320
}
// D's panes last, each as Del closes one: an edited one is warned about
// once, and closes on the next.