diff options
Diffstat (limited to 'src/ninep/pane.zig')
| -rw-r--r-- | src/ninep/pane.zig | 93 |
1 files changed, 91 insertions, 2 deletions
diff --git a/src/ninep/pane.zig b/src/ninep/pane.zig index 3fa45404..3f62aef9 100644 --- a/src/ninep/pane.zig +++ b/src/ninep/pane.zig @@ -12,6 +12,7 @@ const modal = @import("../modal.zig"); const config = @import("../config.zig"); const fs = @import("../fs.zig"); const tree = @import("tree.zig"); +const locations = @import("../locations.zig"); const events = @import("events.zig"); const addressing = @import("addr.zig"); const ctl = @import("ctl.zig"); @@ -319,8 +320,8 @@ pub fn fileSize(p: *Pardes, id: usize, f: PaneFile) u64 { // A terminal's body reads its screen and history, as text. .body => if (pane.file == null and pane.isTerminal()) panes.terminal.screenTextLen(pane) else if (kindText(p, pane)) |t| t.len else bodyOf(pane).len, // What addr selects, generated by each read: no length, as acme's - // data and xdata have none. - .data, .xdata => 0, + // data and xdata have none; and rows, made by each read. + .data, .xdata, .rows => 0, .tag => if (tagOf(p, pane)) |tag| tag.len else |_| 0, // a stat's length is a hint: none when the tag cannot be made .name => nameOf(p, pane).len + 1, .ctl => ctl: { @@ -396,6 +397,12 @@ pub fn read(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply .mark => readFlag(p, req, !pf.nomark), .scroll => readFlag(p, req, !pf.noscroll), .look, .exec, .tagexec => ctl.readResults(p, req), + .rows => rows: { + const f = fileOf(pane) orelse break :rows tree.window(p, req, ""); + var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); + locations.formatRows(f.location_rows, &text.writer) catch break :rows Reply.fail(req.tag, E.NOMEM); + break :rows tree.window(p, req, text.written()); + }, .event => events.readQueue(p, req, &pf.events), .pty_status => pty.readStatus(p, req, id, pane), .pty_data => pty.readData(p, req, pf), @@ -498,6 +505,7 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply // A click in its tag: an event reader holding the pane hears it as // an `x` record (ctl.run), as acme's tag clicks are. .tagexec => ctl.command(p, req, pane.serial, true, true), + .rows => writeRows(p, req, pane), .event => events.writeEvent(p, req, id), .errors => if (appendErrors(p, id, req.data)) |took| .{ .tag = req.tag, .written = @intCast(took) } @@ -510,6 +518,36 @@ pub fn write(p: *Pardes, req: Req, id: usize, pane: *Pane, file: PaneFile) Reply }; } +pub const e_rows_line = "invalid rows: a row is a line, ending in a newline"; +pub const e_rows_text = "invalid rows: a line is no row (the README's rows line says what one is)"; +pub const e_rows_fit = "invalid rows: more rows than the text has lines, or one past its line's end"; + +/// Rows, whole lines of them (a write through a mount is cut anywhere: the +/// rest of a line waits for the open's next write, tree.writeLines): at +/// offset 0 they replace the pane's, at a later one they follow those it +/// holds. Parsed (locations.parseRows) and handed to File.setRows, which +/// refuses what the text cannot hold. +fn writeRows(p: *Pardes, req: Req, pane: *Pane) Reply { + const f = fileOf(pane) orelse return Reply.fail(req.tag, E.PERM); + if (req.data.len == 0) return .{ .tag = req.tag, .written = 0 }; + if (req.data[req.data.len - 1] != '\n') return tree.failText(req.tag, E.INVAL, e_rows_line); + const arena = p.scratch.allocator(); + const written = locations.parseRows(arena, req.data) catch |err| return switch (err) { + error.OutOfMemory => Reply.fail(req.tag, E.NOMEM), + error.BadRows => tree.failText(req.tag, E.INVAL, e_rows_text), + }; + const kept = if (req.off == 0) 0 else f.location_rows.len; + const all = arena.alloc(locations.Row, kept + written.len) catch return Reply.fail(req.tag, E.NOMEM); + @memcpy(all[0..kept], f.location_rows[0..kept]); + @memcpy(all[kept..], written); + const rows = locations.cloneRows(p.gpa, all) catch return Reply.fail(req.tag, E.NOMEM); + panes.File.setRows(p, f, rows) catch { + locations.freeRows(p.gpa, rows); + return tree.failText(req.tag, E.INVAL, e_rows_fit); + }; + return .{ .tag = req.tag, .written = @intCast(req.data.len) }; +} + /// spliceBody for a write of the open `handle`, in the undo step its open /// is making: it joins the step being made only when it is the same open's /// (or the step is anyone's, `joined_by` 0), another open's starting its @@ -981,6 +1019,8 @@ pub fn truncate(p: *Pardes, pane: *Pane, file: PaneFile) tree.Status { pf.limit = null; }, .dot => pf.dot_truncated = fileOf(pane) != null, + // `>` starts the rows over, as a write at 0 does. + .rows => if (fileOf(pane)) |f| panes.File.setRows(p, f, &.{}) catch unreachable, else => {}, } return .ok; @@ -1248,3 +1288,52 @@ test "index names a command's pane cmd, a shell's term" { const shell = try p.newShell(p.freeSlot().?, "/tmp"); try testing.expectEqualStrings("term", kindOf(shell)); } + +test "rows: a pane's rows read as lines, written whole or across writes, refused when its text cannot hold them" { + const gpa = testing.allocator; + const p = try withFile(gpa, "a.txt:3 one\n two\n"); + defer p.deinit(); + const rows = Node.of(serialOf(p), .rows); + try testing.expectEqualStrings("", rd(p, rows, 0, 4096).bytes); + const text = "m 3 0 0 0 0 7 8 3:0+3=keyword.add a.txt\ncd 0 0 0 0 0 0 0 - \n"; + try testing.expectEqual(Status.ok, wr(p, rows, text).reply.status); + try testing.expectEqualStrings(text, rd(p, rows, 0, 4096).bytes); + const f = &p.panes[0].?.file.?; + try testing.expectEqual(@as(usize, 2), f.location_rows.len); + try testing.expectEqualStrings("a.txt", f.location_rows[0].path); + try testing.expect(f.location_rows[1].declaration and f.location_rows[1].kind == .context); + // A write at 0 starts them over; one open's writes, cut mid-line, + // make one set of rows, its later writes following its first. + const h = call(p, .{ .tag = 1, .op = .open, .node = rows, .omode = 1 }).reply.handle; + try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = rows, .handle = h, .off = 0, .data = "m 3 0 0 0 0 7 8 - a.t" }).reply.status); + try testing.expectEqual(Status.ok, call(p, .{ .tag = 3, .op = .write, .node = rows, .handle = h, .off = 21, .data = "xt\np 4 0 0 0 0 0 0 - a.txt\n" }).reply.status); + _ = call(p, .{ .tag = 4, .op = .release, .node = rows, .handle = h }); + try testing.expectEqualStrings("m 3 0 0 0 0 7 8 - a.txt\np 4 0 0 0 0 0 0 - a.txt\n", rd(p, rows, 0, 4096).bytes); + // More rows than lines, an offset or styles past a line's end, a line + // that is no row: refused, the rows as they were. + for ([_][]const u8{ "m 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\nm 1 0 0 0 0 0 0 - a\n", "m 1 0 0 0 0 12 0 - a\n", "m 1 0 0 0 0 0 8 9:0+1=add a\n", "q 1 0 0 0 0 0 0 - a\n", "m 1 0 0 0 0 0 0 - a" }) |bad| + try testing.expectEqual(Status.err, wr(p, rows, bad).reply.status); + try testing.expectEqual(@as(usize, 2), f.location_rows.len); + // `>` truncates: no rows. + _ = call(p, .{ .tag = 5, .op = .setattr, .node = rows, .truncate = true }); + try testing.expectEqualStrings("", rd(p, rows, 0, 4096).bytes); + // An edit drops them. + try testing.expectEqual(Status.ok, wr(p, rows, text).reply.status); + try testing.expectEqual(Status.ok, wr(p, Node.of(serialOf(p), .body), "more\n").reply.status); + try testing.expectEqual(@as(usize, 0), f.location_rows.len); +} + +test "rows: a +Search's rows read back as the addresses its text shows" { + const gpa = testing.allocator; + const p = try withFile(gpa, "x\n"); + defer p.deinit(); + try panes.Output.fillResults(p, 0, "/tmp", .search, "foo", try p.gpa.dupe(u8, "a.txt:1:1 foo\nb.txt:2:3 foo\n"), null); + const serial = for (p.panes) |slot| { + const pane = slot orelse continue; + const f = pane.file orelse continue; + if (f.output != null and f.location_rows.len > 0) break pane.serial; + } else return error.NoSearch; + const text = rd(p, Node.of(serial, .rows), 0, 4096).bytes; + try testing.expect(std.mem.indexOf(u8, text, "m 1 1 ") == 0); + try testing.expect(std.mem.indexOf(u8, text, " a.txt\n") != null and std.mem.indexOf(u8, text, "\nm 2 3 ") != null and std.mem.endsWith(u8, text, " b.txt\n")); +} |
