diff options
48 files changed, 607 insertions, 562 deletions
diff --git a/abi/wire.zig b/abi/wire.zig index cf3daf2e..b2d32033 100644 --- a/abi/wire.zig +++ b/abi/wire.zig @@ -194,7 +194,7 @@ pub const ServerMsg = union(ServerTag) { /// dump that follows is, and the session's offset from UTC (a replica /// guesses the session's time of day). A far client is told how many /// blobs the dump holds (`blobs`). - welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0, blobs: u32 = 0 }, + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: pardes.Pardes.View.Id = .at(0), dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0, blobs: u32 = 0 }, refused: []const u8, pong: u64, dump: []const u8, @@ -223,7 +223,7 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .pid = try r.takeInt(u32, .little), .session = try r.take(try r.takeInt(u16, .little)), .client = try r.takeInt(u16, .little), - .view = try r.takeByte(), + .view = .at(try r.takeByte()), .dump_len = try r.takeInt(u64, .little), .gmtoff = try r.takeInt(i32, .little), .digest = try r.takeInt(u64, .little), @@ -252,7 +252,7 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { try w.writeInt(u32, h.pid, .little); try putSlice16(&w, h.session); try w.writeInt(u16, h.client, .little); - try w.writeByte(h.view); + try w.writeByte(h.view.index()); try w.writeInt(u64, h.dump_len, .little); try w.writeInt(i32, h.gmtoff, .little); try w.writeInt(u64, h.digest, .little); diff --git a/core/Addon.zig b/core/Addon.zig index e84b122b..cb3dbf54 100644 --- a/core/Addon.zig +++ b/core/Addon.zig @@ -69,7 +69,7 @@ pub fn perform(p: *Pardes, op: Op, pane: u8, arg: []const u8) void { var words: [64]LoadedWord = undefined; var why: Why = .{}; const l = readLoaded(arg, &words, &why); - p.update(.{ .addon = .{ .op = op, .pane = pane, .kind = &l } }); + p.update(.{ .addon = .{ .op = op, .pane = .at(pane), .kind = &l } }); } /// What a load reads off the library at `path` (held): the `addon` @@ -727,12 +727,12 @@ pub fn run(p: *Pardes, id: usize, arg: ?[]const u8) void { const dir = std.mem.trimEnd(u8, if (p.panes[id]) |pane| Pardes.paneDir(pane) else p.sessionDir(), "/"); const path = if (std.Io.Dir.path.isAbsolute(rest)) rest else std.fmt.bufPrint(&target, "{s}/{s}", .{ dir, rest }) catch return p.reportFailure(id, "Addon: that path is too long"); if (path.len > pardes.effect_path_cap) return p.reportFailure(id, "Addon: that path is too long"); - return p.emit(.{ .addon = .{ .op = .load, .pane = @intCast(id), .arg = .from(path) } }); + return p.emit(.{ .addon = .{ .op = .load, .pane = .at(id), .arg = .from(path) } }); } const k = p.kinds.byName(rest) orelse return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: no addon brought kind {s}", .{ op, rest }) catch takes); const e = &p.kinds.kinds[k]; if (op == .reload and std.mem.eql(u8, e.path.slice(), "builtin")) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon reload: {s} is built into this pardes", .{rest}) catch takes); - if (op == .reload) return p.emit(.{ .addon = .{ .op = .reload, .pane = @intCast(id), .arg = .from(e.path.slice()) } }); + if (op == .reload) return p.emit(.{ .addon = .{ .op = .reload, .pane = .at(id), .arg = .from(e.path.slice()) } }); const n = openPanes(p, rest); if (n > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon unload: {d} {s} pane{s} open", .{ n, rest, if (n == 1) " is" else "s are" }) catch takes); const hash = e.hash; @@ -743,7 +743,7 @@ pub fn run(p: *Pardes, id: usize, arg: ?[]const u8) void { } fn release(p: *Pardes, hash: [32]u8) void { - p.emit(.{ .addon = .{ .op = .release, .pane = 0, .arg = .from(&hash) } }); + p.emit(.{ .addon = .{ .op = .release, .pane = .at(0), .arg = .from(&hash) } }); } pub fn openPanes(p: *const Pardes, name: []const u8) usize { diff --git a/core/Core.zig b/core/Core.zig index e3238a6e..282094b3 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -956,7 +956,7 @@ pub fn emitWrite(p: *Pardes, id: usize, bytes: []const u8) void { if (p.pending_write[id] == null) { while (off < bytes.len and p.effects_len < p.effects.len) { const n = @min(bytes.len - off, effect_write_cap); - p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(bytes[off .. off + n]) } }); + p.emit(.{ .write = .{ .pane = .at(id), .bytes = .from(bytes[off .. off + n]) } }); off += n; } } @@ -1278,7 +1278,7 @@ fn refillPendingWrites(p: *Pardes) void { // a copy's cursor and re-send the same chunk for ever. const pw = if (slot.*) |*live| live else break; const n = @min(pw.bytes.len - pw.off, effect_write_cap); - p.emit(.{ .write = .{ .pane = @intCast(id), .bytes = .from(pw.bytes[pw.off..][0..n]) } }); + p.emit(.{ .write = .{ .pane = .at(id), .bytes = .from(pw.bytes[pw.off..][0..n]) } }); pw.off += n; p.pending_write_bytes -= n; if (pw.off == pw.bytes.len) { @@ -1324,17 +1324,17 @@ fn nextQueued(p: *Pardes) ?Event { pub fn hostForegroundName(p: *const Pardes, id: usize, buf: []u8) ?[]const u8 { const f = p.host.vtable.fg_name orelse return null; - return f(p.host.ctx, @intCast(id), buf); + return f(p.host.ctx, .at(id), buf); } pub fn hostTtyTaken(p: *const Pardes, id: usize) bool { const f = p.host.vtable.tty_taken orelse return false; - return f(p.host.ctx, @intCast(id)); + return f(p.host.ctx, .at(id)); } pub fn hostKillJob(p: *const Pardes, id: usize) bool { const f = p.host.vtable.kill_job orelse return false; - return f(p.host.ctx, @intCast(id)); + return f(p.host.ctx, .at(id)); } /// A terminal whose shell could not be started: said, and a run waiting @@ -1626,8 +1626,8 @@ fn updateInner(p: *Pardes, ev: Event) void { const hover_stays = switch (ev) { .tick, .search => true, .mouse => |m| m.button == .none and m.kind == .motion, - .output => |o| look.lookHoverPane(p) != o.pane, - .file_changed => |changed| look.lookHoverPane(p) != changed.pane, + .output => |o| look.lookHoverPane(p) != o.pane.index(), + .file_changed => |changed| look.lookHoverPane(p) != changed.pane.index(), else => false, }; if (!hover_stays) { @@ -1665,30 +1665,30 @@ fn updateInner(p: *Pardes, ev: Event) void { p.resize_count += 1; }, .output => |o| { - const pane = p.panes[o.pane] orelse return; + const pane = p.panes[o.pane.index()] orelse return; pane.shell_spoke = true; - ctlfs.events.notePtyOutput(p, o.pane, o.bytes); + ctlfs.events.notePtyOutput(p, o.pane.index(), o.bytes); pane.feedOutput(p, o.bytes); - ctlfs.pty.noteMarks(p, o.pane, pane); - exec.flushReplEnter(p, o.pane, true); + ctlfs.pty.noteMarks(p, o.pane.index(), pane); + exec.flushReplEnter(p, o.pane.index(), true); }, - .exited => |e| if (p.panes[e.pane]) |pane| { + .exited => |e| if (p.panes[e.pane.index()]) |pane| { if (pane.command == null) ctlfs.pty.shellExited(p, pane, e.status) else if (!pane.command_done) - pane.commandDone(p, e.pane, e.status); + pane.commandDone(p, e.pane.index(), e.status); }, - .eof => |e| if (p.panes[e.pane]) |pane| if (pane.command != null) { + .eof => |e| if (p.panes[e.pane.index()]) |pane| if (pane.command != null) { // Output is over; the command is over when its child exits. pane.command_pty = false; - } else p.removePane(e.pane, null) catch |err| { + } else p.removePane(e.pane.index(), null) catch |err| { pane.body.mode = .normal; - p.reportError(.at(e.pane), "terminal exited; Del retries close", err); + p.reportError(e.pane, "terminal exited; Del retries close", err); }, .lsp_resp => |r| @import("lsp/Ask.zig").lspResponse(p, r.id, r.rows), .pipe_resp => |r| Pipe.pipeResponse(p, r.id, r.success, r.outputs, r.failure), .search => |b| Stream.receive(p, &b), - .file_changed => |fc| _ = p.applyWatchedFileChanged(fc.pane, fc.bytes), + .file_changed => |fc| _ = p.applyWatchedFileChanged(fc.pane.index(), fc.bytes), .key => |key| { p.can_ask = true; defer p.can_ask = false; @@ -1706,8 +1706,8 @@ fn updateInner(p: *Pardes, ev: Event) void { if (focused) if (p.host.vtable.newer_build) |f| if (f(p.host.ctx)) p.setMessage(.at(v.active), newer_build_words); }, .handed => |h| p.handover.finish(p, h.seq, h.to, h.failure), - .addon => |a| pardes.Addon.landed(p, a.op, a.pane, a.kind.*), - .job => |j| pardes.Kind.jobDone(p, j.pane, j.serial, j.token, j.gen, j.bytes), + .addon => |a| pardes.Addon.landed(p, a.op, a.pane.index(), a.kind.*), + .job => |j| pardes.Kind.jobDone(p, j.pane.index(), j.serial, j.token, j.gen, j.bytes), .frontend => |f| { v.frontend = f; }, @@ -1960,7 +1960,7 @@ pub fn sync(p: *Pardes) void { if (pane.isTerminal()) { pane.resizeGrid(cols, rows); p.shell_rows.markStale(pane); // reflow moved every row - p.emit(.{ .resize_pty = .{ .pane = @intCast(id), .cols = cols, .rows = rows } }); + p.emit(.{ .resize_pty = .{ .pane = .at(id), .cols = cols, .rows = rows } }); pane.fs.winsize = null; } pane.cols = cols; @@ -1987,7 +1987,7 @@ pub fn sync(p: *Pardes) void { .wait => {}, .never => pane.greet = false, .now => { - p.emit(.{ .resize_pty = .{ .pane = @intCast(id), .cols = pane.cols, .rows = pane.rows } }); + p.emit(.{ .resize_pty = .{ .pane = .at(id), .cols = pane.cols, .rows = pane.rows } }); p.emitWrite(id, "ls\r"); pane.greet = false; }, @@ -2333,7 +2333,8 @@ const FakeTtyQuery = struct { p.host = .{ .ctx = f, .vtable = &vtable }; } - fn answer(ctx: ?*anyopaque, pane: u8) bool { + fn answer(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + const pane = id.index(); const f: *FakeTtyQuery = @ptrCast(@alignCast(ctx.?)); f.asked += 1; return f.taken[pane]; @@ -2462,7 +2463,7 @@ test "pane-tag Exec prefers Term and argument builtins before shell fallback" { var sent_len: usize = 0; while (p.nextEffect()) |effect| switch (effect) { .write => |w| { - try std.testing.expectEqual(@as(u8, 0), w.pane); + try std.testing.expectEqual(0, w.pane.index()); try std.testing.expect(w.bytes.slice().len <= sent.len - sent_len); @memcpy(sent[sent_len..][0..w.bytes.slice().len], w.bytes.slice()); sent_len += w.bytes.slice().len; @@ -2684,7 +2685,7 @@ test "Term spawns a raw shell in the caller's directory" { var found = false; while (p.nextEffect()) |effect| switch (effect) { - .spawn => |s| if (@as(usize, s.pane) == free) { + .spawn => |s| if (s.pane.index() == free) { try std.testing.expectEqualStrings("/tmp/newtty-dir", s.cwd.slice()); found = true; }, @@ -2708,11 +2709,11 @@ test "a terminal respawned into a closed pane's slot hangs the old shell up firs var closed = false; var spawned = false; while (p.nextEffect()) |effect| switch (effect) { - .close_pty => |c| if (c.pane == slot) { + .close_pty => |c| if (c.pane.index() == slot) { try std.testing.expect(!spawned); closed = true; }, - .spawn => |s| if (s.pane == slot) { + .spawn => |s| if (s.pane.index() == slot) { spawned = true; }, else => {}, @@ -2784,7 +2785,7 @@ test "unknown Exec from a document runs as a command pane in its directory, type var spawned = false; while (p.nextEffect()) |effect| switch (effect) { .write => return error.TypedIntoATerminal, - .spawn => |sp| if (sp.pane == dst) { + .spawn => |sp| if (sp.pane.index() == dst) { try std.testing.expectEqualStrings("/tmp/pardes-image-dir", sp.cwd.slice()); spawned = true; }, @@ -2815,7 +2816,7 @@ test "Exec in a terminal whose tty is taken runs as a command pane instead of ty var spawned = false; while (p.nextEffect()) |effect| switch (effect) { .write => return error.WroteACommandLineIntoATerminal, - .spawn => |sp| if (sp.pane == dst) { + .spawn => |sp| if (sp.pane.index() == dst) { // ...in the directory the command was about, the taken pane's own try std.testing.expectEqualStrings("/tmp/pardes-taken", sp.cwd.slice()); spawned = true; @@ -2843,7 +2844,7 @@ test "a command of several lines keeps its tag one row, and its echo does not st var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); _ = try p.render(frame.allocator()); - p.update(.{ .exited = .{ .pane = @intCast(dst), .status = 0 } }); + p.update(.{ .exited = .{ .pane = .at(dst), .status = 0 } }); _ = exec.execute(p, 1, two); while (p.nextEffect()) |_| {} _ = try p.render(frame.allocator()); @@ -2863,7 +2864,8 @@ test "a spawn is for the pane it was made for, not whatever took the slot since" var command: [32]u8 = undefined; var core: *Pardes = undefined; const vtable: Host.VTable = .{ .spawn = spawn }; - fn spawn(_: ?*anyopaque, pane: u8, _: []const u8) void { + fn spawn(_: ?*anyopaque, id: pardes.Pane.Id, _: []const u8) void { + const pane = id.index(); spawns += 1; const line = core.panes[pane].?.command.?; @memcpy(command[0..line.len], line); @@ -2901,8 +2903,8 @@ test "a REPL bound for a language takes the exec made on a body in it; tags, Exe spawned = false; while (pp.nextEffect()) |effect| switch (effect) { .write => |w| { - @memcpy(to[w.pane][len[w.pane]..][0..w.bytes.slice().len], w.bytes.slice()); - len[w.pane] += w.bytes.slice().len; + @memcpy(to[w.pane.index()][len[w.pane.index()]..][0..w.bytes.slice().len], w.bytes.slice()); + len[w.pane.index()] += w.bytes.slice().len; }, .spawn => spawned = true, else => {}, @@ -2960,12 +2962,12 @@ test "a REPL bound for a language takes the exec made on a body in it; tags, Exe try std.testing.expectEqualStrings("a = 1\rb = [201~2\r", Sent.said(1)); // Into a REPL that takes a bracketed paste (3.13's), the Enter waits // for the REPL to answer the paste: read with it, it would be part of it. - p.update(.{ .output = .{ .pane = 1, .bytes = "\x1b[?2004h" } }); + p.update(.{ .output = .{ .pane = .at(1), .bytes = "\x1b[?2004h" } }); Sent.drain(p); _ = exec.executeFrom(p, 0, "print(1)", true); Sent.drain(p); try std.testing.expectEqualStrings("\x1b[200~print(1)\x1b[201~", Sent.said(1)); - p.update(.{ .output = .{ .pane = 1, .bytes = "print(1)" } }); + p.update(.{ .output = .{ .pane = .at(1), .bytes = "print(1)" } }); Sent.drain(p); // Python's echo only restarts a short wait: the Enter goes once the // echo of the whole paste has gone quiet. @@ -2991,7 +2993,7 @@ test "a REPL bound for a language takes the exec made on a body in it; tags, Exe p.update(.tick); Sent.drain(p); try std.testing.expectEqualStrings("\r", Sent.said(1)); - p.update(.{ .output = .{ .pane = 1, .bytes = "\x1b[?2004l" } }); + p.update(.{ .output = .{ .pane = .at(1), .bytes = "\x1b[?2004l" } }); Sent.len[1] = 0; // A block left open by its indented last line gets the blank line that // ends it: a second Enter. @@ -3095,7 +3097,7 @@ test "a REPL bound for a language takes the exec made on a body in it; tags, Exe _ = try pardes.panes.newCommand(p, cmd, "", "python3"); try std.testing.expect(p.executeBuiltinLine(cmd, "Repl python")); try std.testing.expect(p.panes[cmd].?.repl != null); - p.update(.{ .exited = .{ .pane = @intCast(cmd), .status = 0 } }); + p.update(.{ .exited = .{ .pane = .at(cmd), .status = 0 } }); try std.testing.expect(p.panes[cmd].?.repl == null); try std.testing.expect(p.executeBuiltinLine(cmd, "Repl python")); try std.testing.expect(p.panes[cmd].?.repl == null); @@ -3203,32 +3205,32 @@ test "a command pane shows how its command ended, and the next command there run const other = exec.execute(p, 1, "ls") orelse return error.NoCommandPane; try std.testing.expect(other != dst); while (p.nextEffect()) |_| {} - p.update(.{ .output = .{ .pane = @intCast(dst), .bytes = "compiled\r\n" } }); + p.update(.{ .output = .{ .pane = .at(dst), .bytes = "compiled\r\n" } }); // Its exit comes first, a job it left still printing: done, and not // the next command's until its pty closes. - p.update(.{ .exited = .{ .pane = @intCast(dst), .status = 2 } }); + p.update(.{ .exited = .{ .pane = .at(dst), .status = 2 } }); try std.testing.expect(pane.command_done and pane.command_pty); const busy = exec.execute(p, 1, "true") orelse return error.NoCommandPane; try std.testing.expect(busy != dst); while (p.nextEffect()) |_| {} // Its output ending: now it may be reused. - p.update(.{ .eof = .{ .pane = @intCast(dst) } }); + p.update(.{ .eof = .{ .pane = .at(dst) } }); try std.testing.expect(p.panes[dst] == pane and !pane.command_pty); try std.testing.expect(p.panes[dst] == pane); try std.testing.expect(pane.command_done); try std.testing.expect(std.mem.endsWith(u8, try tagline.tagPrefix(p, pane), "(make -j8) exit 2")); // Kill leaves a finished one be. try std.testing.expect(p.executeBuiltinLine(0, "Kill make")); - while (p.nextEffect()) |effect| if (effect == .signal_pty and effect.signal_pty.pane == dst) return error.SignalledAFinishedCommand; + while (p.nextEffect()) |effect| if (effect == .signal_pty and effect.signal_pty.pane.index() == dst) return error.SignalledAFinishedCommand; // A program that crashed out of its full screen leaves the emulator // in its modes; the next command starts clear of them. - p.update(.{ .output = .{ .pane = @intCast(dst), .bytes = "\x1b[?1049h\x1b[?2004h\x1b[?1000h" } }); + p.update(.{ .output = .{ .pane = .at(dst), .bytes = "\x1b[?1049h\x1b[?2004h\x1b[?1000h" } }); try std.testing.expect(pardes.Term.modes(pane.terminal).bracketed_paste); // Done, the directory's next command runs there, below what it showed. try std.testing.expectEqual(@as(?usize, dst), exec.execute(p, 1, "make test")); try std.testing.expect(!pardes.Term.modes(pane.terminal).bracketed_paste); var respawned = false; - while (p.nextEffect()) |effect| if (effect == .spawn and effect.spawn.pane == dst) { + while (p.nextEffect()) |effect| if (effect == .spawn and effect.spawn.pane.index() == dst) { respawned = true; }; try std.testing.expect(respawned); @@ -3242,7 +3244,7 @@ test "a command pane shows how its command ended, and the next command there run // Kill stops a running one: its whole process group, SIGTERM. try std.testing.expect(p.executeBuiltinLine(0, "Kill make")); var signalled = false; - while (p.nextEffect()) |effect| if (effect == .signal_pty and effect.signal_pty.pane == dst) { + while (p.nextEffect()) |effect| if (effect == .signal_pty and effect.signal_pty.pane.index() == dst) { try std.testing.expectEqual(PtySignal.term, effect.signal_pty.sig); signalled = true; }; @@ -3260,7 +3262,7 @@ test "a command pane shows how its command ended, and the next command there run const Count = struct { var signals: usize = 0; const vtable: Host.VTable = .{ .pty_signal = signal }; - fn signal(_: ?*anyopaque, _: u8, _: PtySignal) void { + fn signal(_: ?*anyopaque, _: pardes.Pane.Id, _: PtySignal) void { signals += 1; } }; @@ -3300,7 +3302,7 @@ test "a terminal its program gave back takes command lines again" { var sent_len: usize = 0; while (p.nextEffect()) |effect| switch (effect) { .write => |w| { - try std.testing.expectEqual(@as(u8, 0), w.pane); + try std.testing.expectEqual(0, w.pane.index()); try std.testing.expect(w.bytes.slice().len <= sent.len - sent_len); @memcpy(sent[sent_len..][0..w.bytes.slice().len], w.bytes.slice()); sent_len += w.bytes.slice().len; @@ -3360,7 +3362,7 @@ test "a host with no methods at all is a complete in-process pardes" { // the absent child is SILENT: the bytes are dropped, and nothing appears // on the pane's screen to suggest a program answered const before = pardes.Term.grid(p.panes[0].?.terminal).cursor_y; - p.perform(.{ .write = .{ .pane = 0, .bytes = .from("ls\r") } }); + p.perform(.{ .write = .{ .pane = .at(0), .bytes = .from("ls\r") } }); try std.testing.expectEqual(before, pardes.Term.grid(p.panes[0].?.terminal).cursor_y); // the clipboard round-trips through the in-process one @@ -3374,11 +3376,11 @@ test "a host with no methods at all is a complete in-process pardes" { for (p.panes, 0..) |slot, i| if (slot == doc) { doc_id = @intCast(i); }; - p.perform(.{ .save_file = .{ .pane = doc_id } }); + p.perform(.{ .save_file = .{ .pane = .at(doc_id) } }); try std.testing.expectEqualStrings("body\n", p.fallback.files.get(doc.file.?.path).?); // a language backend nobody supplied answers nothing rather than hanging - p.perform(.{ .lsp = .{ .id = 7, .kind = .hover, .pane = doc_id, .offset = 0, .arg = .{} } }); + p.perform(.{ .lsp = .{ .id = 7, .kind = .hover, .pane = .at(doc_id), .offset = 0, .arg = .{} } }); p.perform(.quit); try std.testing.expect(p.quit); @@ -3775,7 +3777,7 @@ test "raw terminal cursor obeys visibility without hiding modal and tag cursors" while (p.nextEffect()) |_| {} const pane = p.panes[0].?; pane.body.mode = .tty; - p.update(.{ .output = .{ .pane = 0, .bytes = "abc" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "abc" } }); var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const visible = (try p.render(frame.allocator())).cursor orelse return error.MissingCursor; @@ -3784,7 +3786,7 @@ test "raw terminal cursor obeys visibility without hiding modal and tag cursors" try std.testing.expectEqual(rect.x + config.GUTTER + 3, visible.x); try std.testing.expectEqual(body_y, visible.y); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?25l" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?25l" } }); try std.testing.expect((try p.render(frame.allocator())).cursor == null); pane.body.mode = .normal; pane.body.cur_pinned = true; @@ -3800,13 +3802,13 @@ test "raw terminal cursor obeys visibility without hiding modal and tag cursors" pane.body.mode = .tty; try std.testing.expect((try p.render(frame.allocator())).cursor == null); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?25h\x1b[?1049h\x1b[2;5H" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?25h\x1b[?1049h\x1b[2;5H" } }); const alternate = (try p.render(frame.allocator())).cursor orelse return error.MissingCursor; try std.testing.expectEqual(rect.x + config.GUTTER + 4, alternate.x); try std.testing.expectEqual(body_y + 1, alternate.y); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?25l" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?25l" } }); try std.testing.expect((try p.render(frame.allocator())).cursor == null); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1049l\x1b[?25h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1049l\x1b[?25h" } }); const returned = (try p.render(frame.allocator())).cursor orelse return error.MissingCursor; try std.testing.expectEqual(visible.x, returned.x); try std.testing.expectEqual(visible.y, returned.y); @@ -3820,8 +3822,8 @@ test "raw terminal cursor follows its active row only while that row is visible" while (p.nextEffect()) |_| {} const pane = p.panes[0].?; pane.body.mode = .tty; - for (0..60) |_| p.update(.{ .output = .{ .pane = 0, .bytes = "row\r\n" } }); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3;4H" } }); + for (0..60) |_| p.update(.{ .output = .{ .pane = .at(0), .bytes = "row\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[3;4H" } }); var frame = std.heap.ArenaAllocator.init(gpa); defer frame.deinit(); const live = (try p.render(frame.allocator())).cursor orelse return error.MissingCursor; @@ -3881,7 +3883,7 @@ test "tty output follows only from the bottom" { for (0..60) |i| { var buf: [32]u8 = undefined; - p.update(.{ .output = .{ .pane = @intCast(shell), .bytes = std.fmt.bufPrint(&buf, "line {d}\r\n", .{i}) catch unreachable } }); + p.update(.{ .output = .{ .pane = .at(shell), .bytes = std.fmt.bufPrint(&buf, "line {d}\r\n", .{i}) catch unreachable } }); } p.sync(); const bottom = pardes.Term.grid(sp.terminal).offset; @@ -3894,7 +3896,7 @@ test "tty output follows only from the bottom" { try std.testing.expect(parked < bottom); for (60..70) |i| { var buf: [32]u8 = undefined; - p.update(.{ .output = .{ .pane = @intCast(shell), .bytes = std.fmt.bufPrint(&buf, "line {d}\r\n", .{i}) catch unreachable } }); + p.update(.{ .output = .{ .pane = .at(shell), .bytes = std.fmt.bufPrint(&buf, "line {d}\r\n", .{i}) catch unreachable } }); } p.sync(); try std.testing.expectEqual(parked, pardes.Term.grid(sp.terminal).offset); @@ -3906,7 +3908,7 @@ test "tty output follows only from the bottom" { // ...and from there output follows again. const live = pardes.Term.grid(sp.terminal).offset; - p.update(.{ .output = .{ .pane = @intCast(shell), .bytes = "tail\r\n" } }); + p.update(.{ .output = .{ .pane = .at(shell), .bytes = "tail\r\n" } }); p.sync(); try std.testing.expect(pardes.Term.grid(sp.terminal).offset > live); } @@ -3976,7 +3978,7 @@ test "a bracketed paste larger than the ring still closes its bracket" { _ = test_api.ptyBytes(p, null, buf); pardes.keys.enterTty(p, 0); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?2004h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?2004h" } }); try std.testing.expect(pardes.Term.modes(p.panes[0].?.terminal).bracketed_paste); const text = try gpa.alloc(u8, 1536 * 1024); defer gpa.free(text); @@ -4007,7 +4009,7 @@ test "a terminal's answer to its program waits behind the paste still queued for // host takes one write off, and the program asks where its cursor is. const taken = p.nextEffect().?; const off = if (taken == .write) taken.write.bytes.len else 0; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[6n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[6n" } }); const got = test_api.ptyBytes(p, null, buf); // The whole paste first, then the answer, `CSI row ; col R`, whole. try std.testing.expectEqualSlices(u8, text[off..], got[0 .. text.len - off]); @@ -4047,7 +4049,7 @@ test "leaving tty hides the prompt and keeps the command typed at it" { // Exactly what an OSC 133 shell draws: prompt, then the marker that says // the rest of this row is the user's, then what they typed. One grid row. p.update(.{ .output = .{ - .pane = @intCast(shell), + .pane = .at(shell), .bytes = "\x1b]133;A\x1b\\user@box ~/src $ \x1b]133;B\x1b\\grep -rn TODO src/", } }); p.sync(); @@ -4091,7 +4093,7 @@ test "leaving tty hides the prompt and keeps the command typed at it" { // ...and a prompt with nothing typed at it yet is all chrome, so the whole // row goes, which is what it has always done. p.update(.{ .output = .{ - .pane = @intCast(shell), + .pane = .at(shell), .bytes = "\r\n\x1b]133;A\x1b\\user@box ~/src $ \x1b]133;B\x1b\\", } }); p.sync(); @@ -4109,7 +4111,7 @@ test "entering tty walks the shell cursor to the column clicked past the prompt" const shell = p.view().active; const sp = p.panes[shell].?; p.update(.{ .output = .{ - .pane = @intCast(shell), + .pane = .at(shell), .bytes = "\x1b]133;A;cl=line\x1b\\prompt> \x1b]133;B\x1b\\0123456789", } }); p.sync(); diff --git a/core/Effect.zig b/core/Effect.zig index f0e18335..7166294f 100644 --- a/core/Effect.zig +++ b/core/Effect.zig @@ -37,29 +37,29 @@ pub const Effect = union(enum) { /// A child for the pane in slot `pane` -- the one whose serial is /// `serial`: a pane closed and its slot taken before the effect is /// performed is not the one it was for (perform). - spawn: struct { pane: u8, serial: u32, cwd: Buf(effect_path_cap) }, - write: struct { pane: u8, bytes: Buf(effect_write_cap) }, - resize_pty: struct { pane: u8, cols: u16, rows: u16 }, + spawn: struct { pane: Pane.Id, serial: u32, cwd: Buf(effect_path_cap) }, + write: struct { pane: Pane.Id, bytes: Buf(effect_write_cap) }, + resize_pty: struct { pane: Pane.Id, cols: u16, rows: u16 }, /// For the pane whose serial is `serial`, as `spawn` is (perform). - signal_pty: struct { pane: u8, serial: u32, sig: PtySignal }, + signal_pty: struct { pane: Pane.Id, serial: u32, sig: PtySignal }, /// The terminal pane in this slot is gone: the shell hangs up its pty /// and ends the shell, rather than leave it running with nobody to read. - close_pty: struct { pane: u8 }, + close_pty: struct { pane: Pane.Id }, open_link: Buf(256), /// A program a plumbing rule starts (Plumb.run): its directory, then /// its argv, each ended by a NUL. A failure to start it is said at /// pane `pane`. - start: struct { pane: u8, line: Buf(effect_path_cap) }, + start: struct { pane: Pane.Id, line: Buf(effect_path_cap) }, /// write this pane's file content to its path; the shell reads both off /// the core (content is unbounded, effects are fixed-size values) - save_file: struct { pane: u8 }, - save_text: struct { pane: u8, serial: u32, path: Buf(effect_path_cap), promote: bool = false }, + save_file: struct { pane: Pane.Id }, + save_text: struct { pane: Pane.Id, serial: u32, path: Buf(effect_path_cap), promote: bool = false }, write_dump, set_clipboard, read_clipboard, /// `arg` up to 128 bytes, or for `explain` the query it explains, /// `<kind> <arg>`. - lsp: struct { id: u32, kind: lsp.Kind, pane: u8, offset: u32, arg: Buf(160) }, + lsp: struct { id: u32, kind: lsp.Kind, pane: Pane.Id, offset: u32, arg: Buf(160) }, /// Snapshot the matching request with pipeRequest(id), then run it away /// from the UI/event loop and answer with pipe_resp. pipe: struct { id: u32 }, @@ -71,19 +71,19 @@ pub const Effect = union(enum) { hand_over: struct { seq: u32 }, /// Write what an Edit's `w` took (`Pipe.writes`), in order. edit_write, - watch: struct { pane: u8, on: bool, mode: WatchMode = .reconcile }, + watch: struct { pane: Pane.Id, on: bool, mode: WatchMode = .reconcile }, theme_file: struct { generation: u32, on: bool }, /// Write the build-time theme ring below the per-user config directory. /// The pane receives the completion/error message from the native host. - dump_themes: struct { pane: u8 }, - detach: struct { pane: u8 }, + dump_themes: struct { pane: Pane.Id }, + detach: struct { pane: Pane.Id }, /// An addon loaded, let go, or let go and loaded again (Addon.zig): /// `arg` its path, or for unload its kind's name. Answered by an /// `addon` event. - addon: struct { op: @import("Addon.zig").Op, pane: u8, arg: Buf(effect_path_cap) }, + addon: struct { op: @import("Addon.zig").Op, pane: Pane.Id, arg: Buf(effect_path_cap) }, /// A kind's job for the host's workers (host_io.Job), answered by a /// `job` event. - job: struct { pane: u8, serial: u32, token: u32 }, + job: struct { pane: Pane.Id, serial: u32, token: u32 }, quit, pub fn Buf(comptime n: usize) type { @@ -120,8 +120,8 @@ fn performInner(p: *Pardes, e: Effect) void { switch (e) { // The host reads the pane (its command, its shell) as it forks, // so the slot must still hold the pane the spawn was for. - .spawn => |s| if (if (p.panes[s.pane]) |pane| pane.serial != s.serial else true) {} else if (v.spawn) |f| f(p.host.ctx, s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane] orelse "") else s.cwd.slice()) else { - p.fallback.spawned[s.pane] = true; + .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else if (v.spawn) |f| f(p.host.ctx, s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice()) else { + p.fallback.spawned[s.pane.index()] = true; }, // A pane with no child is silent: nothing invents output on its // screen, and the bytes are dropped rather than transcribed. @@ -129,7 +129,7 @@ fn performInner(p: *Pardes, e: Effect) void { .resize_pty => |r| if (v.pty_resize) |f| f(p.host.ctx, r.pane, r.cols, r.rows), // A signal for a pane that closed, its slot retaken in the same // step, would reach another pane's child. - .signal_pty => |s| if (if (p.panes[s.pane]) |pane| pane.serial != s.serial else true) {} else if (v.pty_signal) |f| f(p.host.ctx, s.pane, s.sig), + .signal_pty => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else if (v.pty_signal) |f| f(p.host.ctx, s.pane, s.sig), .close_pty => |c| if (v.close_pty) |f| f(p.host.ctx, c.pane), // The view's frontend opens it, yanks and pastes, on its own // machine (View.yanks); the host here only for a view no client @@ -149,17 +149,17 @@ fn performInner(p: *Pardes, e: Effect) void { _ = words.next(); // its directory const prog = words.next() orelse ""; var said: pardes.Kind.Buf(160) = .{}; - p.reportFailure(st.pane, said.print("plumb start: {s}: {s}", .{ prog[0..@min(64, prog.len)], w })); + p.reportFailure(st.pane.index(), said.print("plumb start: {s}: {s}", .{ prog[0..@min(64, prog.len)], w })); }, .save_file => |sf| { - const pane = p.panes[sf.pane] orelse return; + const pane = p.panes[sf.pane.index()] orelse return; const f = if (pane.file) |*file| file else return; const was = f.saved_revision; f.saved_revision = f.revision; // A save the host could not do (saveFailed) is its err // record alone, not a `save`, and changes nothing: a clean // file stays clean. - const saved_pane = writeChecked(p, sf.pane, pane.serial, f.path, f.content, was) orelse return; + const saved_pane = writeChecked(p, sf.pane.index(), pane.serial, f.path, f.content, was) orelse return; const saved = if (saved_pane.file) |*file| file else return; // Edited while it was written: what is on disk is not this. if (saved.saved_revision != saved.revision) return; @@ -174,19 +174,19 @@ fn performInner(p: *Pardes, e: Effect) void { p.emit(.{ .watch = .{ .pane = sf.pane, .on = true } }); }, .save_text => |st| { - const pane = p.panes[st.pane] orelse return; + const pane = p.panes[st.pane.index()] orelse return; if (pane.serial != st.serial) return; // a recycled slot: not ours if (pane.file) |*f| { // Failed, it changes nothing: not the name, not dirty. - const after = writeChecked(p, st.pane, st.serial, st.path.slice(), f.content, f.saved_revision) orelse return; + const after = writeChecked(p, st.pane.index(), st.serial, st.path.slice(), f.content, f.saved_revision) orelse return; if (after.file == null) return; - if (st.promote) promoteSaved(p, st.pane, st.path.slice()) else ctlfs.events.noteLogAs(p, .save, after, st.path.slice()); + if (st.promote) promoteSaved(p, st.pane.index(), st.path.slice()) else ctlfs.events.noteLogAs(p, .save, after, st.path.slice()); return; } if (!pane.isTerminal()) return; - const text = pardes.Term.scrollback(pane.terminal, p.gpa) catch |err| return p.reportError(.at(st.pane), "Save", err); + const text = pardes.Term.scrollback(pane.terminal, p.gpa) catch |err| return p.reportError(st.pane, "Save", err); defer p.gpa.free(text); - if (writeChecked(p, st.pane, st.serial, st.path.slice(), text, 0)) |still| + if (writeChecked(p, st.pane.index(), st.serial, st.path.slice(), text, 0)) |still| ctlfs.events.noteLogAs(p, .save, still, st.path.slice()); }, .write_dump => { @@ -249,22 +249,22 @@ fn performInner(p: *Pardes, e: Effect) void { }, .watch => |w| if (v.watch_file) |f| { // What a watch is about: a real file, or a kind's (a PDF's). - const path: []const u8 = if (p.panes[w.pane]) |pane| (if (pane.file) |file| (if (file.output == null) file.path else "") else if (pane.addon) |k| k.path else "") else ""; + const path: []const u8 = if (p.panes[w.pane.index()]) |pane| (if (pane.file) |file| (if (file.output == null) file.path else "") else if (pane.addon) |k| k.path else "") else ""; f(p.host.ctx, w.pane, path, w.on, w.mode); } else { - p.fallback.watched[w.pane] = w.on; + p.fallback.watched[w.pane.index()] = w.on; }, .theme_file => |t| if (v.watch_theme) |f| f(p.host.ctx, t.generation, t.on), .dump_themes => |d| if (v.dump_themes) |f| f(p.host.ctx, d.pane), .detach => |d| if (v.detach) |f| f(p.host.ctx) else - p.reportError(.at(d.pane), "detach", error.NotAttached), + p.reportError(d.pane, "detach", error.NotAttached), // dlopen is the same on every host, so none has a method of its // own; a replica's core holds no library (what it brought comes in // the event this taped). - .addon => |a| if (!tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane, a.arg.slice()), - .job => |j| if (v.job) |f| f(p.host.ctx, j.pane, j.serial, j.token) else if (!tape.replaying()) host_io.KindJob.runHere(p, j.pane, j.serial, j.token), + .addon => |a| if (!tape.replaying()) @import("Addon.zig").perform(p, a.op, a.pane.index(), a.arg.slice()), + .job => |j| if (v.job) |f| f(p.host.ctx, j.pane, j.serial, j.token) else if (!tape.replaying()) host_io.KindJob.runHere(p, j.pane.index(), j.serial, j.token), // the loop's own condition; a host tears down after its own loop .quit => p.endSession(), } @@ -282,7 +282,7 @@ pub fn writeChecked(p: *Pardes, id: u8, serial: u32, path: []const u8, bytes: [] if (filesystem.localPath(path)) |local| filesystem.makeDirs(p.tape, std.Io.Dir.path.dirname(local) orelse "/"); // The in-process filesystem reports the same way a real host does, // so an OOM there leaves the pane dirty rather than looking saved. - if (p.host.vtable.write_file) |f| f(p.host.ctx, id, path, bytes) else if (!p.fallback.writeFile(path, bytes)) p.saveFailed(.at(id), path, error.OutOfMemory); + if (p.host.vtable.write_file) |f| f(p.host.ctx, .at(id), path, bytes) else if (!p.fallback.writeFile(path, bytes)) p.saveFailed(.at(id), path, error.OutOfMemory); const pane = p.panes[id] orelse return null; if (pane.serial != serial) return null; if (p.fs.failures == failures) return pane; @@ -312,7 +312,7 @@ fn promoteSaved(p: *Pardes, id: u8, path: []const u8) void { ctlfs.events.noteLog(p, .save, pane); p.recent.opened(p.recentGpa(), f.path); if (filesystem.localPath(f.path) != null) - p.emit(.{ .watch = .{ .pane = id, .on = true } }); + p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); } fn drainForSavePath(p: *Pardes, buf: []u8) ?[]const u8 { @@ -343,7 +343,7 @@ test "Detach asks the host to let its terminal go, and says so when there is not .detach => |d| break d, else => {}, } else return error.NoDetachAsked; - try std.testing.expectEqual(@as(u8, 0), asked.pane); + try std.testing.expectEqual(0, asked.pane.index()); p.perform(.{ .detach = asked }); const pane = p.panes[0].?; @@ -376,7 +376,7 @@ test "Save on a scratch asks for a path in its inherited dir and makes it a file // The write asked for, the pane is the file once it is done. var saved = false; while (p.nextEffect()) |effect| { - if (effect == .save_text and @as(usize, effect.save_text.pane) == id) saved = true; + if (effect == .save_text and effect.save_text.pane.index() == id) saved = true; p.perform(effect); } try std.testing.expect(saved); @@ -392,7 +392,7 @@ test "Save on a terminal writes its plaintext scrollback and stays a terminal" { while (p.nextEffect()) |_| {} const pane = p.panes[0].?; p.setCwd(.at(0), "/tmp/pardes-tty-save"); - p.update(.{ .output = .{ .pane = 0, .bytes = "hello scrollback\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "hello scrollback\r\n" } }); while (p.nextEffect()) |_| {} try std.testing.expect(p.executeBuiltinLine(0, "Save")); @@ -474,7 +474,7 @@ test "Save takes the path as an argument, relative to the pane's own directory" while (p.nextEffect()) |_| {} const pane = p.panes[0].?; p.setCwd(.at(0), "/tmp/pardes-arg-save"); - p.update(.{ .output = .{ .pane = 0, .bytes = "typed and gone\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "typed and gone\r\n" } }); while (p.nextEffect()) |_| {} var buf: [256]u8 = undefined; @@ -510,9 +510,9 @@ test "Save takes the path as an argument, relative to the pane's own directory" test "a save the host could not do leaves the pane dirty" { const gpa = std.testing.allocator; const Refusing = struct { - fn writeFile(ctx: ?*anyopaque, pane: u8, _: []const u8, _: []const u8) void { + fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, _: []const u8) void { const core: *Pardes = @ptrCast(@alignCast(ctx.?)); - core.saveFailed(.at(pane), "/x/y.txt", error.PermissionDenied); + core.saveFailed(id, "/x/y.txt", error.PermissionDenied); } }; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); @@ -577,7 +577,7 @@ test "saves armed in one batch stay with their own panes" { const tty_id = p.freeSlot() orelse return error.NoPaneSlots; const shell = try p.newShell(tty_id, "/tmp/pardes-batch"); p.setCwd(.at(tty_id), "/tmp/pardes-batch"); - p.update(.{ .output = .{ .pane = @intCast(tty_id), .bytes = "shell text\r\n" } }); + p.update(.{ .output = .{ .pane = .at(tty_id), .bytes = "shell text\r\n" } }); const file = try p.setTestFile("file text\n"); const file_id = panes.paneIdOf(p, file) orelse return error.MissingFilePane; try std.testing.expect(shell.isTerminal()); @@ -606,7 +606,7 @@ test "a save whose pane is gone before the drain writes nothing" { const doomed = p.freeSlot() orelse return error.NoPaneSlots; const shell = try p.newShell(doomed, "/tmp/pardes-gone"); - p.update(.{ .output = .{ .pane = @intCast(doomed), .bytes = "doomed text\r\n" } }); + p.update(.{ .output = .{ .pane = .at(doomed), .bytes = "doomed text\r\n" } }); while (p.nextEffect()) |_| {} try std.testing.expect(shell.isTerminal()); @@ -687,19 +687,19 @@ test "a Save the host could not do logs no save record" { const Failing = struct { core: *Pardes, const vtable: Host.VTable = .{ .write_file = write }; - fn write(ctx: ?*anyopaque, pane: u8, path: []const u8, _: []const u8) void { + fn write(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, _: []const u8) void { const f: *@This() = @ptrCast(@alignCast(ctx.?)); - f.core.saveFailed(.at(pane), path, error.AccessDenied); + f.core.saveFailed(id, path, error.AccessDenied); } }; var host: Failing = .{ .core = p }; p.host = .{ .ctx = &host, .vtable = &Failing.vtable }; _ = th.wr(p, ctlfs.Node.of(p.panes[0].?.serial, .body), "more\n"); - p.perform(.{ .save_file = .{ .pane = 0 } }); + p.perform(.{ .save_file = .{ .pane = .at(0) } }); try std.testing.expect(!th.logHas(p, "\nsave ")); try std.testing.expect(p.panes[0].?.file.?.saved_revision != p.panes[0].?.file.?.revision); // With the host's write done, it is logged. p.host = .{}; - p.perform(.{ .save_file = .{ .pane = 0 } }); + p.perform(.{ .save_file = .{ .pane = .at(0) } }); try std.testing.expect(th.logHas(p, "\nsave ")); } diff --git a/core/Event.zig b/core/Event.zig index faea73cc..9988eede 100644 --- a/core/Event.zig +++ b/core/Event.zig @@ -3,6 +3,7 @@ const config = @import("config.zig"); const selection_pipe = @import("selection_pipe.zig"); const Stream = @import("Stream.zig"); +const Pane = @import("panes.zig").Pane; const Layer = @import("Layer.zig"); const RowMetrics = Layer.Metrics; const TagHit = Layer.TagHit; @@ -78,14 +79,14 @@ pub const Event = union(enum) { cell_pixels: CellPixels = .{}, row_metrics: ?RowMetrics = null, }, - output: struct { pane: u8, bytes: []const u8 }, + output: struct { pane: Pane.Id, bytes: []const u8 }, /// The pane's pty reached end of file: its output is over. An /// interactive terminal closes; a command pane waits for `exited`. - eof: struct { pane: u8, status: ?u8 = null }, + eof: struct { pane: Pane.Id, status: ?u8 = null }, /// A command pane's child exited, with this status (null: unknown). It /// is the command's end, whatever still holds its pty (a job it left in /// the background prints on until end of file). - exited: struct { pane: u8, status: ?u8 }, + exited: struct { pane: Pane.Id, status: ?u8 }, lsp_resp: struct { id: u32, rows: ?[]const u8 }, pipe_resp: struct { id: u32, @@ -93,7 +94,7 @@ pub const Event = union(enum) { outputs: []const []const u8, failure: ?selection_pipe.Failure = null, }, - file_changed: struct { pane: u8, bytes: []const u8 }, + file_changed: struct { pane: Pane.Id, bytes: []const u8 }, /// A disk search's batch (Stream.zig), borrowed for the step. search: Stream.Batch, paste: []const u8, @@ -112,10 +113,10 @@ pub const Event = union(enum) { frontend: Frontend, /// What the host's `addon` effect read off a library, or why not /// (Addon.landed). Borrowed for the step. - addon: struct { op: @import("Addon.zig").Op, pane: u8, kind: *const @import("Kind.zig").Loaded }, + addon: struct { op: @import("Addon.zig").Op, pane: Pane.Id, kind: *const @import("Kind.zig").Loaded }, /// A kind's job answered (host_io.Job), at its kind's generation then. /// Borrowed for the step. - job: struct { pane: u8, serial: u32, token: u32, gen: u32, bytes: []const u8 }, + job: struct { pane: Pane.Id, serial: u32, token: u32, gen: u32, bytes: []const u8 }, tick, }; diff --git a/core/File.zig b/core/File.zig index 8337b833..c336b1b8 100644 --- a/core/File.zig +++ b/core/File.zig @@ -581,13 +581,13 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane { pane.body.cur_pinned = true; pane.body.cur_row = @intCast(scroll); p.installPane(id, pane); - if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); return pane; } pub fn deinit(p: *Pardes, pane: *Pane, file: *State) void { if (file.output == null) for (p.panes, 0..) |slot, id| { - if (slot == pane) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); + if (slot == pane) p.emit(.{ .watch = .{ .pane = .at(id), .on = false } }); }; // A twin's text, undo and shared index stay with the twins left. var twins: [pardes.MAX_PANES]usize = undefined; @@ -991,7 +991,7 @@ pub fn zerox(p: *Pardes, from: usize, to: usize) !*Pane { pane.body.cur_row = src.body.cur_row; pane.body.cur_col = src.body.cur_col; pane.body.cur_pinned = true; - if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(to), .on = true } }); + if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = .at(to), .on = true } }); return pane; } @@ -1296,7 +1296,7 @@ fn restoreName(p: *Pardes, pane: *Pane, f: *State, snap: Text.Place) void { f.saved_revision = if (snap.clean) f.revision else f.revision -% 1; f.disk_newer = null; f.disk_newer_warned = false; - if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + if (filesystem.localPath(path) != null) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); } pub fn restoreSnap(tab: u16, pane: *Pane, f: *State, snap: Text.Place) void { diff --git a/core/Jump.zig b/core/Jump.zig index 26b3c4d9..66dbd2b6 100644 --- a/core/Jump.zig +++ b/core/Jump.zig @@ -404,11 +404,11 @@ test "jump history terminal rectangle follows output and clears on reflow" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12, .tty_only = true }); defer p.deinit(); const pane = p.panes[0].?; - p.update(.{ .output = .{ .pane = 0, .bytes = "alpha bravo\r\nsecond line\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "alpha bravo\r\nsecond line\r\n" } }); pane.sel[0] = .{ .state = .done, .c0 = 0, .c1 = 4, .r0 = BOX_H, .r1 = BOX_H }; try edit.capturePointerSelection(p, pane, 0); try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); - for (0..40) |_| p.update(.{ .output = .{ .pane = 0, .bytes = "more output\r\n" } }); + for (0..40) |_| p.update(.{ .output = .{ .pane = .at(0), .bytes = "more output\r\n" } }); try std.testing.expectEqualStrings("alpha", try edit.selectionText(p, pane, pane.sel[0])); pane.resizeGrid(pane.cols - 1, pane.rows); try std.testing.expect(pane.pointerSelection(pane.sel[0]) == null); diff --git a/core/Kind.zig b/core/Kind.zig index 05a813f3..7641c2d1 100644 --- a/core/Kind.zig +++ b/core/Kind.zig @@ -278,7 +278,7 @@ pub fn open(p: *Pardes, id: usize, k: u8, path: ?[]const u8, bytes: ?[]const u8) _ = reopen(p, id, s, bytes); // Its file watched, as a text's is: changed on disk, it opens again // from it (fileChanged); retirePane lets the watch go. - if (s.path.len > 0 and @import("fs.zig").localPath(s.path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + if (s.path.len > 0 and @import("fs.zig").localPath(s.path) != null) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); return pane; } @@ -995,7 +995,7 @@ const Call = struct { for (o.jobs) |token| if (token != 0) { cl.slot.tokens = token; }; - if (cl.p) |p| if (p.panes[cl.id]) |pane| for (o.jobs) |token| if (token != 0) p.emit(.{ .job = .{ .pane = @intCast(cl.id), .serial = pane.serial, .token = token } }); + if (cl.p) |p| if (p.panes[cl.id]) |pane| for (o.jobs) |token| if (token != 0) p.emit(.{ .job = .{ .pane = .at(cl.id), .serial = pane.serial, .token = token } }); for (o.cancels) |token| if (token != 0) { for (&cl.slot.jobs) |*j| if (j.*) |job| if (job.token == token) { cl.slot.gpa.free(job.in); diff --git a/core/Plumb.zig b/core/Plumb.zig index 406e7dd8..aeebfc64 100644 --- a/core/Plumb.zig +++ b/core/Plumb.zig @@ -969,7 +969,7 @@ fn run(p: *Pardes, id: usize, wdir: []const u8, argv: []const []const u8) ?[]con start.data[start.len + w.len] = 0; start.len += @intCast(w.len + 1); }; - p.emit(.{ .start = .{ .pane = @intCast(id), .line = start } }); + p.emit(.{ .start = .{ .pane = .at(id), .line = start } }); return null; } diff --git a/core/Query.zig b/core/Query.zig index 784e6af4..dacc11a1 100644 --- a/core/Query.zig +++ b/core/Query.zig @@ -1756,7 +1756,7 @@ fn promote(p: *Pardes, qid: usize, id: usize) void { f.syntax_dirty = true; pane.attached_to = null; if (f.on_disk) p.recent.opened(p.recentGpa(), f.path); - if (filesystem.localPath(f.path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + if (filesystem.localPath(f.path) != null) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); pardes.ctlfs.events.noteLog(p, .rename, pane); p.panes[qid].?.query.?.preview = 0; } diff --git a/core/builtins.zig b/core/builtins.zig index ca13bd59..6f37e86d 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -447,7 +447,7 @@ pub const Kill = struct { // the marks say is running is the one signalled, not whatever // runs by then. if (command != null) { - c.p.emit(.{ .signal_pty = .{ .pane = @intCast(id), .serial = pane.serial, .sig = .term } }); + c.p.emit(.{ .signal_pty = .{ .pane = .at(id), .serial = pane.serial, .sig = .term } }); stopped = true; } else if (c.p.hostKillJob(id)) stopped = true else jobless = true; } @@ -537,7 +537,7 @@ pub const Detach = struct { pub const scope: Scope = .session; pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { - c.p.emit(.{ .detach = .{ .pane = @intCast(c.id) } }); + c.p.emit(.{ .detach = .{ .pane = .at(c.id) } }); } }; @@ -548,7 +548,7 @@ pub const Adopt = struct { pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { const adopt = c.p.host.vtable.adopt orelse return c.p.reportFailure(c.id, "Adopt: no pardes daemon is keeping shells for this session"); - adopt(c.p.host.ctx, @intCast(c.id)); + adopt(c.p.host.ctx, .at(c.id)); } }; @@ -560,7 +560,7 @@ pub const Upgrade = struct { pub const enabled = pardes.can_attach; pub fn run(c: Ctx) void { const upgrade = c.p.host.vtable.upgrade orelse return c.p.reportFailure(c.id, "Upgrade: this pardes cannot move to another build in place"); - upgrade(c.p.host.ctx, @intCast(c.id)); + upgrade(c.p.host.ctx, .at(c.id)); } }; @@ -682,7 +682,7 @@ pub const DumpThemes = struct { c.p.reportError(.at(c.id), "dump themes", error.NoConfigDirectory); return; } - c.p.emit(.{ .dump_themes = .{ .pane = @intCast(c.id) } }); + c.p.emit(.{ .dump_themes = .{ .pane = .at(c.id) } }); } }; @@ -2011,7 +2011,7 @@ test "Kill stops the commands pardes typed, bare all and by name those that matc const term = p.view().active; try std.testing.expect(p.panes[term].?.isTerminal()); const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = prompt } }); + p.update(.{ .output = .{ .pane = .at(term), .bytes = prompt } }); while (p.nextEffect()) |_| {} // The host the core asks: how many it was asked to stop, and whether // their shells run jobs in groups of their own. @@ -2019,7 +2019,7 @@ test "Kill stops the commands pardes typed, bare all and by name those that matc var asked: usize = 0; var job_control = true; const vtable: pardes.Host.VTable = .{ .kill_job = answer }; - fn answer(_: ?*anyopaque, _: u8) bool { + fn answer(_: ?*anyopaque, _: pardes.Pane.Id) bool { asked += 1; return job_control; } @@ -2037,7 +2037,7 @@ test "Kill stops the commands pardes typed, bare all and by name those that matc while (p.nextEffect()) |_| {} // Typed but not started yet (no C mark): not running. try std.testing.expectEqual(@as(usize, 0), killed.count(p, "Kill")); - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = "\x1b]133;C;aid=pardes\x07" } }); + p.update(.{ .output = .{ .pane = .at(term), .bytes = "\x1b]133;C;aid=pardes\x07" } }); while (p.nextEffect()) |_| {} try std.testing.expectEqual(@as(usize, 0), killed.count(p, "Kill ls")); try std.testing.expectEqual(@as(usize, 1), killed.count(p, "Kill ls make")); @@ -2053,7 +2053,7 @@ test "Kill stops the commands pardes typed, bare all and by name those that matc p.fs.failure_len = 0; killed.job_control = true; // Ended (a D mark): nothing to stop again. - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = "\x1b]133;D;143;aid=pardes\x07" ++ prompt } }); + p.update(.{ .output = .{ .pane = .at(term), .bytes = "\x1b]133;D;143;aid=pardes\x07" ++ prompt } }); while (p.nextEffect()) |_| {} try std.testing.expectEqual(@as(usize, 0), killed.count(p, "Kill")); } diff --git a/core/dump.zig b/core/dump.zig index 3b5ed421..1616f557 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -651,7 +651,7 @@ fn reviveSession(p: *Pardes, previous: ?*Pardes, resumed: bool) !void { /// the addon's). fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool) void { if (pane.addon) |slot| { - if (slot.path.len > 0 and filesystem.localPath(slot.path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + if (slot.path.len > 0 and filesystem.localPath(slot.path) != null) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); return reviveSlot(p, id, slot); } if (pane.terminal != null) { @@ -690,7 +690,7 @@ fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool f.content = text; } else |_| {} } - p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); // Its file gone from disk since the dump: its text is nowhere else, so // it is said deleted, and Del, Exit and Restore ask first. One never on // disk (a name not yet saved) comes back as it was. diff --git a/core/echo.zig b/core/echo.zig index 90dcfede..40200696 100644 --- a/core/echo.zig +++ b/core/echo.zig @@ -564,7 +564,7 @@ const Rig = struct { written: std.ArrayListUnmanaged(u8) = .empty, now: u64 = 1_000 * ms, - fn ptyWrite(ctx: ?*anyopaque, _: u8, bytes: []const u8) void { + fn ptyWrite(ctx: ?*anyopaque, _: pardes.Pane.Id, bytes: []const u8) void { const g: *Rig = @ptrCast(@alignCast(ctx.?)); g.written.appendSlice(testing.allocator, bytes) catch {}; } @@ -601,7 +601,7 @@ const Rig = struct { /// The shell prints `bytes`; the replica gets the unit and the delta. fn prints(g: *Rig, bytes: []const u8) !void { - g.a.output(.{ .output = .{ .pane = g.shell, .bytes = bytes } }); + g.a.output(.{ .output = .{ .pane = .at(g.shell), .bytes = bytes } }); try g.sync(); } diff --git a/core/edit.zig b/core/edit.zig index 214e83a8..0014f681 100644 --- a/core/edit.zig +++ b/core/edit.zig @@ -2352,7 +2352,7 @@ test "flat text replacement preserves a terminal fragment origin and direction" if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 12 }); defer p.deinit(); - p.update(.{ .output = .{ .pane = 0, .bytes = "first\r\nsecond\r\nthird\r\n界a\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "first\r\nsecond\r\nthird\r\n界a\r\n" } }); const pane = p.panes[0].?; pane.body.mode = .normal; pane.body.cur_pinned = true; @@ -2487,7 +2487,7 @@ test "a paste into a terminal is xterm's: bracketed when the program asked, cont fn f(pp: *Pardes, o: *std.ArrayList(u8)) ![]const u8 { o.clearRetainingCapacity(); while (pp.nextEffect()) |effect| { - if (effect == .write and effect.write.pane == 0) try o.appendSlice(pp.gpa, effect.write.bytes.slice()); + if (effect == .write and effect.write.pane.index() == 0) try o.appendSlice(pp.gpa, effect.write.bytes.slice()); } return o.items; } @@ -2496,7 +2496,7 @@ test "a paste into a terminal is xterm's: bracketed when the program asked, cont try std.testing.expectEqual(.tty, p.panes[0].?.body.mode); p.update(.{ .paste = "echo a\nb\x03\tc" }); try std.testing.expectEqualStrings("echo a\rb \tc", try take(p, &out)); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?2004h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?2004h" } }); _ = try take(p, &out); p.update(.{ .paste = "x\x1b[201~y\n" }); try std.testing.expectEqualStrings("\x1b[200~x [201~y\n\x1b[201~", try take(p, &out)); diff --git a/core/exec.zig b/core/exec.zig index 6a8ef6d8..687faa98 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -322,7 +322,7 @@ pub fn saveFile(p: *Pardes, id: usize) void { } f.disk_newer = null; } - p.emit(.{ .save_file = .{ .pane = @intCast(id) } }); + p.emit(.{ .save_file = .{ .pane = .at(id) } }); } pub fn saveTo(p: *Pardes, id: usize, path: []const u8) void { @@ -347,7 +347,7 @@ pub fn saveTo(p: *Pardes, id: usize, path: []const u8) void { // Save that fails changes nothing, the name included. if (f.output != null and panes.Output.fileTraits(f.output).saves) { p.emit(.{ .save_text = .{ - .pane = @intCast(id), + .pane = .at(id), .serial = pane.serial, .path = Pardes.SavePath.from(full), .promote = true, @@ -361,7 +361,7 @@ pub fn saveTo(p: *Pardes, id: usize, path: []const u8) void { fn askWrite(p: *Pardes, id: usize, serial: u32, path: []const u8) void { p.emit(.{ .save_text = .{ - .pane = @intCast(id), + .pane = .at(id), .serial = serial, .path = Pardes.SavePath.from(path), } }); diff --git a/core/fs.zig b/core/fs.zig index a79aa55c..0a10825d 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -1138,7 +1138,7 @@ test "unmount refuses pane paths inherited directories and queued save targets" try std.testing.expectError(error.MountInUse, unmount(p, "peer")); p.setCwd(.at(slot), "/"); while (p.nextEffect()) |_| {} - p.emit(.{ .save_text = .{ .pane = 0, .serial = pane.serial, .path = .from("/n/peer/pending") } }); + p.emit(.{ .save_text = .{ .pane = .at(0), .serial = pane.serial, .path = .from("/n/peer/pending") } }); try std.testing.expectError(error.MountInUse, unmount(p, "peer")); while (p.nextEffect()) |_| {} try unmount(p, "peer"); @@ -1317,7 +1317,7 @@ test "bounded reads release terminal snapshots after success and size refusal" { const empty = try readLimit(p, path, 0); defer gpa.free(empty); try std.testing.expectEqual(@as(usize, 0), empty.len); - p.update(.{ .output = .{ .pane = 0, .bytes = "limited terminal output" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "limited terminal output" } }); const expected = "limited terminal output\n"; // a line ends with its newline const bytes = try readLimit(p, path, expected.len); defer gpa.free(bytes); diff --git a/core/keys.zig b/core/keys.zig index 2f63d045..92e78b0e 100644 --- a/core/keys.zig +++ b/core/keys.zig @@ -543,7 +543,7 @@ test "Esc back into a tty leaves its view at the prompt" { // rides the view down and leaves that pin far up in the scrollback. for (0..60) |i| { var buf: [32]u8 = undefined; - p.update(.{ .output = .{ .pane = @intCast(shell), .bytes = std.fmt.bufPrint(&buf, "line {d}\r\n", .{i}) catch unreachable } }); + p.update(.{ .output = .{ .pane = .at(shell), .bytes = std.fmt.bufPrint(&buf, "line {d}\r\n", .{i}) catch unreachable } }); } p.sync(); const live = pardes.Term.grid(sp.terminal).offset; diff --git a/core/look.zig b/core/look.zig index b2a7b945..f7084079 100644 --- a/core/look.zig +++ b/core/look.zig @@ -1923,7 +1923,7 @@ test "Look hover waits without mutating the pane and input cancels it" { // Background traffic in another pane does not change the text or geometry // under this pointer and must not strand a stationary hover unarmed. - p.update(.{ .output = .{ .pane = @intCast(other_id), .bytes = "busy\r\n" } }); + p.update(.{ .output = .{ .pane = .at(other_id), .bytes = "busy\r\n" } }); try std.testing.expect(p.look_hover_preview != null); const seam_x = p.view().col_x[0] + p.view().col_w[0] - 1; @@ -2504,9 +2504,9 @@ test "LookNext in a command pane walks its own file:line places, whoever walked while (p.nextEffect()) |_| {} const dst = exec.execute(p, 0, "make") orelse return error.NoCommandPane; while (p.nextEffect()) |_| {} - p.update(.{ .output = .{ .pane = @intCast(dst), .bytes = "x.c:3:1: error: bad\r\ny.c:2:1: warning: odd\r\n" } }); - p.update(.{ .exited = .{ .pane = @intCast(dst), .status = 2 } }); - p.update(.{ .eof = .{ .pane = @intCast(dst) } }); + p.update(.{ .output = .{ .pane = .at(dst), .bytes = "x.c:3:1: error: bad\r\ny.c:2:1: warning: odd\r\n" } }); + p.update(.{ .exited = .{ .pane = .at(dst), .status = 2 } }); + p.update(.{ .eof = .{ .pane = .at(dst) } }); const cmd = p.panes[dst].?; // A walk last made elsewhere, and another pane with places in it. try std.testing.expect(p.executeBuiltinLine(0, "New")); diff --git a/core/lsp/Ask.zig b/core/lsp/Ask.zig index df6a058a..376b093d 100644 --- a/core/lsp/Ask.zig +++ b/core/lsp/Ask.zig @@ -113,7 +113,7 @@ pub fn lspRequest(p: *Pardes, asked: usize, kind: lsp.Kind, arg_in: []const u8) p.emit(.{ .lsp = .{ .id = p.lsp_ask.seq, .kind = kind, - .pane = @intCast(id), + .pane = .at(id), .offset = @intCast(at), .arg = .from(sent), } }); @@ -445,7 +445,7 @@ test "Lspwhy after a Hover asks to explain that hover, in its file at its place, try std.testing.expectEqual(lsp.Kind.explain, e.lsp.kind); try std.testing.expectEqualStrings("hover ", e.lsp.arg.slice()); try std.testing.expectEqual(hover_at, e.lsp.offset); - try std.testing.expectEqual(@as(u8, 0), e.lsp.pane); + try std.testing.expectEqual(0, e.lsp.pane.index()); seen = true; }; try std.testing.expect(seen); diff --git a/core/mouse.zig b/core/mouse.zig index 0ff4c7e3..1ef348f8 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -1580,17 +1580,17 @@ test "a wheel over a program that owns it is the program's, in the form it asked .{ .modes = "\x1b[?1007l", .button = .wheel_up, .sent = "" }, }; for (cases) |case| { - p.update(.{ .output = .{ .pane = 0, .bytes = case.modes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = case.modes } }); _ = pardes.test_api.ptyBytes(p, 0, &out); p.update(.{ .mouse = .{ .button = case.button, .kind = .press, .col = col, .row = row, .ctrl = case.ctrl, .alt = case.alt, .shift = case.shift } }); try std.testing.expectEqualStrings(case.sent, pardes.test_api.ptyBytes(p, 0, &out)); } // A shell on the main screen: the wheel scrolls its scrollback, and // the shell hears nothing. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1l\x1b[?1049l" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1l\x1b[?1049l" } }); for (0..40) |i| { var line: [16]u8 = undefined; - p.update(.{ .output = .{ .pane = 0, .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); } _ = try p.render(frame.allocator()); _ = pardes.test_api.ptyBytes(p, 0, &out); @@ -1600,7 +1600,7 @@ test "a wheel over a program that owns it is the program's, in the form it asked try std.testing.expect(pane.scroll() != was); // A program tracking the mouse on the main screen: Shift and the // wheel scroll the scrollback, the program hears nothing. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1000h\x1b[?1006h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1000h\x1b[?1006h" } }); _ = pardes.test_api.ptyBytes(p, 0, &out); const tracked = pane.scroll(); p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = col, .row = row, .shift = true } }); @@ -1671,7 +1671,7 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri } }.f; // htop's modes: clicks in SGR, a release its own `m`. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1049h\x1b[?1h\x1b[?1006;1000h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1049h\x1b[?1h\x1b[?1006;1000h" } }); _ = pardes.test_api.ptyBytes(p, 0, &out); try std.testing.expectEqualStrings("\x1b[<0;6;4M", try send(p, &out, col, row, .{ .button = .left, .kind = .press })); try std.testing.expect(p.view().drag == .none); @@ -1713,7 +1713,7 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri // codex's any-event tracking: a drag once per cell, carried past the // grid's edge and clamped there, and bare moves. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1003h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1003h" } }); _ = pardes.test_api.ptyBytes(p, 0, &out); try std.testing.expectEqualStrings("\x1b[<35;9;4M", try send(p, &out, col, row, .{ .button = .none, .kind = .motion, .dx = 3 })); try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .none, .kind = .motion, .dx = 3 })); @@ -1737,7 +1737,7 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri // Legacy X10 bytes: a release is button 3. (Turning 1003 off turns // tracking off, as xterm's one tracking mode goes.) - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1003l\x1b[?1006l\x1b[?1000h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1003l\x1b[?1006l\x1b[?1000h" } }); _ = pardes.test_api.ptyBytes(p, 0, &out); try std.testing.expectEqualStrings("\x1b[M &$", try send(p, &out, col, row, .{ .button = .left, .kind = .press })); try std.testing.expectEqualStrings("\x1b[M#&$", try send(p, &out, col, row, .{ .button = .left, .kind = .release })); diff --git a/core/ninep/ctl.zig b/core/ninep/ctl.zig index cf68fa76..302b916a 100644 --- a/core/ninep/ctl.zig +++ b/core/ninep/ctl.zig @@ -1282,7 +1282,7 @@ pub fn getFile(p: *Pardes, pane: *Pane, path: []const u8) GetError!void { f.disk_newer_warned = false; f.disk_gone = false; f.on_disk = fs.localPath(path) != null; - if (f.on_disk) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } }); + if (f.on_disk) p.emit(.{ .watch = .{ .pane = .at(id), .on = true } }); } /// What Get and Edit's `e` fail with, as their callers say it (the 9P @@ -1500,7 +1500,7 @@ test "a body write to a terminal is typed input, not a paste, even to a program const term = for (p.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break id; } else return error.SkipZigTest; - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = "\x1b[?2004h" } }); + p.update(.{ .output = .{ .pane = .at(term), .bytes = "\x1b[?2004h" } }); while (p.nextEffect()) |_| {} const w = wr(p, Node.of(p.panes[term].?.serial, .body), "ls\n"); try testing.expectEqual(Status.ok, w.reply.status); @@ -1513,7 +1513,7 @@ test "Save path of a file pane logs a save record naming that path" { const serial = serialOf(p); const saved = wr(p, Node.of(serial, .ctl), "Save /tmp/pardes-save-copy.txt\n"); try testing.expectEqual(Status.ok, saved.reply.status); - p.perform(.{ .save_text = .{ .pane = 0, .serial = serial, .path = Pardes.SavePath.from("/tmp/pardes-save-copy.txt") } }); + p.perform(.{ .save_text = .{ .pane = .at(0), .serial = serial, .path = Pardes.SavePath.from("/tmp/pardes-save-copy.txt") } }); var want: [64]u8 = undefined; try testing.expect(th.logHas(p, try std.fmt.bufPrint(&want, "save {d} /tmp/pardes-save-copy.txt\n", .{serial}))); } @@ -1590,7 +1590,7 @@ test "exec Save writes the named file through the host" { const saved = wr(p, Node.of(serial, .exec), "Save\n"); try testing.expectEqual(Status.ok, saved.reply.status); try testing.expect(saved.saved); - p.perform(.{ .save_file = .{ .pane = 0 } }); + p.perform(.{ .save_file = .{ .pane = .at(0) } }); try testing.expect(!pane_files.dirtyOf(pane)); try testing.expectEqualStrings(pane.file.?.content, p.fallback.get("/tmp/renamed.txt").?); } @@ -1990,7 +1990,7 @@ test "a terminal told the directory it already goes by logs no rename" { const session = p.sessionDir(); p.setCwd(.at(term), session); try testing.expect(!th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} ", .{p.panes[term].?.serial}))); - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = "$ " } }); + p.update(.{ .output = .{ .pane = .at(term), .bytes = "$ " } }); // Somewhere else is a rename. p.setCwd(.at(term), "/usr"); try testing.expect(th.logHas(p, try std.fmt.bufPrint(&rec, "rename {d} /usr", .{p.panes[term].?.serial}))); @@ -2001,13 +2001,13 @@ test "a terminal with text typed at its prompt is never typed into: a look at it defer p.deinit(); const term = p.paneBySerial(serialOf(p)).?; p.setCwd(.at(term), "/tmp"); - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = "\x1b]133;A;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07TYPED" } }); + p.update(.{ .output = .{ .pane = .at(term), .bytes = "\x1b]133;A;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07TYPED" } }); while (p.nextEffect()) |_| {} var count: usize = 0; for (p.panes) |slot| count += @intFromBool(slot != null); try testing.expectEqual(Status.ok, wr(p, @backingInt(tree.TopFile.look), "/tmp\n").reply.status); var typed_into = false; - while (p.nextEffect()) |effect| if (effect == .write and effect.write.pane == term) { + while (p.nextEffect()) |effect| if (effect == .write and effect.write.pane.index() == term) { typed_into = true; }; try testing.expect(!typed_into); @@ -2016,7 +2016,7 @@ test "a terminal with text typed at its prompt is never typed into: a look at it try testing.expectEqual(count + 1, after); // An exec at it runs in a pane of its own, not typed after TYPED. _ = wr(p, Node.of(serialOf(p), .exec), "echo hi\n"); - while (p.nextEffect()) |effect| if (effect == .write and effect.write.pane == term) { + while (p.nextEffect()) |effect| if (effect == .write and effect.write.pane.index() == term) { typed_into = true; }; try testing.expect(!typed_into); @@ -2260,8 +2260,8 @@ test "a focus moved over 9P tells the programs that asked for focus reports, as }; try testing.expectEqual(@as(usize, 2), n); p.view().active = shells[0]; - p.update(.{ .output = .{ .pane = @intCast(shells[0]), .bytes = "\x1b[?1004h" } }); - p.update(.{ .output = .{ .pane = @intCast(shells[1]), .bytes = "\x1b[?1004h" } }); + p.update(.{ .output = .{ .pane = .at(shells[0]), .bytes = "\x1b[?1004h" } }); + p.update(.{ .output = .{ .pane = .at(shells[1]), .bytes = "\x1b[?1004h" } }); while (p.nextEffect()) |_| {} var digits: [16]u8 = undefined; const moved = wr(p, @backingInt(tree.TopFile.focus), try std.fmt.bufPrint(&digits, "{d}\n", .{p.panes[shells[1]].?.serial})); @@ -2344,7 +2344,7 @@ test "a new file saved shows in /recent, open, while it is" { const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; // A host that writes files, as the real ones do. const Writing = struct { - fn write(_: ?*anyopaque, _: u8, path: []const u8, bytes: []const u8) void { + fn write(_: ?*anyopaque, _: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = bytes }) catch {}; } }; @@ -2357,7 +2357,7 @@ test "a new file saved shows in /recent, open, while it is" { _ = wr(p, Node.of(made, .name), try std.fmt.bufPrint(&name, "{s}\n", .{path})); _ = wr(p, Node.of(made, .body), "fresh\n"); try testing.expectEqual(Status.ok, wr(p, Node.of(made, .ctl), "Save\n").reply.status); - p.perform(.{ .save_file = .{ .pane = @intCast(p.paneBySerial(made).?) } }); + p.perform(.{ .save_file = .{ .pane = .at(p.paneBySerial(made).?) } }); var want: [4300]u8 = undefined; try testing.expect(std.mem.indexOf(u8, rd(p, @backingInt(tree.TopFile.recent), 0, 8192).bytes, try std.fmt.bufPrint(&want, "open {s}\n", .{path})) != null); } @@ -2575,7 +2575,7 @@ test "a word written to a raw terminal's look is listed as in normal mode" { defer p.deinit(); const serial = serialOf(p); const id = p.paneBySerial(serial).?; - p.update(.{ .output = .{ .pane = @intCast(id), .bytes = "one MARK two\r\n" } }); + p.update(.{ .output = .{ .pane = .at(id), .bytes = "one MARK two\r\n" } }); p.panes[id].?.body.mode = .tty; try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .look), "MARK\n").reply.status); const said = std.mem.trimEnd(u8, rd(p, Node.of(serial, .look), 0, 64).bytes, "\n"); @@ -2588,7 +2588,7 @@ test "@p<terminal>:<address> takes any address over the terminal's lines, select defer p.deinit(); const serial = serialOf(p); const id = p.paneBySerial(serial).?; - p.update(.{ .output = .{ .pane = @intCast(id), .bytes = "one\r\ntwo MARK three\r\n" } }); + p.update(.{ .output = .{ .pane = .at(id), .bytes = "one\r\ntwo MARK three\r\n" } }); const pane = p.panes[id].?; const root = @backingInt(tree.TopFile.look); var line: [64]u8 = undefined; diff --git a/core/ninep/pane.zig b/core/ninep/pane.zig index 76765535..8be26602 100644 --- a/core/ninep/pane.zig +++ b/core/ninep/pane.zig @@ -941,7 +941,7 @@ pub fn nameBuffer(p: *Pardes, id: usize, full: []const u8, preserve_tag: bool) ! f.context.revision = null; pane.geometry_key = 0; look.invalidateLookHover(p, id); - p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); + p.emit(.{ .watch = .{ .pane = .at(id), .on = false } }); events.noteLog(p, .rename, pane); // A name given: whether a file there is on disk is the disk's to say. f.on_disk = f.output == null and pardes.Pardes.recentKeeps(p.tape, f.path); @@ -1065,7 +1065,7 @@ test "a terminal's body stats as long as it reads" { while (p.nextEffect()) |_| {} const pane = p.panes[0].?; try testing.expect(pane.isTerminal()); - p.update(.{ .output = .{ .pane = 0, .bytes = "$ ls\r\nalpha beta\r\n$ " } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "$ ls\r\nalpha beta\r\n$ " } }); const body = Node.of(pane.serial, .body); const whole = rd(p, body, 0, 1 << 16); try testing.expect(whole.bytes.len > 0); diff --git a/core/ninep/pty.zig b/core/ninep/pty.zig index 96f2a6bf..6f3a8af0 100644 --- a/core/ninep/pty.zig +++ b/core/ninep/pty.zig @@ -96,14 +96,14 @@ fn verb(p: *Pardes, id: usize, line: []const u8, apply: bool) ?[]const u8 { // is refused, never quietly raised. if (rows < pty_rows_min) return e_winsize_rows; if (!apply) return null; - p.emit(.{ .resize_pty = .{ .pane = @intCast(id), .cols = cols, .rows = rows } }); + p.emit(.{ .resize_pty = .{ .pane = .at(id), .cols = cols, .rows = rows } }); pane.fs.winsize = .{ cols, rows }; }, .sig => { const which = signalNamed(words.next() orelse return e_bad_pty_ctl) orelse return e_bad_pty_ctl; if (words.next() != null) return e_bad_pty_ctl; if (!apply) return null; - p.emit(.{ .signal_pty = .{ .pane = @intCast(id), .serial = pane.serial, .sig = which } }); + p.emit(.{ .signal_pty = .{ .pane = .at(id), .serial = pane.serial, .sig = which } }); }, .exec => { if (words.next() != null) return e_bad_pty_ctl; @@ -709,7 +709,7 @@ test "pty/data writes at the shell and reads the raw stream" { try testing.expectEqual(Status.again, rd(p, data, 0, 64).reply.status); - p.update(.{ .output = .{ .pane = 0, .bytes = "unwatched" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "unwatched" } }); while (p.nextEffect()) |_| {} try testing.expectEqual(@as(usize, 0), p.panes[0].?.fs.pty_out.buf.items.len); try testing.expectEqual(Status.again, rd(p, data, 0, 64).reply.status); @@ -724,20 +724,20 @@ test "pty/data writes at the shell and reads the raw stream" { try testing.expectEqual(@as(u16, 0), p.fs.listeners); try testing.expect(p.panes[0].?.fs.readers == 0); - p.update(.{ .output = .{ .pane = 0, .bytes = "hello" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "hello" } }); while (p.nextEffect()) |_| {} try testing.expectEqualStrings("hello", rd(p, data, 0, 64).bytes); try testing.expectEqual(Status.again, rd(p, data, 0, 64).reply.status); - p.update(.{ .output = .{ .pane = 0, .bytes = "abcdef" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "abcdef" } }); while (p.nextEffect()) |_| {} try testing.expectEqualStrings("ab", rd(p, data, 0, 2).bytes); try testing.expectEqualStrings("cd", rd(p, data, 0, 2).bytes); - p.update(.{ .output = .{ .pane = 0, .bytes = "ghi" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "ghi" } }); while (p.nextEffect()) |_| {} try testing.expectEqualStrings("efghi", rd(p, data, 0, 64).bytes); - p.update(.{ .output = .{ .pane = 0, .bytes = "orphan" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "orphan" } }); while (p.nextEffect()) |_| {} _ = call(p, .{ .tag = 6, .op = .release, .node = data, .handle = reader.reply.handle }); try testing.expect(!p.panes[0].?.fs.pty_reader); @@ -750,7 +750,7 @@ test "pty/data writes at the shell and reads the raw stream" { try testing.expectEqual(Status.err, second.reply.status); try testing.expectEqualStrings(tree.e_in_use, second.reply.ename); try testing.expect(p.panes[0].?.fs.pty_reader); - p.update(.{ .output = .{ .pane = 0, .bytes = "still" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "still" } }); while (p.nextEffect()) |_| {} try testing.expectEqualStrings("still", rd(p, data, 0, 64).bytes); } @@ -765,7 +765,7 @@ test "a run waiting for a new terminal's first prompt is the only line typed the const node = Node.of(serialOf(p), .pty_run); const h = call(p, .{ .tag = 1, .op = .open, .node = node }).reply.handle; _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" }); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07" } }); var sent: [32]u8 = undefined; var len: usize = 0; while (p.nextEffect()) |e| if (e == .write) { @@ -783,7 +783,7 @@ test "run answers how its command ended once the shell is back at a prompt" { const node = Node.of(serialOf(p), .pty_run); const out = struct { fn put(pp: *Pardes, bytes: []const u8) void { - pp.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + pp.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); while (pp.nextEffect()) |_| {} } }; @@ -801,7 +801,7 @@ test "run answers how its command ended once the shell is back at a prompt" { // A respawn before that prompt keeps it waiting for the new shell's. p.acknowledgeShell(.at(0), "/bin/sh", true); try testing.expectEqual(Status.again, call(p, .{ .tag = 3, .op = .read, .node = node, .handle = early.reply.handle, .size = 64 }).reply.status); - p.update(.{ .output = .{ .pane = 0, .bytes = prompt } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = prompt } }); var sent_late: [16]u8 = undefined; var sent_len: usize = 0; while (p.nextEffect()) |e| if (e == .write) { @@ -909,7 +909,7 @@ test "a shell that exits under a run answers its status, and the log says exit b const serial = serialOf(p); const node = Node.of(serial, .pty_run); const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; - p.update(.{ .output = .{ .pane = 0, .bytes = prompt } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = prompt } }); while (p.nextEffect()) |_| {} const log = @backingInt(tree.TopFile.log); const f = call(p, .{ .tag = 1, .op = .open, .node = log }).reply.handle; @@ -918,9 +918,9 @@ test "a shell that exits under a run answers its status, and the log says exit b const r = call(p, .{ .tag = 4, .op = .open, .node = node }); _ = call(p, .{ .tag = 5, .op = .write, .node = node, .handle = r.reply.handle, .data = "echo bye; exit 3\n" }); - p.update(.{ .output = .{ .pane = 0, .bytes = "echo bye; exit 3\r\n\x1b]133;C;aid=pardes\x07bye\r\n" } }); - p.update(.{ .exited = .{ .pane = 0, .status = 3 } }); - p.update(.{ .eof = .{ .pane = 0 } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "echo bye; exit 3\r\n\x1b]133;C;aid=pardes\x07bye\r\n" } }); + p.update(.{ .exited = .{ .pane = .at(0), .status = 3 } }); + p.update(.{ .eof = .{ .pane = .at(0) } }); while (p.nextEffect()) |_| {} try testing.expect(p.paneBySerial(serial) == null); try testing.expectEqualStrings("exit 3\nbye\n", call(p, .{ .tag = 6, .op = .read, .node = node, .handle = r.reply.handle, .size = 64 }).bytes); @@ -969,7 +969,7 @@ test "a run's answer says cut when its output's start is gone, and reads a bound const pane = p.panes[0].?; const sh = struct { fn put(pp: *Pardes, bytes: []const u8) void { - pp.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + pp.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); while (pp.nextEffect()) |_| {} } fn send(pp: *Pardes, n: u64) u32 { @@ -1099,7 +1099,7 @@ test "a terminal's ctl counts its body, and a new directory is a rename in the l const p = try withTerm(gpa); defer p.deinit(); const serial = serialOf(p); - p.update(.{ .output = .{ .pane = 0, .bytes = "some history\r\nmore" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "some history\r\nmore" } }); while (p.nextEffect()) |_| {} const opened = call(p, .{ .tag = 1, .op = .open, .node = Node.of(serial, .body) }); const body_len = call(p, .{ .tag = 2, .op = .read, .node = Node.of(serial, .body), .handle = opened.reply.handle, .size = 1 << 16 }).bytes.len; diff --git a/core/ninep/screen.zig b/core/ninep/screen.zig index cd3c73b6..1e2791b1 100644 --- a/core/ninep/screen.zig +++ b/core/ninep/screen.zig @@ -124,7 +124,7 @@ test "terminal body handles keep one history snapshot across fragmented reads" { const p = try withTerm(gpa); defer p.deinit(); const node = Node.of(serialOf(p), .body); - p.update(.{ .output = .{ .pane = 0, .bytes = "old caf\xc3\xa9\r\nold tail" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "old caf\xc3\xa9\r\nold tail" } }); while (p.nextEffect()) |_| {} const original = try pardes.Term.scrollback(p.panes[0].?.terminal, gpa); defer gpa.free(original); @@ -132,7 +132,7 @@ test "terminal body handles keep one history snapshot across fragmented reads" { try testing.expectEqual(Status.ok, opened.reply.status); const first = call(p, .{ .tag = 2, .op = .read, .node = node, .handle = opened.reply.handle, .size = 3 }); try testing.expectEqualStrings(original[0..3], first.bytes); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3J\x1b[2J\x1b[Hnew output" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[3J\x1b[2J\x1b[Hnew output" } }); while (p.nextEffect()) |_| {} var offset: usize = 3; while (offset < original.len) { @@ -154,7 +154,7 @@ test "an empty terminal body snapshot stays empty while output continues" { const opened = call(p, .{ .tag = 1, .op = .open, .node = node }); try testing.expectEqualStrings("", call(p, .{ .tag = 2, .op = .read, .node = node, .handle = opened.reply.handle, .size = 32 }).bytes); try testing.expect(p.fs.opens[opened.reply.handle - 1].what.snapshot != null); - p.update(.{ .output = .{ .pane = 0, .bytes = "new output" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "new output" } }); while (p.nextEffect()) |_| {} try testing.expectEqualStrings("", call(p, .{ .tag = 3, .op = .read, .node = node, .handle = opened.reply.handle, .size = 32 }).bytes); _ = call(p, .{ .tag = 4, .op = .release, .node = node, .handle = opened.reply.handle }); @@ -182,7 +182,7 @@ test "terminal body snapshots are lazy bounded and released after the pane close const scratch = try th.newPane(p); const scratch_id = p.paneBySerial(scratch).?; try testing.expect(p.panes[scratch_id].?.file != null); - p.update(.{ .output = .{ .pane = 0, .bytes = "latest output" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "latest output" } }); while (p.nextEffect()) |_| {} var failing = std.testing.FailingAllocator.init(gpa, .{ .fail_index = 0 }); p.gpa = failing.allocator(); @@ -298,7 +298,7 @@ test "a terminal's body is its logical lines: a row the terminal wrapped is join const pane = p.panes[0].?; const long = &@as([50]u8, @splat('w')) ++ &@as([150]u8, @splat('x')); // wider than the pane: it wraps try testing.expect(long.len > pane.cols); - p.update(.{ .output = .{ .pane = 0, .bytes = long ++ "\r\nnext" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = long ++ "\r\nnext" } }); while (p.nextEffect()) |_| {} const node = Node.of(pane.serial, .body); const body = th.rd(p, node, 0, 4096).bytes; diff --git a/core/ninep/testing.zig b/core/ninep/testing.zig index 23a9199f..cf75d80d 100644 --- a/core/ninep/testing.zig +++ b/core/ninep/testing.zig @@ -153,15 +153,15 @@ pub fn nameAt(list: []const Dirent, want: []const u8) ?Dirent { /// none. pub const starting: pardes.Host.VTable = .{ .spawn = startNothing }; -fn startNothing(_: ?*anyopaque, _: u8, _: []const u8) void {} +fn startNothing(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} pub const FakeTty = struct { taken: bool, pub const vtable: pardes.Host.VTable = .{ .tty_taken = answer }; - fn answer(ctx: ?*anyopaque, pane: u8) bool { - _ = pane; + fn answer(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + _ = id; const f: *FakeTty = @ptrCast(@alignCast(ctx.?)); return f.taken; } diff --git a/core/panes.zig b/core/panes.zig index 7ea479b9..b928917a 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -1192,8 +1192,8 @@ pub fn retirePane(p: *Pardes, pane: *Pane) void { if (pane.file) |*file| panes.File.leaveTwins(p, file); const watched = (if (pane.file) |f| f.output == null else false) or (if (pane.addon) |k| k.path.len > 0 else false); for (p.panes, 0..) |slot, id| if (slot == pane) { - if (watched) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = false } }); - if (pane.isTerminal()) p.emit(.{ .close_pty = .{ .pane = @intCast(id) } }); + if (watched) p.emit(.{ .watch = .{ .pane = .at(id), .on = false } }); + if (pane.isTerminal()) p.emit(.{ .close_pty = .{ .pane = .at(id) } }); // Its pty's end and its emulator are the authority's: a replica's // speculation that closes a terminal waits for it. if (pane.terminal != null) tape.poison("close a terminal"); @@ -1294,7 +1294,7 @@ pub fn emitSpawn(p: *Pardes, id: usize, serial: u32, cwd: []const u8) void { if (p.spawn_dirs[id]) |old| p.gpa.free(old); p.spawn_dirs[id] = null; if (cwd.len > effect_path_cap) p.spawn_dirs[id] = p.gpa.dupe(u8, cwd) catch null; - p.emit(.{ .spawn = .{ .pane = @intCast(id), .serial = serial, .cwd = .from(if (cwd.len > effect_path_cap) "" else cwd) } }); + p.emit(.{ .spawn = .{ .pane = .at(id), .serial = serial, .cwd = .from(if (cwd.len > effect_path_cap) "" else cwd) } }); } /// A command pane: a terminal whose child is `$SHELL -c line` rather @@ -1713,7 +1713,7 @@ test "owned cwd EOF failure retains the pane and Del retries its close" { p.sync(); while (p.nextEffect()) |_| {} allocator.fail_index = allocator.alloc_index; - p.update(.{ .eof = .{ .pane = 0 } }); + p.update(.{ .eof = .{ .pane = .at(0) } }); try std.testing.expectEqual(parent, p.panes[0].?); try std.testing.expectEqual(parent, child.cwd.inherited); try std.testing.expectEqual(Text.Mode.normal, parent.body.mode); diff --git a/core/replica.zig b/core/replica.zig index 625153d1..3f04b3bc 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -2278,7 +2278,7 @@ pub const Peers = struct { if (ps.next_id == 0) ps.next_id = 1; // The session's digest as dumped, for the joiner to check its load by. const digest = tape.digestOf(core); - peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = .at(k), .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; if (ps.threads) peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null; if (ps.threads) peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null; if (ps.threads and (peer.writer == null or peer.reader == null)) { @@ -2591,7 +2591,7 @@ fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Hav .welcome => |w| { r.j.pid = w.pid; r.j.client = w.client; - r.j.view = w.view; + r.j.view = w.view.index(); r.j.gmtoff = w.gmtoff; r.j.digest = w.digest; r.j.semantics = w.semantics; @@ -3292,7 +3292,7 @@ const TestHost = struct { /// A host with ptys whose pump polls for a frame, as a client's does. const PtyHost = struct { - fn resized(_: ?*anyopaque, _: u8, _: u16, _: u16) void {} + fn resized(_: ?*anyopaque, _: pardes.Pane.Id, _: u16, _: u16) void {} fn poll(_: ?*anyopaque) void {} const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll }; fn host() pardes.Host { @@ -3729,7 +3729,7 @@ test "a delta applied under a speculation outlives its rollback: the terminal st for (0..3) |round| { // The shell prints: its unit rolls the replica back and applies the // key again; then its delta lands, under the new copy. - for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "round {d} line {d}\r\n", .{ round, i }) } }); + for (0..40) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "round {d} line {d}\r\n", .{ round, i }) } }); r.confirm((try a.pending()).?); r.settle(); try std.testing.expect(r.copy.held); @@ -3751,7 +3751,7 @@ test "a delta applied under a speculation outlives its rollback: the terminal st r.confirm(try a.local(.{ .focus = false })); _ = try pardes.edit.shellRows(r.core(), r.core().panes[shell].?); try key(r, 'x', &sent); - for (0..40) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); + for (0..40) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "last line {d}\r\n", .{i}) } }); r.confirm((try a.pending()).?); r.settle(); try deliver(a, &screens, peer, r, .flush); @@ -3875,7 +3875,7 @@ test "a terminal sized again by a client's key: the step reads the grid it had, // History the replica holds as holes, of lines a narrow grid wraps in // two, which a wider one unwraps. var line: [96]u8 = undefined; - for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i}) } }); + for (0..60) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i}) } }); r.confirm((try a.pending()).?); // Out of terminal mode in the session's view, which sizes it; then a // client's own view (a copy of it), wider: its first key sizes the @@ -3954,7 +3954,7 @@ test "pardes's own keys over a terminal's history apply at once: its view moves a.core.view().active = shell; r.confirm(try a.local(.{ .focus = true })); var line: [64]u8 = undefined; - for (0..200) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "history line {d}\r\n", .{i}) } }); + for (0..200) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "history line {d}\r\n", .{i}) } }); r.confirm((try a.pending()).?); try deliver(a, &screens, peer, r, .flush); r.settle(); @@ -3977,7 +3977,7 @@ test "pardes's own keys over a terminal's history apply at once: its view moves // The shell prints while they wait: its unit, then its delta, which // sets the view the authority's (at the bottom still): what was applied // goes on top of it again. - for (0..5) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "late line {d}\r\n", .{i}) } }); + for (0..5) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "late line {d}\r\n", .{i}) } }); r.confirm((try a.pending()).?); r.settle(); try deliver(a, &screens, peer, r, .flush); @@ -4043,7 +4043,7 @@ test "a unit kept over a copy taken ahead is never replayed over a delta that ca a.core.view().active = shell; r.confirm(try a.local(.{ .focus = true })); var line: [96]u8 = undefined; - for (0..30) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} {s}\r\n", .{ i, "words of a length its own"[0 .. i % 20 + 4] }) } }); + for (0..30) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "line {d} {s}\r\n", .{ i, "words of a length its own"[0 .. i % 20 + 4] }) } }); r.confirm((try a.pending()).?); try deliver(a, &screens, peer, r, .flush); // Out of the shell's tty mode, in the session's view: keys over its text. @@ -4061,8 +4061,8 @@ test "a unit kept over a copy taken ahead is never replayed over a delta that ca r.settle(); } // The program redraws the screen: other rows where those keys read. - a.output(.{ .output = .{ .pane = shell, .bytes = "\x1b[H\x1b[2J" } }); - for (0..30) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); + a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b[H\x1b[2J" } }); + for (0..30) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "{d}\r\n", .{i}) } }); r.confirm((try a.pending()).?); r.settle(); try deliver(a, &screens, peer, r, .flush); @@ -4174,7 +4174,7 @@ test "a replica joined from a dump replays a step that opens a file's pane and s defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const zon = pardes.Kind.loadedOf(&Zon.ops, "/x/zon.so", @splat(9), &words); - _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &zon } }); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &zon } }); const r = try joinedNow(a, 1, 0); defer r.deinit(); try std.testing.expectEqual(digestOf(a), r.digest()); @@ -4703,7 +4703,7 @@ test "a step that reads a terminal's rows as the core keeps them sends a new cli } else return error.NoShell; a.core.view().active = shell; var line: [64]u8 = undefined; - for (0..60) |i| a.output(.{ .output = .{ .pane = shell, .bytes = try std.fmt.bufPrint(&line, "line {d} of the shell's history\r\n", .{i}) } }); + for (0..60) |i| a.output(.{ .output = .{ .pane = .at(shell), .bytes = try std.fmt.bufPrint(&line, "line {d} of the shell's history\r\n", .{i}) } }); const out = try gpa.dupe(u8, (try a.pending()).?); defer gpa.free(out); for ([_]*Replica{ r1, r2 }) |r| r.confirm(out); @@ -4733,7 +4733,7 @@ test "a replica with no code for a kind whose state is not plain shows its pane // replica's core takes the kind's words, never its code. var words: [64]pardes.Kind.LoadedWord = undefined; const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words); - r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } })); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &counter } })); r.confirm(try a.local(.{ .command = "Counter" })); try std.testing.expect(r.core().kinds.ops[r.core().kinds.n - 1] == null); try std.testing.expect(try shows(r.core(), "addon counter 1 does not run in this client")); @@ -4791,7 +4791,7 @@ test "a kind's place moved in a step of another view's: a replica's key that rea defer r.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); - r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &placed } })); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &placed } })); // The replica's view opens the kind's pane, and an Esc goes back to // the file (Last): view 0 has the file. const own = try a.openView(1); @@ -4845,7 +4845,7 @@ test "a client joined from a dump numbers a kind's next job as the session does" defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const jobber = pardes.Kind.loadedOf(&Jobber.ops, "/x/jobber.so", @splat(7), &words); - _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &jobber } }); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &jobber } }); _ = try a.local(.{ .command = "Jobber" }); for (0..3) |_| _ = try a.local(.{ .key = .{ .cp = 'x', .text = "x" } }); const id = a.core.view().active; @@ -4864,7 +4864,7 @@ test "a replica's word that opens a kind's pane with no file waits for the autho defer r.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); - r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &placed } })); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &placed } })); // Its replica has no code for the kind: the open's outcome (its place, // `1:0 0`) is the authority's to say. const msg = try gpa.dupe(u8, try r.input(.{ .command = "Placed" })); @@ -4883,7 +4883,7 @@ test "a client joined from a dump tells a kind each view's facts as the session defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const placed = pardes.Kind.loadedOf(&Placed.ops, "/x/placed.so", @splat(7), &words); - _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &placed } }); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &placed } }); _ = try a.local(.{ .command = "Placed" }); // A message in its pane: a band over its body, in each view. _ = try a.local(.{ .command = "Placed x" }); @@ -4937,7 +4937,8 @@ const Asker = struct { const BusyHost = struct { var core: ?*Pardes = null; var tasks: @import("selection_pipe.zig").Tasks = .{ .len = @import("selection_pipe.zig").Tasks.capacity }; - fn job(_: ?*anyopaque, pane: u8, serial: u32, token: u32) void { + fn job(_: ?*anyopaque, id: pardes.Pane.Id, serial: u32, token: u32) void { + const pane = id.index(); pardes.host_io.KindJob.start(std.testing.io, core.?, &tasks, pane, serial, token, null, never); } fn never(_: ?*anyopaque, j: *pardes.host_io.KindJob) void { @@ -4955,7 +4956,7 @@ test "a kind's job answered at once inside its host's call (no worker free) is t defer r.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const asker = pardes.Kind.loadedOf(&Asker.ops, "/x/asker.so", @splat(7), &words); - r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &asker } })); + r.confirm(try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &asker } })); // The client has its code (as a library of its own found by its scan). r.core().kinds.ops[r.core().kinds.byName("asker").?] = &Asker.ops; r.confirm(try a.local(.{ .command = "Asker" })); @@ -4981,7 +4982,7 @@ test "a closed terminal with a kitty image brought back on a client: its image d } else return error.NoShell; // A 2x2 picture in RGB (f=24): drawn, the emulator keeps it as RGBA // of its own (TerminalImages.converted). - a.output(.{ .output = .{ .pane = shell, .bytes = "\x1b_Ga=T,i=7,f=24,s=2,v=2;ESIzESIzESIzESIz\x1b\\" } }); + a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b_Ga=T,i=7,f=24,s=2,v=2;ESIzESIzESIzESIz\x1b\\" } }); r.confirm((try a.pending()).?); // Closed and brought back: the client loads the session's dump of it, // its image with it. @@ -5034,7 +5035,7 @@ test "a far client's own Del of a pane whose kind's state is not plain, rolled b defer a.deinit(); var words: [64]pardes.Kind.LoadedWord = undefined; const counter = pardes.Kind.loadedOf(&Counter.ops, "/x/counter.so", @splat(7), &words); - _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &counter } }); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &counter } }); _ = try a.local(.{ .command = "Counter" }); // Joined from the dump: the kind's pane is a line here (no code), its // slot the dump's. @@ -5101,7 +5102,7 @@ const SeenHost = struct { of(ctx).linked += 1; } fn watchTheme(_: ?*anyopaque, _: u32, _: bool) void {} - fn watchFile(_: ?*anyopaque, _: u8, _: []const u8, _: bool, _: pardes.WatchMode) void {} + fn watchFile(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8, _: bool, _: pardes.WatchMode) void {} const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .set_clipboard = setClipboard, .read_clipboard = readClipboard, .open_link = openLink, .watch_theme = watchTheme, .watch_file = watchFile }; fn host(h: *SeenHost) pardes.Host { return .{ .ctx = h, .vtable = &vtable }; @@ -5256,7 +5257,7 @@ test "a client's n in a kind's pane is a motion: it steps to the next place a re const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd))); var words: [64]pardes.Kind.LoadedWord = undefined; const still = pardes.Kind.loadedOf(&Still.ops, "/x/still.so", @splat(7), &words); - _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &still } }); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &still } }); // The kind's pane, its +Search (a place a row, as a kind's `/` leaves // it), and a scratch whose words name files in the session's directory // (as a command's output would). @@ -5563,14 +5564,14 @@ test "a far client's every word and key, on a file's pane, a kind's and a shell' if (slot) |pane| if (pane.isTerminal()) break id; } else null; if (shell) |id| { - a.output(.{ .output = .{ .pane = @intCast(id), .bytes = "\x1b[?1004h$ " } }); + a.output(.{ .output = .{ .pane = .at(id), .bytes = "\x1b[?1004h$ " } }); _ = (try a.pending()).?; } _ = try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/mise.toml", .{dir}) }); const file = a.core.view().active; var words: [64]pardes.Kind.LoadedWord = undefined; const still = pardes.Kind.loadedOf(&Still.ops, "/x/still.so", @splat(7), &words); - _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &still } }); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &still } }); _ = try a.local(.{ .command = "Still" }); const kind = a.core.view().active; try std.testing.expect(file != kind and a.core.panes[file].?.file != null and a.core.panes[kind].?.addon != null); @@ -5670,7 +5671,7 @@ test "a far client's click into a shell whose program asked for focus events is const Told = struct { var bytes: [64]u8 = undefined; var len: usize = 0; - fn write(_: ?*anyopaque, _: u8, b: []const u8) void { + fn write(_: ?*anyopaque, _: pardes.Pane.Id, b: []const u8) void { @memcpy(bytes[len..][0..b.len], b); len += b.len; } @@ -5682,7 +5683,7 @@ test "a far client's click into a shell whose program asked for focus events is const shell: u8 = for (a.core.panes, 0..) |slot, id| { if (slot) |pane| if (pane.isTerminal()) break @intCast(id); } else return error.NoShell; - a.output(.{ .output = .{ .pane = shell, .bytes = "\x1b[?1004h$ " } }); + a.output(.{ .output = .{ .pane = .at(shell), .bytes = "\x1b[?1004h$ " } }); _ = (try a.pending()).?; _ = try a.local(.{ .command = "New" }); var screens: Screens = .init(gpa); diff --git a/core/root.zig b/core/root.zig index 706c833d..681be15d 100644 --- a/core/root.zig +++ b/core/root.zig @@ -221,7 +221,7 @@ pub const test_api = if (@import("builtin").is_test) struct { var n: usize = 0; while (p.nextEffect()) |effect| switch (effect) { .write => |w| { - if (pane != null and w.pane != pane.?) continue; + if (pane != null and w.pane.index() != pane.?) continue; const b = w.bytes.slice(); if (n + b.len > buf.len) break; @memcpy(buf[n..][0..b.len], b); diff --git a/core/views.zig b/core/views.zig index a8bf8e60..9985288d 100644 --- a/core/views.zig +++ b/core/views.zig @@ -859,7 +859,7 @@ test "a terminal shown in a view shorter than it keeps its cursor in sight" { defer out.deinit(gpa); for (1..rows) |i| try out.print(gpa, "row-{d}\r\n", .{i}); try out.appendSlice(gpa, "LAST$ "); - p.update(.{ .output = .{ .pane = @intCast(id), .bytes = out.items } }); + p.update(.{ .output = .{ .pane = .at(id), .bytes = out.items } }); // Sized for the tall view, it fits there whole: nothing is passed over. try testing.expectEqual(@as(u16, 0), term.pan); p.toView(.at(0)); diff --git a/dev/Host.zig b/dev/Host.zig index 1834e52e..24df3895 100644 --- a/dev/Host.zig +++ b/dev/Host.zig @@ -24,30 +24,30 @@ pub const VTable = struct { /// for the daemon to attach again (daemon.zig). detach: ?*const fn (ctx: ?*anyopaque) void = null, /// Adopt: the shells a dead session left, asked of the daemon. - adopt: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, + adopt: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, /// Upgrade: this process exec'd as the pardes installed since, at /// the step's end (upgrade.zig); `pane` hears why if it cannot be. - upgrade: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, + upgrade: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, /// Whether a newer pardes is installed at the path this process /// started from, to be said once (upgrade.zig): asked at a focus-in. newer_build: ?*const fn (ctx: ?*anyopaque) bool = null, - spawn: ?*const fn (ctx: ?*anyopaque, pane: u8, cwd: []const u8) void = null, - pty_write: ?*const fn (ctx: ?*anyopaque, pane: u8, bytes: []const u8) void = null, - pty_resize: ?*const fn (ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void = null, - pty_signal: ?*const fn (ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void = null, - close_pty: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, - tty_taken: ?*const fn (ctx: ?*anyopaque, pane: u8) bool = null, + spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8) void = null, + pty_write: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, bytes: []const u8) void = null, + pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cols: u16, rows: u16) void = null, + pty_signal: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, sig: pardes.PtySignal) void = null, + close_pty: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, + tty_taken: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) bool = null, /// The name of the program holding the tty, when it is not the /// shell (host_io.foregroundName), in `buf`. - fg_name: ?*const fn (ctx: ?*anyopaque, pane: u8, buf: []u8) ?[]const u8 = null, + fg_name: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, buf: []u8) ?[]const u8 = null, /// Answered at once, not queued as an effect: Kill asks while the /// job it saw running is still the one in the tty (host_io.killJob). - kill_job: ?*const fn (ctx: ?*anyopaque, pane: u8) bool = null, - write_file: ?*const fn (ctx: ?*anyopaque, pane: u8, path: []const u8, bytes: []const u8) void = null, + kill_job: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) bool = null, + write_file: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, path: []const u8, bytes: []const u8) void = null, write_dump: ?*const fn (ctx: ?*anyopaque, bytes: []const u8) void = null, - watch_file: ?*const fn (ctx: ?*anyopaque, pane: u8, path: []const u8, on: bool, mode: pardes.WatchMode) void = null, + watch_file: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, path: []const u8, on: bool, mode: pardes.WatchMode) void = null, watch_theme: ?*const fn (ctx: ?*anyopaque, generation: u32, on: bool) void = null, - dump_themes: ?*const fn (ctx: ?*anyopaque, pane: u8) void = null, + dump_themes: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, set_clipboard: ?*const fn (ctx: ?*anyopaque, text: []const u8) void = null, read_clipboard: ?*const fn (ctx: ?*anyopaque) void = null, open_link: ?*const fn (ctx: ?*anyopaque, url: []const u8) void = null, @@ -65,5 +65,5 @@ pub const VTable = struct { /// Run job `token` of the pane in slot `pane` (whose serial is /// `serial`) on a worker (`KindJob`), answering with the `job` event; /// absent, it runs on the loop. - job: ?*const fn (ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void = null, + job: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, serial: u32, token: u32) void = null, }; diff --git a/dev/Inbox.zig b/dev/Inbox.zig index d8b14221..ed3a9835 100644 --- a/dev/Inbox.zig +++ b/dev/Inbox.zig @@ -8,8 +8,8 @@ const Lsp = host_io.Lsp; const Inbox = @This(); pub const Msg = union(enum) { - output: struct { pane: u8, gen: u32, bytes: []u8 }, - eof: struct { pane: u8, gen: u32, failure: ?anyerror = null }, + output: struct { pane: pardes.Pane.Id, gen: u32, bytes: []u8 }, + eof: struct { pane: pardes.Pane.Id, gen: u32, failure: ?anyerror = null }, lsp: struct { id: u32, rows: ?[]u8 }, lsp_status: []u8, pipe: selection_pipe.Response, @@ -96,18 +96,18 @@ pub fn wake(q: *Inbox) void { if (q.wake_fn) |f| f(q.wake_ctx); } -pub fn acceptReader(q: *Inbox, pane: u8, gen: u32) void { +pub fn acceptReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { q.lock(); defer q.unlock(); - std.debug.assert(q.readers[pane] == null); - q.readers[pane] = gen; + std.debug.assert(q.readers[pane.index()] == null); + q.readers[pane.index()] = gen; } -pub fn cancelReader(q: *Inbox, pane: u8, gen: u32) void { +pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { q.lock(); defer q.unlock(); - if (q.readers[pane] != gen) return; - q.readers[pane] = null; + if (q.readers[pane.index()] != gen) return; + q.readers[pane.index()] = null; var i: usize = 0; while (i < q.len) { const msg = q.items[(q.head + i) % q.items.len]; @@ -121,14 +121,14 @@ pub fn cancelReader(q: *Inbox, pane: u8, gen: u32) void { q.space.broadcast(q.io); } -pub fn pushOutput(q: *Inbox, pane: u8, gen: u32, bytes: []u8) bool { +pub fn pushOutput(q: *Inbox, pane: pardes.Pane.Id, gen: u32, bytes: []u8) bool { q.lock(); - while (!q.closed and q.readers[pane] == gen and q.len >= output_capacity) { + while (!q.closed and q.readers[pane.index()] == gen and q.len >= output_capacity) { q.waiting += 1; q.space.waitUncancelable(q.io, &q.mutex); q.waiting -= 1; } - if (q.closed or q.readers[pane] != gen) { + if (q.closed or q.readers[pane.index()] != gen) { q.unlock(); q.gpa.free(bytes); return false; @@ -186,7 +186,7 @@ pub fn push(q: *Inbox, m: Msg) void { }, else => { if (m == .eof) { - if (q.readers[m.eof.pane] != m.eof.gen) { + if (q.readers[m.eof.pane.index()] != m.eof.gen) { q.unlock(); return; } @@ -270,13 +270,13 @@ pub fn deinit(q: *Inbox) void { /// A pane's reader: its pty's output pushed as it comes, then its end /// of file; canceled (closePty), it pushes nothing more. -pub fn startReader(q: *Inbox, pty: std.Io.File, pane: u8, gen: u32) !std.Io.Future(void) { +pub fn startReader(q: *Inbox, pty: std.Io.File, pane: pardes.Pane.Id, gen: u32) !std.Io.Future(void) { q.acceptReader(pane, gen); errdefer q.cancelReader(pane, gen); return q.io.concurrent(readPty, .{ q, pty, pane, gen }); } -fn readPty(q: *Inbox, pty: std.Io.File, pane: u8, gen: u32) void { +fn readPty(q: *Inbox, pty: std.Io.File, pane: pardes.Pane.Id, gen: u32) void { var buf: [0x10000]u8 = undefined; var failure: ?anyerror = null; while (true) { diff --git a/dev/KindJob.zig b/dev/KindJob.zig index c24ad88d..e9cea5b4 100644 --- a/dev/KindJob.zig +++ b/dev/KindJob.zig @@ -48,7 +48,7 @@ pub fn run(j: *KindJob) void { /// Its answer to the core, and it gone. pub fn land(j: *KindJob, p: *pardes.Pardes) void { if (j.draw != null) return pardes.Kind.landDraw(p, j); - p.update(.{ .job = .{ .pane = j.pane, .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); + p.update(.{ .job = .{ .pane = .at(j.pane), .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); j.drop(); } diff --git a/dev/Lsp.zig b/dev/Lsp.zig index d7f11bfe..4b8bfa03 100644 --- a/dev/Lsp.zig +++ b/dev/Lsp.zig @@ -5,7 +5,7 @@ const filesystem = @import("core").filesystem; pub const Request = struct { id: u32, kind: pardes.lsp.Kind, - pane: u8, + pane: pardes.Pane.Id, offset: u32, arg: []const u8, }; @@ -45,8 +45,8 @@ pub const Job = struct { // Copy before starting a worker; the editor may replace any source slice afterward. pub fn snapshot(gpa: std.mem.Allocator, core: *const pardes.Pardes, req: Request) !*Job { - if (req.pane >= core.panes.len) return error.NoPane; - const pane = core.panes[req.pane] orelse return error.NoPane; + if (req.pane.index() >= core.panes.len) return error.NoPane; + const pane = core.panes[req.pane.index()] orelse return error.NoPane; const file = pane.file; const job = try gpa.create(Job); errdefer gpa.destroy(job); @@ -87,7 +87,7 @@ pub fn snapshot(gpa: std.mem.Allocator, core: *const pardes.Pardes, req: Request var copied: usize = 0; for (core.panes, 0..) |slot, i| { const other = slot orelse continue; - if (i == req.pane) continue; + if (i == req.pane.index()) continue; const f = other.file orelse continue; if (f.output != null) continue; if (copied + f.content.len > pardes.memory.limits.lsp_snapshot_bytes) continue; @@ -118,12 +118,12 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, core: *pardes.Pardes, task: *?T } const job = snapshot(gpa, core, req) catch |err| { core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return core.reportError(.at(req.pane), "lsp", err); + return core.reportError(req.pane, "lsp", err); }; const future = io.concurrent(work, .{ io, gpa, job, ctx, deliver }) catch |err| { job.free(gpa); core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return core.reportError(.at(req.pane), "lsp", err); + return core.reportError(req.pane, "lsp", err); }; task.* = .{ .id = req.id, .future = future }; } @@ -173,7 +173,7 @@ test "a snapshot owns every byte the backend will read" { const job = try snapshot(gpa, core, .{ .id = 7, .kind = .status, - .pane = @intCast(core.view().active), + .pane = .at(core.view().active), .offset = 0, .arg = &needle, }); @@ -202,7 +202,7 @@ test "LSP snapshot frees every partially copied field on allocation failure" { const job = try snapshot(gpa, p, .{ .id = 7, .kind = .hover, - .pane = @intCast(p.view().active), + .pane = .at(p.view().active), .offset = 6, .arg = "query", }); @@ -226,7 +226,7 @@ test "LSP snapshots translate explicit OS paths once and retain virtual names" { const replacement = try gpa.dupe(u8, case.path); gpa.free(pane.file.?.path); pane.file.?.path = replacement; - const job = try snapshot(gpa, core, .{ .id = 1, .kind = .hover, .pane = @intCast(core.view().active), .offset = 0, .arg = "" }); + const job = try snapshot(gpa, core, .{ .id = 1, .kind = .hover, .pane = .at(core.view().active), .offset = 0, .arg = "" }); defer job.free(gpa); try std.testing.expectEqualStrings(case.native, job.path); try std.testing.expectEqualStrings(std.Io.Dir.path.dirname(case.native).?, job.root); @@ -247,7 +247,7 @@ test "a pane that is gone yields no job rather than a null deref" { try std.testing.expectError(error.NoPane, snapshot(gpa, core, .{ .id = 1, .kind = .definition, - .pane = empty, + .pane = .at(empty), .offset = 0, .arg = "", })); @@ -272,7 +272,7 @@ test "work consumes the job and hands its rows to the sink" { const job = try snapshot(gpa, core, .{ .id = 42, .kind = .status, - .pane = @intCast(core.view().active), + .pane = .at(core.view().active), .offset = 0, .arg = "", }); @@ -320,7 +320,7 @@ test "LSP edit query failure leaves text and undo untouched before a successful const job = try snapshot(failing.allocator(), core, .{ .id = core.lsp_ask.wait.?.id, .kind = kind, - .pane = @intCast(core.view().active), + .pane = .at(core.view().active), .offset = 6, .arg = "renamed", }); @@ -389,7 +389,7 @@ test "LSP work delivers failure when transferring result ownership cannot alloca } }; const core = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); defer core.deinit(); - const job = try snapshot(gpa, core, .{ .id = 37, .kind = .status, .pane = @intCast(core.view().active), .offset = 0, .arg = "" }); + const job = try snapshot(gpa, core, .{ .id = 37, .kind = .status, .pane = .at(core.view().active), .offset = 0, .arg = "" }); var sink: Sink = .{}; work(std.testing.io, gpa, job, &sink, Sink.take); if (sink.rows) |rows| gpa.free(rows); diff --git a/dev/file_watch.zig b/dev/file_watch.zig index a02ad272..84f37c81 100644 --- a/dev/file_watch.zig +++ b/dev/file_watch.zig @@ -706,7 +706,7 @@ test "a restored file is watched again: one clean takes what its file became sin defer watchPane(fd, &watches, id, null, pane.serial, .{ .text = 0 }); var armed = false; while (core.nextEffect()) |effect| switch (effect) { - .watch => |watch| if (watch.pane == id and watch.on) { + .watch => |watch| if (watch.pane.index() == id and watch.on) { _ = applyEffect(core, io, fd, &watches, id, watch.on, watch.mode); armed = true; }, @@ -774,7 +774,7 @@ test "ordinary file watches still reconcile changes between open and watch" { defer watchPane(fd, &watches, id, null, pane.serial, .{ .text = 0 }); var armed = false; while (core.nextEffect()) |effect| switch (effect) { - .watch => |watch| if (watch.pane == id and watch.on) { + .watch => |watch| if (watch.pane.index() == id and watch.on) { try std.testing.expectEqual(.reconcile, watch.mode); try std.testing.expect(!applyEffect(core, io, fd, &watches, id, watch.on, watch.mode)); armed = true; @@ -949,7 +949,7 @@ test "a watched file deleted wakes the watch at once, and its pane says so" { var watches: Table = @splat(null); defer watchPane(fd, &watches, id, null, pane.serial, .{ .text = 0 }); while (core.nextEffect()) |effect| switch (effect) { - .watch => |watch| if (watch.pane == id and watch.on) { + .watch => |watch| if (watch.pane.index() == id and watch.on) { _ = applyEffect(core, io, fd, &watches, id, watch.on, watch.mode); }, else => {}, diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 3d050bbf..c147c2e2 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -1130,7 +1130,7 @@ const Own = struct { pub fn start(o: *Own, l: *Session) !void { for (&o.ptys, 0..) |*slot, id| if (slot.*) |*pt| { - pt.reader = try o.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), o.gens[id]); + pt.reader = try o.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, .at(id), o.gens[id]); }; if (o.inotify_fd >= 0) { o.watch_stop = try stopPipe(); @@ -2485,7 +2485,8 @@ const Shell = struct { took_input: bool = false, held: ?c.SDL_Event = null, - fn closePaneShell(ctx: ?*anyopaque, pane: u8) void { + fn closePaneShell(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); shellOf(ctx).closePty(pane); } @@ -2493,7 +2494,7 @@ const Shell = struct { const pt = if (s.ptys[pane]) |*pt| pt else return; // Out of the queue first, which lets go a reader waiting for room // in it; then its read is canceled (or, at its end, it is joined). - s.queue.cancelReader(pane, s.gens[pane]); + s.queue.cancelReader(.at(pane), s.gens[pane]); if (pt.reader) |*reader| { reader.cancel(s.queue.io); // the Io it was started on (startReader) pt.reader = null; @@ -2603,12 +2604,14 @@ const Shell = struct { return shellOf(ctx).gui != null; } - fn adopt(ctx: ?*anyopaque, pane: u8) void { + fn adopt(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const s = shellOf(ctx); s.adoptions.ask(s.core, pane); } - fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { + fn upgradeAsked(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const s = shellOf(ctx); if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); s.upgrade_asked = pane; @@ -2656,7 +2659,7 @@ const Shell = struct { const failed = upgrade.dumpInto(core, &rec, &at_buf, &why) orelse upgrade.execAs(s.gpa, path, &rec, &at_buf, s.fs, &why); // As it was: its ptys read on. for (s.ptys, 0..) |*slot, id| if (slot.*) |*pt| if (pt.fd >= 0 and pt.reader == null) { - pt.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, @intCast(id), s.gens[id]) catch null; + pt.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, .at(id), s.gens[id]) catch null; }; core.reportFailure(pane, failed); } @@ -2751,18 +2754,18 @@ const Shell = struct { var check_files = false; for (msgs.slice()) |m| switch (m) { .output => |o| { - if (s.gens[o.pane] == o.gen) + if (s.gens[o.pane.index()] == o.gen) s.core.update(.{ .output = .{ .pane = o.pane, .bytes = o.bytes } }); s.gpa.free(o.bytes); }, .eof => |e| { - if (s.gens[e.pane] == e.gen) eof: { + if (s.gens[e.pane.index()] == e.gen) eof: { if (e.failure) |err| { - s.closePty(e.pane); - s.core.readerFailed(.at(e.pane), err); + s.closePty(e.pane.index()); + s.core.readerFailed(e.pane, err); break :eof; } - const pt = if (s.ptys[e.pane]) |*pt| pt else { + const pt = if (s.ptys[e.pane.index()]) |*pt| pt else { s.core.update(.{ .eof = .{ .pane = e.pane } }); break :eof; }; @@ -2770,15 +2773,15 @@ const Shell = struct { // closing it would hang up one that runs on without it. // Its exit, if it came first, is told now its output is in. if (pt.cmd.watched) { - host_io.commandEof(s.core, s.ptys, e.pane, s, closeWatched); + host_io.commandEof(s.core, s.ptys, e.pane.index(), s, closeWatched); break :eof; } // Unwatched, a command's exit is read here, as its end, // and a shell's, for a run waiting on it and the log. - const unwatched = if (s.core.panes[e.pane]) |pane| pane.command != null else false; + const unwatched = if (s.core.panes[e.pane.index()]) |pane| pane.command != null else false; const status = host_io.exitStatus(pt.pid, 100); if (status != null) pt.pid = 0; // reaped: no shell to retire - s.closePty(e.pane); + s.closePty(e.pane.index()); if (unwatched or status != null) s.core.update(.{ .exited = .{ .pane = e.pane, .status = status } }); s.core.update(.{ .eof = .{ .pane = e.pane } }); } @@ -3327,32 +3330,33 @@ fn guiNow(ctx: ?*anyopaque) u64 { return s.test_clock orelse s.paced_ns orelse c.SDL_GetTicksNS(); } -fn spawnPane(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { +fn spawnPane(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { + const pane = id.index(); const s = shellOf(ctx); - if (s.adoptions.take(pane)) |a| return takeShell(s, pane, a); + if (s.adoptions.take(id)) |a| return takeShell(s, pane, a); // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = s.ptys[pane] != null; const child = host_io.forkShell(s.core, pane, s.prompt_rcs, s.core.shellBin(), cwd, s.core.view().screen_h, s.core.view().screen_w) catch |err| { - if (running) return s.core.restartFailed(.at(pane), err); - return s.core.shellFailed(.at(pane), err); + if (running) return s.core.restartFailed(id, err); + return s.core.shellFailed(id, err); }; s.closePty(pane); if (s.ptys[pane] != null) { _ = libc.close(child.file.handle); host_io.retireShell(child.pid); - return s.core.reportError(.at(pane), "shell", error.WorkersBusy); + return s.core.reportError(id, "shell", error.WorkersBusy); } s.gens[pane] +%= 1; const command = s.core.panes[pane].?.command != null; const pt: Pty = .{ .fd = child.file.handle, .pid = child.pid, .serial = s.core.panes[pane].?.serial, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; s.ptys[pane] = pt; var lbuf: [pardes.memory.limits.host_path_cap + 1]u8 = undefined; - if (host_io.shellCwd(pt.pid, &lbuf)) |wd| s.core.setCwd(.at(pane), wd); + if (host_io.shellCwd(pt.pid, &lbuf)) |wd| s.core.setCwd(id, wd); if (!s.threads_ok) return; - s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, pane, s.gens[pane]) catch |err| { + s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = pt.fd, .flags = .{ .nonblocking = false } }, id, s.gens[pane]) catch |err| { s.closePty(pane); - return s.core.reportError(.at(pane), "terminal reader", err); + return s.core.reportError(id, "terminal reader", err); }; } @@ -3368,9 +3372,9 @@ fn takeShell(s: *Shell, pane: u8, a: host_io.Adoption) void { if (host_io.shellCwd(a.pid, &lbuf)) |wd| s.core.setCwd(.at(pane), wd); const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (s.core.panes[pane]) |pn| ptyResize(s, pane, @max(pn.cols, 1), @max(pn.rows, 1)); + if (s.core.panes[pane]) |pn| ptyResize(s, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); if (!s.threads_ok) return; - s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = a.fd, .flags = .{ .nonblocking = false } }, pane, s.gens[pane]) catch |err| { + s.ptys[pane].?.reader = s.queue.startReader(.{ .handle = a.fd, .flags = .{ .nonblocking = false } }, .at(pane), s.gens[pane]) catch |err| { s.closePty(pane); return s.core.reportError(.at(pane), "terminal reader", err); }; @@ -3397,12 +3401,14 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void { } } -fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { +fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { + const pane = id.index(); const s = shellOf(ctx); if (s.ptys[pane]) |pt| _ = host_io.writeFd(pt.fd, bytes); } -fn ptyResize(ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void { +fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { + const pane = id.index(); const s = shellOf(ctx); if (s.ptys[pane]) |pt| { const ws = host_io.ptyWinsize(s.core, cols, rows); @@ -3410,31 +3416,36 @@ fn ptyResize(ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void { } } -fn ptySignal(ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void { +fn ptySignal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { + const pane = id.index(); const s = shellOf(ctx); // A command whose exit is recorded is reaped: its group may be another's. if (s.ptys[pane]) |pt| if (!pt.cmd.exited) host_io.signalTty(pt.pid, pt.fd, sig, pt.cmd.watched); } -fn ttyTaken(ctx: ?*anyopaque, pane: u8) bool { +fn ttyTaken(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + const pane = id.index(); const s = shellOf(ctx); const pt = s.ptys[pane] orelse return false; return host_io.ttyTaken(pt.pid, pt.fd); } -fn fgName(ctx: ?*anyopaque, pane: u8, buf: []u8) ?[]const u8 { +fn fgName(ctx: ?*anyopaque, id: pardes.Pane.Id, buf: []u8) ?[]const u8 { + const pane = id.index(); const s = shellOf(ctx); const pt = s.ptys[pane] orelse return null; return host_io.foregroundName(pt.pid, pt.fd, buf); } -fn killJob(ctx: ?*anyopaque, pane: u8) bool { +fn killJob(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + const pane = id.index(); const s = shellOf(ctx); const pt = s.ptys[pane] orelse return false; return host_io.killJob(pt.pid, pt.fd); } -fn writeFile(ctx: ?*anyopaque, pane: u8, path: []const u8, bytes: []const u8) void { +fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { + const pane = id.index(); const s = shellOf(ctx); host_io.writeFile(s.core, s.watches, pane, path, bytes); } @@ -3443,7 +3454,8 @@ fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void { host_io.writeDump(shellOf(ctx).core, bytes); } -fn watchFile(ctx: ?*anyopaque, pane: u8, _: []const u8, on: bool, mode: pardes.WatchMode) void { +fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void { + const pane = id.index(); const s = shellOf(ctx); if (file_watch.applyEffect(s.core, s.io, s.inotify_fd, s.watches, pane, on, mode)) s.queue.push(.files_changed); @@ -3461,7 +3473,8 @@ fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void { )) s.queue.push(.files_changed); } -fn dumpThemes(ctx: ?*anyopaque, pane: u8) void { +fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const s = shellOf(ctx); host_io.dumpThemes(s.core, s.io, s.gpa, pane); } @@ -3501,7 +3514,7 @@ fn lsp(ctx: ?*anyopaque, req: host_io.Lsp.Request) void { const s = shellOf(ctx); if (s.threads_ok) return s.queue.startLsp(s.core, req); s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - s.core.reportError(.at(req.pane), "lsp", error.WorkersUnavailable); + s.core.reportError(req.pane, "lsp", error.WorkersUnavailable); } fn pipe(ctx: ?*anyopaque, id: u32) void { @@ -3517,7 +3530,8 @@ fn kindDraw(ctx: ?*anyopaque, j: *host_io.KindJob) bool { return (s.threads_ok or s.client != null) and host_io.KindJob.go(s.io, &s.job_tasks, j, s.queue, Queue.pushJob); } -fn kindJob(ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void { +fn kindJob(ctx: ?*anyopaque, id: pardes.Pane.Id, serial: u32, token: u32) void { + const pane = id.index(); const s = shellOf(ctx); if (s.threads_ok) return host_io.KindJob.start(s.io, s.core, &s.job_tasks, pane, serial, token, s.queue, Queue.pushJob); host_io.KindJob.runHere(s.core, pane, serial, token); @@ -6280,7 +6294,7 @@ test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { const rcs: host_io.Shell.PromptFiles = .{}; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, 0, 1); + ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); try std.testing.expect(host_io.writeFd(child.file.handle, "printf '\\120\\101\\122\\104\\105\\123\\055\\124\\101\\111\\114'; exit\n")); var bytes: std.ArrayList(u8) = .empty; defer bytes.deinit(gpa); @@ -6317,8 +6331,8 @@ test "GUI PTY Restore joins a real reader waiting for queue space" { try dump.dumpState(core); var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; defer queue.deinit(); - queue.acceptReader(1, 1); - for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = 1, .gen = 1, .bytes = try gpa.dupe(u8, "other pane") } }); + queue.acceptReader(.at(1), 1); + for (0..output_capacity) |_| queue.push(.{ .output = .{ .pane = .at(1), .gen = 1, .bytes = try gpa.dupe(u8, "other pane") } }); var ptys: [pardes.MAX_PANES]?Pty = @splat(null); var gens: [pardes.MAX_PANES]u32 = @splat(1); var shell: Shell = undefined; @@ -6331,7 +6345,7 @@ test "GUI PTY Restore joins a real reader waiting for queue space" { const rcs: host_io.Shell.PromptFiles = .{}; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, 0, 1); + ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); try std.testing.expect(host_io.writeFd(child.file.handle, "printf 'after-full'; exit\n")); try host_io.PtyTests.waitBlocked(&queue); const old_serial = core.panes[0].?.serial; @@ -6345,10 +6359,10 @@ test "GUI PTY Restore joins a real reader waiting for queue space" { try std.testing.expectEqual(@as(usize, 0), queue.waiting); try std.testing.expect(queue.readers[0] == null); try std.testing.expectEqual(@as(c_int, -1), libc.fcntl(child.file.handle, libc.F.GETFD)); - queue.cancelReader(1, 1); - queue.acceptReader(0, gens[0]); - queue.push(.{ .eof = .{ .pane = 0, .gen = 1 } }); - try std.testing.expect(queue.pushOutput(0, gens[0], try gpa.dupe(u8, "fresh"))); + queue.cancelReader(.at(1), 1); + queue.acceptReader(.at(0), gens[0]); + queue.push(.{ .eof = .{ .pane = .at(0), .gen = 1 } }); + try std.testing.expect(queue.pushOutput(.at(0), gens[0], try gpa.dupe(u8, "fresh"))); var batch = queue.take(); defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); try std.testing.expectEqual(@as(usize, 1), batch.len); @@ -6373,7 +6387,7 @@ test "GUI PTY deletion joins an idle reader and its shell is reaped by exit" { const rcs: host_io.Shell.PromptFiles = .{}; const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); ptys[0] = .{ .fd = child.file.handle, .pid = child.pid, .serial = core.panes[0].?.serial }; - ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, 0, 1); + ptys[0].?.reader = try queue.startReader(.{ .handle = ptys[0].?.fd, .flags = .{ .nonblocking = false } }, .at(0), 1); try core.removePane(0, null); shell.reconcilePtys(); try std.testing.expect(queue.readers[0] == null); @@ -6414,9 +6428,9 @@ test "GUI completion failure survives backlog and leaves the document unchanged" const id = core.lsp_ask.wait.?.id; var queue: Queue = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .lsp_id = id }; defer queue.deinit(); - queue.acceptReader(0, 1); + queue.acceptReader(.at(0), 1); for (0..output_capacity) |_| queue.push(.{ .output = .{ - .pane = 0, + .pane = .at(0), .gen = 1, .bytes = try gpa.dupe(u8, "output"), } }); @@ -6459,7 +6473,7 @@ test "GUI worker setup failures finish matching LSP and pipe requests" { const req: host_io.Lsp.Request = .{ .id = core.lsp_ask.wait.?.id, .kind = .status, - .pane = @intCast(core.view().active), + .pane = .at(core.view().active), .offset = 0, .arg = "", }; @@ -6560,7 +6574,7 @@ test "the window's function and editing keys reach a terminal as its cursor keys .{ .modes = "", .sym = c.SDLK_UP, .sent = "\x1b[1;1:1A" }, }; for (cases) |case| { - core.update(.{ .output = .{ .pane = 0, .bytes = case.modes } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = case.modes } }); while (core.nextEffect()) |_| {} keyDown(&in, case.sym, case.mod); written.clearRetainingCapacity(); @@ -6678,7 +6692,7 @@ test "the clipboard pasted into a terminal reaches its pty as xterm pastes: brac fn f(core_: *pardes.Pardes, out: *std.ArrayList(u8)) ![]const u8 { out.clearRetainingCapacity(); while (core_.nextEffect()) |effect| { - if (effect == .write and effect.write.pane == 0) try out.appendSlice(std.testing.allocator, effect.write.bytes.slice()); + if (effect == .write and effect.write.pane.index() == 0) try out.appendSlice(std.testing.allocator, effect.write.bytes.slice()); } return out.items; } @@ -6692,7 +6706,7 @@ test "the clipboard pasted into a terminal reaches its pty as xterm pastes: brac putClipboard(gpa, "ls\x1b[201~; rm -rf ~\nx"); readClipboard(&shell); try std.testing.expectEqualStrings("ls [201~; rm -rf ~\rx", try take(core, &sent)); - core.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?2004h" } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?2004h" } }); _ = try take(core, &sent); readClipboard(&shell); try std.testing.expectEqualStrings("\x1b[200~ls [201~; rm -rf ~\nx\x1b[201~", try take(core, &sent)); @@ -6774,7 +6788,7 @@ test "a click over codex in a terminal reaches its pty with its modifiers; Shift fn f(core_: *pardes.Pardes, out: *std.ArrayList(u8)) ![]const u8 { out.clearRetainingCapacity(); while (core_.nextEffect()) |effect| { - if (effect == .write and effect.write.pane == 0) try out.appendSlice(std.testing.allocator, effect.write.bytes.slice()); + if (effect == .write and effect.write.pane.index() == 0) try out.appendSlice(std.testing.allocator, effect.write.bytes.slice()); } return out.items; } @@ -6784,7 +6798,7 @@ test "a click over codex in a terminal reaches its pty with its modifiers; Shift const r = core.view().rects[0]; const mc: MouseCell = .{ .col = r.x + config.GUTTER + 5, .row = core.bodyTop(pane, r) + 3 }; const next: MouseCell = .{ .col = mc.col + 1, .row = mc.row }; - core.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1049h\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1006h" } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1049h\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1006h" } }); _ = try take(core, &sent); in.post(.{ .mouse = pointerMouse(.none, .motion, mc, .{}) }); try std.testing.expectEqualStrings("\x1b[<35;6;4M", try take(core, &sent)); @@ -6824,7 +6838,7 @@ test "a wheel over codex in a terminal reaches its pty as the mouse reports it a fn take(core_: *pardes.Pardes, out: *std.ArrayList(u8)) ![]const u8 { out.clearRetainingCapacity(); while (core_.nextEffect()) |effect| { - if (effect == .write and effect.write.pane == 0) try out.appendSlice(std.testing.allocator, effect.write.bytes.slice()); + if (effect == .write and effect.write.pane.index() == 0) try out.appendSlice(std.testing.allocator, effect.write.bytes.slice()); } return out.items; } @@ -6834,7 +6848,7 @@ test "a wheel over codex in a terminal reaches its pty as the mouse reports it a const r = core.view().rects[0]; const mc: MouseCell = .{ .col = r.x + config.GUTTER + 5, .row = core.bodyTop(pane, r) + 3 }; // What codex 0.159's chat sets on start. - core.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?2004h\x1b[>7u\x1b[?1004h\x1b[?1049h\x1b[?1007l\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1006h" } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?2004h\x1b[>7u\x1b[?1004h\x1b[?1049h\x1b[?1007l\x1b[?1000h\x1b[?1002h\x1b[?1003h\x1b[?1006h" } }); pardes.draw.facts(core, &g.facts); _ = try Sent.take(core, &sent); // A notch up, then a fast turn of three down: each its report, none @@ -6860,14 +6874,14 @@ test "a wheel over codex in a terminal reaches its pty as the mouse reports it a try std.testing.expectEqualStrings("", try Sent.take(core, &sent)); resetScroll(&g); // Its sign-in screen: mouse off, alternate scroll on, so arrow keys. - core.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1006l\x1b[?1003l\x1b[?1002l\x1b[?1000l\x1b[?1007h" } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1006l\x1b[?1003l\x1b[?1002l\x1b[?1000l\x1b[?1007h" } }); pardes.draw.facts(core, &g.facts); _ = try Sent.take(core, &sent); wheelAt(&g, &in, .{ .y = -1, .integer_y = -1 }, mc); try std.testing.expectEqualStrings("\x1b[B", try Sent.take(core, &sent)); // Back on the main screen a shell's wheel is ours: a notch to glide, // nothing to the pty. - core.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1049l" } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1049l" } }); pardes.draw.facts(core, &g.facts); _ = try Sent.take(core, &sent); wheelAt(&g, &in, .{ .y = 1, .integer_y = 1 }, mc); diff --git a/dev/host_io.zig b/dev/host_io.zig index 2ee7c47e..f86c3250 100644 --- a/dev/host_io.zig +++ b/dev/host_io.zig @@ -69,7 +69,7 @@ pub fn restoreCore(gpa: std.mem.Allocator, core: *pardes.Pardes, path: []const u /// A shell of a dead session the daemon handed this one (Adopt), and the /// terminal pane made for it once there is one. -pub const Adoption = struct { pid: libc.pid_t, fd: c_int, pane: ?u8 }; +pub const Adoption = struct { pid: libc.pid_t, fd: c_int, pane: ?pardes.Pane.Id }; /// The shells of dead sessions handed this one (Adopt), each waiting for /// the spawn of the terminal pane made for it (`take`). @@ -99,7 +99,7 @@ pub const Adoptions = struct { a.list[a.len] = .{ .pid = pid, .fd = fd, .pane = null }; a.len += 1; if (core.adoptShell()) |id| { - a.list[a.len - 1].pane = id.index(); + a.list[a.len - 1].pane = id; a.taken += 1; return; } @@ -112,7 +112,7 @@ pub const Adoptions = struct { } /// The adopted shell pane `pane`'s spawn takes, off the list. - pub fn take(a: *Adoptions, pane: u8) ?Adoption { + pub fn take(a: *Adoptions, pane: pardes.Pane.Id) ?Adoption { for (a.list[0..a.len], 0..) |it, i| if (it.pane == pane) { std.mem.copyForwards(Adoption, a.list[i .. a.len - 1], a.list[i + 1 .. a.len]); a.len -= 1; @@ -1105,7 +1105,7 @@ pub fn takeExits(core: *pardes.Pardes, ptys: anytype, ctx: anytype, comptime clo if (!pt.cmd.watched or !pt.cmd.due()) continue; pt.cmd.told = true; const eof = pt.cmd.eof; - core.update(.{ .exited = .{ .pane = @intCast(id), .status = pt.cmd.status } }); + core.update(.{ .exited = .{ .pane = .at(id), .status = pt.cmd.status } }); if (eof) close(ctx, id); } } @@ -1115,7 +1115,7 @@ pub fn takeExits(core: *pardes.Pardes, ptys: anytype, ctx: anytype, comptime clo pub fn commandEof(core: *pardes.Pardes, ptys: anytype, id: usize, ctx: anytype, comptime close: anytype) void { const pt = slotPty(&ptys[id]) orelse return; pt.cmd.eof = true; - core.update(.{ .eof = .{ .pane = @intCast(id) } }); + core.update(.{ .eof = .{ .pane = .at(id) } }); if (pt.cmd.told) return close(ctx, id); takeExits(core, ptys, ctx, close); } @@ -1549,7 +1549,7 @@ pub const PtyTests = struct { } fn tail(queue: *Inbox, bytes: []u8) void { - if (queue.pushOutput(0, 1, bytes)) queue.push(.{ .eof = .{ .pane = 0, .gen = 1 } }); + if (queue.pushOutput(.at(0), 1, bytes)) queue.push(.{ .eof = .{ .pane = .at(0), .gen = 1 } }); } }; @@ -2155,9 +2155,9 @@ test "completion survives a full output queue" { const gpa = std.testing.allocator; var queue: Inbox = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .lsp_id = 7 }; defer queue.deinit(); - queue.acceptReader(0, 1); + queue.acceptReader(.at(0), 1); for (0..Inbox.output_capacity) |_| queue.push(.{ .output = .{ - .pane = 0, + .pane = .at(0), .gen = 1, .bytes = try gpa.dupe(u8, "output"), } }); @@ -2193,15 +2193,15 @@ test "PTY backpressure retains byte order and tail before EOF" { const gpa = std.testing.allocator; var queue: Inbox = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; defer queue.deinit(); - queue.acceptReader(0, 1); - for (0..Inbox.output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); + queue.acceptReader(.at(0), 1); + for (0..Inbox.output_capacity) |_| queue.push(.{ .output = .{ .pane = .at(0), .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); const tail = try gpa.dupe(u8, "tail"); var thread: ?std.Thread = std.Thread.spawn(.{}, PtyTests.tail, .{ &queue, tail }) catch |err| { gpa.free(tail); return err; }; defer { - queue.cancelReader(0, 1); + queue.cancelReader(.at(0), 1); if (thread) |owned| owned.join(); } try PtyTests.waitBlocked(&queue); @@ -2227,24 +2227,24 @@ test "PTY backpressure cancellation and close release owned producers" { for ([_]bool{ false, true }) |close| { var queue: Inbox = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; defer queue.deinit(); - queue.acceptReader(0, 1); - for (0..Inbox.output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); + queue.acceptReader(.at(0), 1); + for (0..Inbox.output_capacity) |_| queue.push(.{ .output = .{ .pane = .at(0), .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); const tail = try gpa.dupe(u8, "cancelled"); const thread = std.Thread.spawn(.{}, PtyTests.tail, .{ &queue, tail }) catch |err| { gpa.free(tail); return err; }; defer { - queue.cancelReader(0, 1); + queue.cancelReader(.at(0), 1); thread.join(); } try PtyTests.waitBlocked(&queue); - if (close) queue.close() else queue.cancelReader(0, 1); + if (close) queue.close() else queue.cancelReader(.at(0), 1); if (!close) { - queue.acceptReader(0, 2); - queue.push(.{ .eof = .{ .pane = 0, .gen = 1 } }); - try std.testing.expect(queue.pushOutput(0, 2, try gpa.dupe(u8, "replacement"))); - queue.push(.{ .eof = .{ .pane = 0, .gen = 2 } }); + queue.acceptReader(.at(0), 2); + queue.push(.{ .eof = .{ .pane = .at(0), .gen = 1 } }); + try std.testing.expect(queue.pushOutput(.at(0), 2, try gpa.dupe(u8, "replacement"))); + queue.push(.{ .eof = .{ .pane = .at(0), .gen = 2 } }); var batch = queue.take(); defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); try std.testing.expectEqual(@as(usize, 2), batch.len); @@ -2258,14 +2258,14 @@ test "PTY EOF reserve cannot evict terminal output" { const gpa = std.testing.allocator; var queue: Inbox = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa }; defer queue.deinit(); - for (0..pardes.MAX_PANES) |pane| queue.acceptReader(@intCast(pane), 1); - for (0..Inbox.output_capacity) |_| queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); - for (0..pardes.MAX_PANES) |pane| queue.push(.{ .eof = .{ .pane = @intCast(pane), .gen = 1 } }); + for (0..pardes.MAX_PANES) |pane| queue.acceptReader(.at(pane), 1); + for (0..Inbox.output_capacity) |_| queue.push(.{ .output = .{ .pane = .at(0), .gen = 1, .bytes = try gpa.dupe(u8, "before") } }); + for (0..pardes.MAX_PANES) |pane| queue.push(.{ .eof = .{ .pane = .at(pane), .gen = 1 } }); var batch = queue.take(); defer for (batch.slice()) |msg| msg.deinit(gpa, gpa); try std.testing.expectEqual(Inbox.capacity, batch.len); for (batch.items[0..Inbox.output_capacity]) |msg| try std.testing.expectEqualStrings("before", msg.output.bytes); - for (batch.items[Inbox.output_capacity..batch.len], 0..) |msg, pane| try std.testing.expectEqual(pane, msg.eof.pane); + for (batch.items[Inbox.output_capacity..batch.len], 0..) |msg, pane| try std.testing.expectEqual(pane, msg.eof.pane.index()); } test "completions retain their arrival order across LSP and pipes" { @@ -2288,8 +2288,8 @@ test "completion reset frees queued payloads and rejects late LSP workers" { const gpa = std.testing.allocator; var queue: Inbox = .{ .io = std.testing.io, .gpa = gpa, .lsp_allocator = gpa, .lsp_id = 9 }; defer queue.deinit(); - queue.acceptReader(0, 1); - queue.push(.{ .output = .{ .pane = 0, .gen = 1, .bytes = try gpa.dupe(u8, "retained") } }); + queue.acceptReader(.at(0), 1); + queue.push(.{ .output = .{ .pane = .at(0), .gen = 1, .bytes = try gpa.dupe(u8, "retained") } }); queue.push(.{ .lsp = .{ .id = 9, .rows = try gpa.dupe(u8, "current") } }); queue.push(.{ .lsp = .{ .id = 8, .rows = try gpa.dupe(u8, "late old request") } }); try std.testing.expectEqualStrings("current", queue.completions[0].lsp.rows.?); @@ -2343,7 +2343,7 @@ test "LSP query answers, and a newer one cancels it" { defer queue.deinit(); for (0..2) |_| { core.lspRequest(core.view().active, .status, ""); - queue.startLsp(core, .{ .id = core.lsp_ask.wait.?.id, .kind = .status, .pane = @intCast(core.view().active), .offset = 0, .arg = "" }); + queue.startLsp(core, .{ .id = core.lsp_ask.wait.?.id, .kind = .status, .pane = .at(core.view().active), .offset = 0, .arg = "" }); } const last = core.lsp_ask.wait.?.id; queue.lsp_task.?.future.await(io); diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 48b5d3d1..1f6b9a49 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -1099,7 +1099,7 @@ const Own = struct { } sh.pipe_tasks.cancelAll(io); for (&sh.gens) |*generation| generation.* +%= 1; - for (0..pardes.MAX_PANES) |pane| Shell.closePty(sh, @intCast(pane)); + for (0..pardes.MAX_PANES) |pane| Shell.closePty(sh, .at(pane)); // One view, the terminal's (host_io.restoreCore). const live = o.screen; stopInput(io, &live.thread); @@ -1386,7 +1386,8 @@ const Shell = struct { if (stub >= 0) daemon.tellStub(stub, "r{s}", .{why}); } - fn adopt(ctx: ?*anyopaque, pane: u8) void { + fn adopt(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const s = of(ctx); s.adoptions.ask(s.core, pane); } @@ -1397,7 +1398,8 @@ const Shell = struct { return of(ctx).client == null and upgrade.newer(); } - fn upgradeAsked(ctx: ?*anyopaque, pane: u8) void { + fn upgradeAsked(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const s = of(ctx); if (comptime !upgrade.supported) return s.core.reportFailure(pane, "Upgrade: this system cannot move a session to another build in place"); s.upgrade_asked = pane; @@ -1748,7 +1750,7 @@ const Shell = struct { s.output_seen = true; if (pr.id == core.view().active) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); if (s.gens[pr.id] == pr.gen) - core.update(.{ .output = .{ .pane = @intCast(pr.id), .bytes = pr.bytes } }); + core.update(.{ .output = .{ .pane = .at(pr.id), .bytes = pr.bytes } }); s.gpa.free(pr.bytes); // An exit waiting behind this output may be told now. s.takeExits(); @@ -1771,9 +1773,9 @@ const Shell = struct { const status = host_io.exitStatus(pt.pid, 100); if (status == null) host_io.retireShell(pt.pid); s.ptys[e.id] = null; - if (command or status != null) core.update(.{ .exited = .{ .pane = @intCast(e.id), .status = status } }); + if (command or status != null) core.update(.{ .exited = .{ .pane = .at(e.id), .status = status } }); } - core.update(.{ .eof = .{ .pane = @intCast(e.id) } }); + core.update(.{ .eof = .{ .pane = .at(e.id) } }); }, .key_press => |key| if (s.in_paste) { const bytes = vaxis_input.pasteBytes(key); @@ -2028,17 +2030,18 @@ const Shell = struct { tracy.frameMark(); } - fn spawn(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { + fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { + const pane = id.index(); const s = of(ctx); - if (s.adoptions.take(pane)) |a| return s.takeShell(pane, a); + if (s.adoptions.take(id)) |a| return s.takeShell(pane, a); // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = s.ptys[pane] != null; const child = host_io.forkShell(s.core, pane, s.prompt_rcs, s.core.shellBin(), cwd, s.core.view().screen_h, s.core.view().screen_w) catch |err| { - if (running) return s.core.restartFailed(.at(pane), err); - return s.core.shellFailed(.at(pane), err); + if (running) return s.core.restartFailed(id, err); + return s.core.shellFailed(id, err); }; - closePty(ctx, pane); // a shell still in the slot goes, reaped + closePty(ctx, id); // a shell still in the slot goes, reaped s.gens[pane] +%= 1; const command = if (s.core.panes[pane]) |pn| pn.command != null else false; s.ptys[pane] = .{ .file = child.file, .pid = child.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{ .watched = command and host_io.watchExit(child.pid), .fd = child.file.handle } }; @@ -2046,7 +2049,7 @@ const Shell = struct { // Only a shell given no directory is asked where it is: one given a // directory is named by it already (newShell), and asked now, before // its chdir, it would answer this process's, a rename to undo. - if (cwd.len == 0) if (host_io.shellCwd(child.pid, &lbuf)) |wd| s.core.setCwd(.at(pane), wd); + if (cwd.len == 0) if (host_io.shellCwd(child.pid, &lbuf)) |wd| s.core.setCwd(id, wd); if (s.threads_ok) { if (s.ptys[pane]) |*pt| { pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, @as(usize, pane), s.gens[pane], s.loop }) catch pt.reader; @@ -2057,7 +2060,7 @@ const Shell = struct { /// An adopted shell, running already, as pane `pane`'s: told the size /// twice (a size it never had, then the pane's), which makes it redraw. fn takeShell(s: *Shell, pane: u8, a: host_io.Adoption) void { - closePty(s, pane); + closePty(s, .at(pane)); s.gens[pane] +%= 1; const file: std.Io.File = .{ .handle = a.fd, .flags = .{ .nonblocking = false } }; s.ptys[pane] = .{ .file = file, .pid = a.pid, .reader = .{ .any_future = null, .result = {} }, .cmd = .{} }; @@ -2067,18 +2070,20 @@ const Shell = struct { if (host_io.shellCwd(a.pid, &lbuf)) |wd| s.core.setCwd(.at(pane), wd); const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (s.core.panes[pane]) |pn| ptyResize(s, pane, @max(pn.cols, 1), @max(pn.rows, 1)); + if (s.core.panes[pane]) |pn| ptyResize(s, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); if (s.threads_ok) if (s.ptys[pane]) |*pt| { pt.reader = s.io.concurrent(readPty, .{ s.io, s.gpa, pt.file, @as(usize, pane), s.gens[pane], s.loop }) catch pt.reader; }; } - fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { + fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { + const pane = id.index(); const s = of(ctx); if (s.ptys[pane]) |pt| _ = host_io.writeFd(pt.file.handle, bytes); } - fn ptyResize(ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void { + fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { + const pane = id.index(); const s = of(ctx); if (s.ptys[pane]) |pt| { const ws = host_io.ptyWinsize(s.core, cols, rows); @@ -2086,7 +2091,8 @@ const Shell = struct { } } - fn ptySignal(ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void { + fn ptySignal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { + const pane = id.index(); const s = of(ctx); // A command whose exit is recorded is reaped: its group may be // another's by now. @@ -2096,7 +2102,8 @@ const Shell = struct { /// The pane is gone: hang its pty up, which the kernel passes on to the /// shell as SIGHUP, and retire the shell. Output still in flight /// carries the old generation and is dropped. - fn closePty(ctx: ?*anyopaque, pane: u8) void { + fn closePty(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const s = of(ctx); var pt = s.ptys[pane] orelse return; s.ptys[pane] = null; @@ -2107,13 +2114,15 @@ const Shell = struct { host_io.retireShell(pt.pid); } - fn ttyTaken(ctx: ?*anyopaque, pane: u8) bool { + fn ttyTaken(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + const pane = id.index(); const s = of(ctx); const pt = s.ptys[pane] orelse return false; return host_io.ttyTaken(pt.pid, pt.file.handle); } - fn fgName(ctx: ?*anyopaque, pane: u8, buf: []u8) ?[]const u8 { + fn fgName(ctx: ?*anyopaque, id: pardes.Pane.Id, buf: []u8) ?[]const u8 { + const pane = id.index(); const s = of(ctx); const pt = s.ptys[pane] orelse return null; return host_io.foregroundName(pt.pid, pt.file.handle, buf); @@ -2130,13 +2139,15 @@ const Shell = struct { s.ptys[id] = null; } - fn killJob(ctx: ?*anyopaque, pane: u8) bool { + fn killJob(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + const pane = id.index(); const s = of(ctx); const pt = s.ptys[pane] orelse return false; return host_io.killJob(pt.pid, pt.file.handle); } - fn writeFile(ctx: ?*anyopaque, pane: u8, path: []const u8, bytes: []const u8) void { + fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { + const pane = id.index(); const s = of(ctx); host_io.writeFile(s.core, &s.watches, pane, path, bytes); } @@ -2145,7 +2156,8 @@ const Shell = struct { host_io.writeDump(of(ctx).core, bytes); } - fn watchFile(ctx: ?*anyopaque, pane: u8, _: []const u8, on: bool, mode: pardes.WatchMode) void { + fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, on: bool, mode: pardes.WatchMode) void { + const pane = id.index(); const s = of(ctx); if (file_watch.applyEffect(s.core, s.io, s.inotify_fd, &s.watches, pane, on, mode)) s.loop.postEvent(.files_changed) catch {}; @@ -2157,7 +2169,8 @@ const Shell = struct { s.loop.postEvent(.files_changed) catch {}; } - fn dumpThemes(ctx: ?*anyopaque, pane: u8) void { + fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const s = of(ctx); host_io.dumpThemes(s.core, s.io, s.gpa, pane); } @@ -2185,7 +2198,7 @@ const Shell = struct { const s = of(ctx); if (!s.threads_ok) { s.core.update(.{ .lsp_resp = .{ .id = req.id, .rows = null } }); - return s.core.reportError(.at(req.pane), "lsp", error.WorkersUnavailable); + return s.core.reportError(req.pane, "lsp", error.WorkersUnavailable); } host_io.Lsp.start(s.io, s.lsp_gpa, s.core, &s.lsp_task, req, s, deliverLspRows); } @@ -2220,7 +2233,8 @@ const Shell = struct { }.deliver); } - fn job(ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void { + fn job(ctx: ?*anyopaque, id: pardes.Pane.Id, serial: u32, token: u32) void { + const pane = id.index(); const s = of(ctx); if (!s.threads_ok) return host_io.KindJob.runHere(s.core, pane, serial, token); host_io.KindJob.start(s.io, s.core, &s.job_tasks, pane, serial, token, s, struct { @@ -2960,7 +2974,7 @@ test "on a terminal with no kitty graphics a program's 64x64 image over 8x4 cell for (0..64 * 64) |i| px[i * 3 ..][0..3].* = .{ 13, 77, 231 }; const enc = try a.alloc(u8, std.base64.standard.Encoder.calcSize(px.len)); _ = std.base64.standard.Encoder.encode(enc, px); - core.update(.{ .output = .{ .pane = 0, .bytes = try std.fmt.allocPrint(a, "\x1b[H\x1b[2J\r\nANCHOR>\x1b_Ga=T,i=42,f=24,s=64,v=64,c=8,r=4;{s}\x1b\\\r\n", .{enc}) } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = try std.fmt.allocPrint(a, "\x1b[H\x1b[2J\r\nANCHOR>\x1b_Ga=T,i=42,f=24,s=64,v=64,c=8,r=4;{s}\x1b\\\r\n", .{enc}) } }); while (core.nextEffect()) |_| {} const s = try core.render(a); try std.testing.expectEqual(@as(usize, 4), s.placements.items.len); @@ -3056,7 +3070,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { const req: host_io.Lsp.Request = .{ .id = core.lsp_ask.wait.?.id, .kind = .status, - .pane = @intCast(core.view().active), + .pane = .at(core.view().active), .offset = 0, .arg = "", }; @@ -3156,7 +3170,7 @@ test "the terminal's keys reach a program as its cursor keys' mode and keyboard .{ .modes = "\x1b[?1l\x1b[>7u", .in = "\x1b[99;5u\x1b[97;3u\x1b[A", .sent = "\x1b[99;5u\x1b[97;3u\x1b[1;1:1A" }, }; for (cases) |case| { - core.update(.{ .output = .{ .pane = 0, .bytes = case.modes } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = case.modes } }); while (core.nextEffect()) |_| {} var parser: vaxis.Parser = .{}; var bytes = case.in; diff --git a/runtime/macos.zig b/runtime/macos.zig index 3a00210d..408915da 100644 --- a/runtime/macos.zig +++ b/runtime/macos.zig @@ -781,31 +781,31 @@ fn drainInbox(st: *State) bool { defer msg.deinit(st.gpa, st.gpa); switch (msg) { .output => |o| { - if (st.gens[o.pane] != o.gen) continue; - st.cwd_stale[o.pane] = true; + if (st.gens[o.pane.index()] != o.gen) continue; + st.cwd_stale[o.pane.index()] = true; st.core.update(.{ .output = .{ .pane = o.pane, .bytes = o.bytes } }); }, .eof => |e| { - if (st.gens[e.pane] != e.gen) continue; - if (st.ptys[e.pane]) |*pt| { + if (st.gens[e.pane.index()] != e.gen) continue; + if (st.ptys[e.pane.index()]) |*pt| { // A command's pty stays open until its child has exited: // closing it would hang up one that runs on without it. // Its exit, if it came first, is told now its output is in. if (pt.cmd.watched) { - host_io.commandEof(st.core, &st.ptys, e.pane, st, closeWatched); + host_io.commandEof(st.core, &st.ptys, e.pane.index(), st, closeWatched); continue; } // Unwatched, a command's exit is read here, as its end, // and a shell's, for a run waiting on it and the log. - const command = if (st.core.panes[e.pane]) |pane| pane.command != null else false; + const command = if (st.core.panes[e.pane.index()]) |pane| pane.command != null else false; const status = host_io.exitStatus(pt.pid, 100); if (status != null) pt.pid = 0; // reaped: no shell to retire - reap(st, e.pane); + reap(st, e.pane.index()); if (command or status != null) st.core.update(.{ .exited = .{ .pane = e.pane, .status = status } }); st.core.update(.{ .eof = .{ .pane = e.pane } }); continue; } - reap(st, e.pane); + reap(st, e.pane.index()); st.core.update(.{ .eof = .{ .pane = e.pane } }); }, .lsp => |d| { @@ -1648,15 +1648,16 @@ fn hostState(ctx: ?*anyopaque) *State { return @ptrCast(@alignCast(ctx.?)); } -fn spawnShell(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { +fn spawnShell(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { + const pane = id.index(); const st = hostState(ctx); const core = st.core; // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = st.ptys[pane] != null; const child = host_io.forkShell(core, pane, &st.prompt_rcs, core.shellBin(), cwd, core.view().screen_h, core.view().screen_w) catch |err| { - if (running) return core.restartFailed(.at(pane), err); - return core.shellFailed(.at(pane), err); + if (running) return core.restartFailed(id, err); + return core.shellFailed(id, err); }; reap(st, pane); st.gens[pane] +%= 1; @@ -1673,54 +1674,61 @@ fn spawnShell(ctx: ?*anyopaque, pane: u8, cwd: []const u8) void { // Only a shell given no directory is asked where it is: one given a // directory is named by it already (newShell), and asked now, before // its chdir, it would answer this process's, a rename to undo. - if (cwd.len == 0) if (host_io.shellCwd(child.pid, &lbuf)) |wd| core.setCwd(.at(pane), wd); + if (cwd.len == 0) if (host_io.shellCwd(child.pid, &lbuf)) |wd| core.setCwd(id, wd); if (st.started) if (st.ptys[pane]) |*pt| startReader(st, pt, pane); } -fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { +fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { + const pane = id.index(); const st = hostState(ctx); if (st.ptys[pane]) |pt| _ = host_io.writeFd(pt.file.handle, bytes); } -fn ptyResize(ctx: ?*anyopaque, pane: u8, cols: u16, rows: u16) void { +fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { + const pane = id.index(); const st = hostState(ctx); const pt = st.ptys[pane] orelse return; const ws: posix.winsize = .{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; _ = posix.system.ioctl(pt.file.handle, TIOCSWINSZ, @intFromPtr(&ws)); } -fn ptySignal(ctx: ?*anyopaque, pane: u8, sig: pardes.PtySignal) void { +fn ptySignal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { + const pane = id.index(); const st = hostState(ctx); // A command whose exit is recorded is reaped: its group may be another's. if (st.ptys[pane]) |pt| if (!pt.cmd.exited) host_io.signalTty(pt.pid, pt.file.handle, sig, pt.cmd.watched); } -fn ttyTaken(ctx: ?*anyopaque, pane: u8) bool { +fn ttyTaken(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + const pane = id.index(); const st = hostState(ctx); const pt = st.ptys[pane] orelse return false; return host_io.ttyTaken(pt.pid, pt.file.handle); } -fn killJob(ctx: ?*anyopaque, pane: u8) bool { +fn killJob(ctx: ?*anyopaque, id: pardes.Pane.Id) bool { + const pane = id.index(); const st = hostState(ctx); const pt = st.ptys[pane] orelse return false; return host_io.killJob(pt.pid, pt.file.handle); } -fn writeFile(ctx: ?*anyopaque, pane: u8, path: []const u8, bytes: []const u8) void { +fn writeFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, bytes: []const u8) void { + const pane = id.index(); const st = hostState(ctx); filesystem.write(st.core, path, bytes) catch |err| - return st.core.saveFailed(.at(pane), path, err); + return st.core.saveFailed(id, path, err); st.file_watches.restampText(pane, path, std.hash.Wyhash.hash(0, bytes)); var mbuf: [256]u8 = undefined; - st.core.setMessage(.at(pane), message.stamp(&mbuf, "saved", path)); + st.core.setMessage(id, message.stamp(&mbuf, "saved", path)); } fn writeDump(ctx: ?*anyopaque, bytes: []const u8) void { host_io.writeDump(hostState(ctx).core, bytes); } -fn watchFile(ctx: ?*anyopaque, pane: u8, path: []const u8, on: bool, mode: pardes.WatchMode) void { +fn watchFile(ctx: ?*anyopaque, id: pardes.Pane.Id, path: []const u8, on: bool, mode: pardes.WatchMode) void { + const pane = id.index(); const st = hostState(ctx); if (!on or path.len == 0) { const generation = st.file_watches.stop(st.gpa, pane); @@ -1735,16 +1743,16 @@ fn watchTheme(ctx: ?*anyopaque, generation: u32, on: bool) void { setThemeFileWatch(st, generation, on); } -fn dumpThemes(ctx: ?*anyopaque, pane: u8) void { +fn dumpThemes(ctx: ?*anyopaque, id: pardes.Pane.Id) void { const st = hostState(ctx); const config_dir = st.core.opts.config_dir orelse return; const out_dir = pardes.config.User.dumpThemes(st.io, st.gpa, config_dir, pardes.themes) catch |err| { - st.core.reportError(.at(pane), "dump themes", err); + st.core.reportError(id, "dump themes", err); return; }; defer st.gpa.free(out_dir); var mbuf: [256]u8 = undefined; - st.core.setMessage(.at(pane), message.stamp(&mbuf, "dumped themes", out_dir)); + st.core.setMessage(id, message.stamp(&mbuf, "dumped themes", out_dir)); } fn setClipboard(ctx: ?*anyopaque, text: []const u8) void { @@ -1764,14 +1772,15 @@ fn openLink(_: ?*anyopaque, url: []const u8) void { } fn startReader(st: *State, pt: *Pty, id: u8) void { - pt.reader = st.inbox.startReader(pt.file, id, pt.gen) catch |err| { + pt.reader = st.inbox.startReader(pt.file, .at(id), pt.gen) catch |err| { log.err("pane {d} has no reader ({t}); it will not show output", .{ id, err }); return; }; } /// The pane is gone: its shell goes with it. -fn closePty(ctx: ?*anyopaque, pane: u8) void { +fn closePty(ctx: ?*anyopaque, id: pardes.Pane.Id) void { + const pane = id.index(); const st = hostState(ctx); if (st.ptys[pane] == null) return; reap(st, pane); @@ -1782,7 +1791,7 @@ fn reap(st: *State, pane: u8) void { var pt = st.ptys[pane] orelse return; st.ptys[pane] = null; // The inbox first: a reader waiting for space there wakes and returns. - st.inbox.cancelReader(pane, pt.gen); + st.inbox.cancelReader(.at(pane), pt.gen); pt.reader.cancel(st.io); _ = libc.close(pt.file.handle); host_io.retireShell(pt.pid); diff --git a/test/fs_namespace.zig b/test/fs_namespace.zig index ef5dec60..ceda8158 100644 --- a/test/fs_namespace.zig +++ b/test/fs_namespace.zig @@ -203,7 +203,7 @@ test "explicit OS file opens and restores keep their names and request watches" try std.testing.expectEqualStrings("const disk = 1;\n", pane.file.?.content); var watched_open = false; while (p.nextEffect()) |effect| if (effect == .watch and effect.watch.on) { - if (effect.watch.pane == opened) watched_open = true; + if (effect.watch.pane.index() == opened) watched_open = true; }; try std.testing.expect(watched_open); // Unsaved, dumped and loaded: its text kept, its file watched again. @@ -216,7 +216,7 @@ test "explicit OS file opens and restores keep their names and request watches" try std.testing.expectEqualStrings("// unsaved\nconst disk = 1;\n", copy.file.?.content); var watched_restore = false; while (restored.nextEffect()) |effect| if (effect == .watch and effect.watch.on) { - if (effect.watch.pane == opened) watched_restore = true; + if (effect.watch.pane.index() == opened) watched_restore = true; }; try std.testing.expect(watched_restore); } diff --git a/test/hxdiff.zig b/test/hxdiff.zig index 01169504..db858561 100644 --- a/test/hxdiff.zig +++ b/test/hxdiff.zig @@ -352,7 +352,7 @@ fn runCase(arena: std.mem.Allocator, case: Case, pane_kind: []const u8) !Result cooked[cooked_i] = ch; cooked_i += 1; } - core.update(.{ .output = .{ .pane = 0, .bytes = cooked } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = cooked } }); pump(core); // leave tty mode the way a live session does: Ctrl-<tty_toggle>, // then park the cursor at (0,0) — the helix harness's initial diff --git a/test/kitty.zig b/test/kitty.zig index 6db6e8fc..76d97421 100644 --- a/test/kitty.zig +++ b/test/kitty.zig @@ -90,13 +90,13 @@ const Rig = struct { fn drain(r: *Rig) void { while (r.p.nextEffect()) |effect| { - if (effect == .write) r.read[effect.write.pane].appendSlice(gpa, effect.write.bytes.slice()) catch @panic("OOM"); + if (effect == .write) r.read[effect.write.pane.index()].appendSlice(gpa, effect.write.bytes.slice()) catch @panic("OOM"); } } /// One pty read of pane `id`'s output. fn feed(r: *Rig, id: u8, bytes: []const u8) void { - r.p.update(.{ .output = .{ .pane = id, .bytes = bytes } }); + r.p.update(.{ .output = .{ .pane = .at(id), .bytes = bytes } }); r.drain(); } diff --git a/test/output.zig b/test/output.zig index a8a1f239..38e8ba0d 100644 --- a/test/output.zig +++ b/test/output.zig @@ -389,7 +389,7 @@ test "Mini asks for no watch on its pane" { try std.testing.expect(id != 0); try std.testing.expect(p.executeBuiltinLine(id, "Mini build.zig")); while (p.nextEffect()) |effect| switch (effect) { - .watch => |watch| try std.testing.expect(watch.pane != id), + .watch => |watch| try std.testing.expect(watch.pane.index() != id), else => {}, }; } @@ -414,7 +414,7 @@ test "Mini dump preserves its exact virtual snapshot and body writes detach colo try std.testing.expectEqualSlices(u8, before.mini.?.colors, mini.file.?.mini.?.colors); try std.testing.expect(before.content.ptr != mini.file.?.content.ptr); while (restored.nextEffect()) |effect| switch (effect) { - .watch => |watch| try std.testing.expect(watch.pane != restored.view().active), + .watch => |watch| try std.testing.expect(watch.pane.index() != restored.view().active), else => {}, }; var body_buf: [128]u8 = undefined; @@ -534,7 +534,7 @@ test "Config with no startup file opens a pane of its name, and Save makes its d try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(std.testing.io, "pardes", .{})); // Save's effect, as a host performs it. try std.testing.expect(p.executeBuiltinLine(opened, "Save")); - p.perform(.{ .save_file = .{ .pane = @intCast(opened) } }); + p.perform(.{ .save_file = .{ .pane = .at(opened) } }); const made = try tmp.dir.statFile(std.testing.io, "pardes", .{}); try std.testing.expectEqual(std.Io.File.Kind.directory, made.kind); } diff --git a/test/panes.zig b/test/panes.zig index 022d672c..dbdbec2f 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -453,7 +453,7 @@ const TtySelectionTests = struct { const pane = p.panes[0].?; pane.tty_filter = false; while (p.nextEffect()) |_| {} - p.update(.{ .output = .{ .pane = 0, .bytes = "alpha" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "alpha" } }); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); _ = try p.render(frame.allocator()); @@ -478,7 +478,7 @@ const TtySelectionTests = struct { pane.body.mode = .tty; pane.tty_filter = false; while (p.nextEffect()) |_| {} - p.update(.{ .output = .{ .pane = 0, .bytes = if (prompt) + p.update(.{ .output = .{ .pane = .at(0), .bytes = if (prompt) "\x1b]133;A\x07$ \x1b]133;B\x07abc" else "alpha\r\nbravo" } }); @@ -519,7 +519,7 @@ const TtySelectionTests = struct { try noChildInput(p); _ = try p.render(frame.allocator()); try std.testing.expectEqualStrings(expected, pardes.exec.heldSelection(p, 0).?); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;C\x07" ++ std.mem.asBytes(&@as([30][12]u8, @splat("\r\nnew output".*))) } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;C\x07" ++ std.mem.asBytes(&@as([30][12]u8, @splat("\r\nnew output".*))) } }); _ = try p.render(frame.allocator()); try std.testing.expectEqual(panes.Text.Mode.tty, pane.body.mode); try std.testing.expectEqualStrings(expected, pardes.exec.heldSelection(p, 0).?); @@ -542,10 +542,10 @@ const TtySelectionTests = struct { } p.update(.tick); while (p.nextEffect()) |_| {} - p.update(.{ .output = .{ .pane = 0, .bytes = "alpha\r\nbravo" } }); - p.update(.{ .output = .{ .pane = 1, .bytes = "target> " } }); - if (bracketed) p.update(.{ .output = .{ .pane = 1, .bytes = "\x1b[?2004h" } }); - if (reports_mouse) p.update(.{ .output = .{ .pane = 1, .bytes = "\x1b[?1000h\x1b[?1006h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "alpha\r\nbravo" } }); + p.update(.{ .output = .{ .pane = .at(1), .bytes = "target> " } }); + if (bracketed) p.update(.{ .output = .{ .pane = .at(1), .bytes = "\x1b[?2004h" } }); + if (reports_mouse) p.update(.{ .output = .{ .pane = .at(1), .bytes = "\x1b[?1000h\x1b[?1006h" } }); var frame = std.heap.ArenaAllocator.init(p.gpa); defer frame.deinit(); _ = try p.render(frame.allocator()); @@ -569,7 +569,7 @@ const TtySelectionTests = struct { defer written.deinit(); while (p.nextEffect()) |effect| { if (effect == .write) { - try std.testing.expectEqual(@as(u8, 1), effect.write.pane); + try std.testing.expectEqual(1, effect.write.pane.index()); try written.writer.writeAll(effect.write.bytes.slice()); } } @@ -613,7 +613,7 @@ const TtySelectionTests = struct { defer p.deinit(); const pane = p.panes[0].?; try std.testing.expectEqual(panes.Text.Mode.tty, pane.body.mode); - if (bracketed) p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?2004h" } }); + if (bracketed) p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?2004h" } }); try std.testing.expectEqual(bracketed, pardes.Term.modes(pane.terminal).bracketed_paste); try p.registers.put(p.gpa, '"', "one\ntwo", 0, 1, true); // what a `y` anywhere left behind var buf: [256]u8 = undefined; @@ -964,7 +964,7 @@ test "terminal overlay recoloring never materializes unrelated history" { while (p.nextEffect()) |_| {} const pane = p.panes[0].?; pane.tty_filter = false; - for (0..1000) |_| p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[32mcaf\xc3\xa9 \xe7\x95\x8c\x1b[0m\r\n" } }); + for (0..1000) |_| p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[32mcaf\xc3\xa9 \xe7\x95\x8c\x1b[0m\r\n" } }); pane.body.mode = .normal; pane.ovl = .{ .row = 0, .rows = 1, .text = try p.gpa.dupe(u8, "edited history") }; var frame = std.heap.ArenaAllocator.init(testing.allocator); @@ -1009,7 +1009,7 @@ test "terminal output keeps following new rows after scrollback eviction" { var buf: [64]u8 = undefined; const bytes = try std.fmt.bufPrint(&buf, "\x1b[32mrow-{d:0>5}\x1b[0m\r\n", .{n}); const previous = pages.scrollbar().total; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); if (pages.scrollbar().total < previous) evictions += 1; } try std.testing.expect(evictions > phase); @@ -1036,7 +1036,7 @@ test "terminal output keeps following new rows after scrollback eviction" { p.update(.{ .key = .{ .cp = 'f', .text = "f" } }); const effect = p.nextEffect() orelse return error.MissingTerminalInput; try std.testing.expectEqual(.write, std.meta.activeTag(effect)); - try std.testing.expectEqual(@as(u8, 0), effect.write.pane); + try std.testing.expectEqual(0, effect.write.pane.index()); try std.testing.expectEqualStrings("f", effect.write.bytes.slice()); try std.testing.expect(p.nextEffect() == null); } @@ -1055,7 +1055,7 @@ test "terminal edit stays on its surviving row when old scrollback is evicted" { while (next < 3000) { var buf: [32]u8 = undefined; const bytes = try std.fmt.bufPrint(&buf, "row-{d:0>5}\r\n", .{next}); - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); next += 1; if (pages.scrollbar().total > pages.pages.first.?.rows() + pane.rows + 10) break; } @@ -1073,7 +1073,7 @@ test "terminal edit stays on its surviving row when old scrollback is evicted" { var buf: [32]u8 = undefined; const bytes = try std.fmt.bufPrint(&buf, "row-{d:0>5}\r\n", .{n}); const previous = pages.scrollbar().total; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); if (pages.scrollbar().total < previous) { removed = previous + 1 - pages.scrollbar().total; break; @@ -1099,8 +1099,8 @@ test "terminal edits and undo survive partial history clearing and whole-screen const pane = p.panes[0].?; pane.body.mode = .tty; if (action == .reset_alternate) - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1049h" } }); - p.update(.{ .output = .{ .pane = 0, .bytes = std.mem.asBytes(&@as([40][9]u8, @splat("old row\r\n".*))) } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1049h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = std.mem.asBytes(&@as([40][9]u8, @splat("old row\r\n".*))) } }); const pages = &pardes.Term.emulatorOf(pane.terminal.?).vt.screens.active.pages; pages.limits.set(.bytes, 64 * 1024); const row: i32 = @as(i32, @intCast(pages.total_rows - pane.rows)) + @@ -1130,7 +1130,7 @@ test "terminal edits and undo survive partial history clearing and whole-screen .reset_primary, .reset_alternate => "\x1bc", .evict_all, .hidden_undo => std.mem.asBytes(&@as([4000][9]u8, @splat("new row\r\n".*))), }; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); const expected: i32 = if (action == .clear_history) 2 else 0; // The copy's top row was in the history ED 3 erased: the copy goes // with it (terminal.zig's rule for rows that are gone), and the @@ -1166,7 +1166,7 @@ test "terminal lower scroll regions do not move edits above them" { while (p.nextEffect()) |_| {} const pane = p.panes[0].?; pane.body.mode = .normal; - p.update(.{ .output = .{ .pane = 0, .bytes = "top\r\nsecond\r\nthird" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "top\r\nsecond\r\nthird" } }); pane.ovl = .{ .row = 1, .rows = 1, .text = try gpa.dupe(u8, "unsent") }; pane.body.cur_row = 1; pane.body.cur_pinned = true; @@ -1174,8 +1174,8 @@ test "terminal lower scroll regions do not move edits above them" { const total = pardes.Term.grid(pane.terminal).total; var setup: [64]u8 = undefined; const bytes = try std.fmt.bufPrint(&setup, "\x1b[4;{d}r\x1b[{d};1H", .{ pane.rows, pane.rows }); - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); - p.update(.{ .output = .{ .pane = 0, .bytes = std.mem.asBytes(&@as([50][6]u8, @splat("tail\r\n".*))) } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = std.mem.asBytes(&@as([50][6]u8, @splat("tail\r\n".*))) } }); try std.testing.expectEqual(total, pardes.Term.grid(pane.terminal).total); try std.testing.expectEqual(@as(i32, 1), pane.ovl.?.row); try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); @@ -1198,7 +1198,7 @@ test "terminal eviction and lower-region scrolling in one read preserve a surviv while (next < 3000) { var buf: [32]u8 = undefined; const bytes = try std.fmt.bufPrint(&buf, "row-{d:0>5}\r\n", .{next}); - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); next += 1; if (pages.pages.first != pages.pages.last and pages.pages.last.?.rows() == pages.pages.last.?.capacity().rows) break; @@ -1214,7 +1214,7 @@ test "terminal eviction and lower-region scrolling in one read preserve a surviv const old_total = pages.total_rows; var buf: [128]u8 = undefined; const bytes = try std.fmt.bufPrint(&buf, "new\r\n\x1b[4;{d}r\x1b[{d};1Hregion\r\n", .{ pane.rows, pane.rows }); - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); try std.testing.expectEqual(old_total + 1 - removed, pages.total_rows); try std.testing.expectEqual(@as(i32, 2), pane.ovl.?.row); try std.testing.expectEqual(@as(i32, 2), pane.body.cur_row); @@ -1412,12 +1412,12 @@ test "Save rearms renamed file watches only after a successful write" { watches: usize = 0, err: ?anyerror = null, - fn write(ctx: ?*anyopaque, id: u8, _: []const u8, _: []const u8) void { + fn write(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, _: []const u8) void { const h: *@This() = @ptrCast(@alignCast(ctx.?)); switch (h.result) { .success => {}, - .failure => h.core.saveFailed(.at(id), "save", error.PermissionDenied), - .edited => h.core.panes[id].?.file.?.revision +%= 1, + .failure => h.core.saveFailed(id, "save", error.PermissionDenied), + .edited => h.core.panes[id.index()].?.file.?.revision +%= 1, .replaced => { const replacement = h.core.setTestFile("replacement\n") catch |err| { h.err = err; @@ -1427,16 +1427,16 @@ test "Save rearms renamed file watches only after a successful write" { replacement.file.?.saved_revision -%= 1; }, .closed => { - pardes.panes.deinitPane(h.core, h.core.panes[id].?) catch |err| { + pardes.panes.deinitPane(h.core, h.core.panes[id.index()].?) catch |err| { h.err = err; return; }; - h.core.panes[id] = null; + h.core.panes[id.index()] = null; }, } } - fn watch(ctx: ?*anyopaque, _: u8, _: []const u8, on: bool, _: pardes.WatchMode) void { + fn watch(ctx: ?*anyopaque, _: pardes.Pane.Id, _: []const u8, on: bool, _: pardes.WatchMode) void { const h: *@This() = @ptrCast(@alignCast(ctx.?)); if (on) h.watches += 1; } @@ -1484,9 +1484,9 @@ test "Save rearms renamed file watches only after a successful write" { test "Save on a scratch does not watch a path the host could not create" { const Refusing = struct { - fn write(ctx: ?*anyopaque, id: u8, _: []const u8, _: []const u8) void { + fn write(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, _: []const u8) void { const p: *Pardes = @ptrCast(@alignCast(ctx.?)); - p.saveFailed(.at(id), "save", error.PermissionDenied); + p.saveFailed(id, "save", error.PermissionDenied); } }; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); @@ -1518,9 +1518,9 @@ test "Save on a scratch does not watch a path the host could not create" { test "a failed Save to another name leaves a clean named file clean" { const Refusing = struct { - fn write(ctx: ?*anyopaque, id: u8, _: []const u8, _: []const u8) void { + fn write(ctx: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, _: []const u8) void { const p: *Pardes = @ptrCast(@alignCast(ctx.?)); - p.saveFailed(.at(id), "save", error.AccessDenied); + p.saveFailed(id, "save", error.AccessDenied); } }; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); @@ -1544,14 +1544,14 @@ test "unplaced terminal panes keep their allocated grid until placement" { const id = p.freeSlot().?; const pane = try p.newShell(id, ""); try std.testing.expectEqual(pardes.Rect{}, p.view().rects[id]); - p.update(.{ .output = .{ .pane = @intCast(id), .bytes = "unplaced\r\n" } }); + p.update(.{ .output = .{ .pane = .at(id), .bytes = "unplaced\r\n" } }); try std.testing.expectEqual(pardes.Rect{}, p.view().rects[id]); try std.testing.expectEqual(@as(u16, 100), pane.cols); try std.testing.expectEqual(@as(u16, 30), pane.rows); try std.testing.expectEqual(pane.cols, pardes.Term.emulatorOf(pane.terminal.?).vt.cols); try std.testing.expectEqual(pane.rows, pardes.Term.emulatorOf(pane.terminal.?).vt.rows); while (p.nextEffect()) |effect| { - if (effect == .resize_pty and effect.resize_pty.pane == id) return error.ResizedUnplacedPane; + if (effect == .resize_pty and effect.resize_pty.pane.index() == id) return error.ResizedUnplacedPane; } pardes.layout.insert(p, 0, 1, id); p.update(.tick); @@ -1572,7 +1572,7 @@ test "document panes ignore terminal output without allocating terminal state" { const resizes = allocator.resize_index; allocator.fail_index = allocations; allocator.resize_fail_index = resizes; - p.update(.{ .output = .{ .pane = 0, .bytes = "unrelated PTY output\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "unrelated PTY output\r\n" } }); try std.testing.expect(!allocator.has_induced_failure); try std.testing.expectEqual(allocations, allocator.alloc_index); try std.testing.expectEqual(resizes, allocator.resize_index); @@ -2442,19 +2442,19 @@ const TerminalTests = struct { .write => return error.GreetedBeforeOutput, else => {}, }; - p.update(.{ .output = .{ .pane = 0, .bytes = "startup banner\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "startup banner\r\n" } }); while (p.nextEffect()) |effect| switch (effect) { .write => return error.GreetedBeforePrompt, else => {}, }; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;A;aid=pardes\x07prompt$ " } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;A;aid=pardes\x07prompt$ " } }); try std.testing.expect(!pardes.Term.prompt(pane.terminal).input_ready); while (p.nextEffect()) |effect| switch (effect) { .write => return error.GreetedDuringPrompt, else => {}, }; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;B;aid=pardes\x07" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;B;aid=pardes\x07" } }); try std.testing.expect(pardes.Term.prompt(pane.terminal).input_ready); var greeted = false; while (p.nextEffect()) |effect| switch (effect) { @@ -2477,9 +2477,9 @@ const TerminalTests = struct { const pane = p.panes[0].?; while (p.nextEffect()) |_| {} p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); - p.update(.{ .output = .{ .pane = 0, .bytes = output } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = output } }); // and a later, quiet prompt changes nothing - p.update(.{ .output = .{ .pane = 0, .bytes = "\r\n\x1b]133;A;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\r\n\x1b]133;A;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07" } }); while (p.nextEffect()) |effect| switch (effect) { .write => |write| if (std.mem.eql(u8, write.bytes.slice(), "ls\r")) return error.GreetedLate, else => {}, @@ -2505,13 +2505,13 @@ const TerminalTests = struct { .write => return error.CommandEscapedBeforePrompt, else => {}, }; - p.update(.{ .output = .{ .pane = 0, .bytes = "startup\r\n\x1b]133;A\x07prompt$ " } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "startup\r\n\x1b]133;A\x07prompt$ " } }); while (p.nextEffect()) |effect| switch (effect) { .write => return error.CommandEscapedDuringPrompt, else => {}, }; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;B\x07" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;B\x07" } }); var sent: [64]u8 = undefined; var sent_len: usize = 0; while (p.nextEffect()) |effect| switch (effect) { @@ -2536,7 +2536,7 @@ const TerminalTests = struct { p.acknowledgeShell(.at(0), "/bin/sh", false); try std.testing.expect(!pane.greet); try std.testing.expectEqual(.none, pane.pending_command.wait); - p.update(.{ .output = .{ .pane = 0, .bytes = "plain prompt$ " } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "plain prompt$ " } }); while (p.nextEffect()) |effect| switch (effect) { .write => return error.UnmarkedGreetingEscaped, else => {}, @@ -2569,7 +2569,7 @@ const TerminalTests = struct { const effect = p.nextEffect() orelse return error.MissingWriteEffect; switch (effect) { .write => |write| { - try std.testing.expectEqual(@as(u8, 0), write.pane); + try std.testing.expectEqual(0, write.pane.index()); try std.testing.expectEqualStrings(expected, write.bytes.slice()); }, else => return error.UnexpectedEffect, @@ -2603,7 +2603,7 @@ const TerminalTests = struct { pane.tty_filter = false; pane.body.mode = .normal; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -2658,7 +2658,7 @@ const TerminalTests = struct { // grapheme for that cell, not one per stored codepoint: stepping twice ate // the `A`, and left every colour a cell to the left of its glyph with the // last one stranded on a blank. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;A\x1b\\\x1b[32me\u{301}\x1b]133;B\x1b\\\x1b[31mA\x1b[34mB\x1b[35mC" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;A\x1b\\\x1b[32me\u{301}\x1b]133;B\x1b\\\x1b[31mA\x1b[34mB\x1b[35mC" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -2694,7 +2694,7 @@ const TerminalTests = struct { for (0..60) |i| { var buf: [32]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[4{d}mL{d:0>2}\x1b[0m\r\n", .{ (i % 6) + 1, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } p.shell_rows.stale = true; pardes.Term.scroll(pane.terminal.?, -20); @@ -2745,7 +2745,7 @@ const TerminalTests = struct { pane.tty_filter = false; pane.body.mode = .normal; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -2782,7 +2782,7 @@ const TerminalTests = struct { pane.tty_filter = false; pane.body.mode = .normal; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -2821,7 +2821,7 @@ const TerminalTests = struct { while (p.nextEffect()) |_| {} const pane = p.panes[0].?; pane.tty_filter = filter; - p.update(.{ .output = .{ .pane = 0, .bytes = payload } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = payload } }); const diffs = try modeStyleDiffs(p, pane, testing.allocator, if (filter) "session filter=on" else "session filter=off"); try testing.expectEqual(@as(usize, 0), diffs); } @@ -2864,7 +2864,7 @@ const TerminalTests = struct { "\x1b]133;A\x1b\\\x1b[32m{s}$ \x1b]133;B\x1b\\\x1b[31mab\x1b[34mcd\x1b[0m", .{prompt}, ); - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -2923,7 +2923,7 @@ const TerminalTests = struct { while (p.nextEffect()) |_| {} const pane = p.panes[0].?; pane.tty_filter = filter; - p.update(.{ .output = .{ .pane = 0, .bytes = payload } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = payload } }); const diffs = try modeStyleDiffs(p, pane, testing.allocator, if (filter) "bg filter=on" else "bg filter=off"); try testing.expectEqual(@as(usize, 0), diffs); } @@ -2941,7 +2941,7 @@ const TerminalTests = struct { for (0..60) |i| { var buf: [40]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[3{d}mL{d:0>2}\x1b[0m\r\n", .{ (i % 6) + 1, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } var frame = std.heap.ArenaAllocator.init(testing.allocator); @@ -3004,7 +3004,7 @@ const TerminalTests = struct { p.sync(); pane.tty_filter = filter; pane.body.mode = mode; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[32m\u{754C}\x1b[41m\x1b[K\x1b[7G\x1b[44m\x1b[K\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[32m\u{754C}\x1b[41m\x1b[K\x1b[7G\x1b[44m\x1b[K\r\n" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -3040,7 +3040,7 @@ const TerminalTests = struct { const pane = p.panes[0].?; pane.tty_filter = false; pane.body.mode = mode; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[41mzzzzzzzzzzzzz\u{754C}\x1b[0m\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[41mzzzzzzzzzzzzz\u{754C}\x1b[0m\r\n" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -3070,7 +3070,7 @@ const TerminalTests = struct { for (0..40) |i| { var buf: [64]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[38;5;{d}mline-{d:0>2}\x1b[0m\r\n", .{ 20 + i, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } p.shell_rows.stale = true; @@ -3124,12 +3124,12 @@ const TerminalTests = struct { const tx = r.x + config.GUTTER; const body_y = bodyY(p, r); - p.update(.{ .output = .{ .pane = 0, .bytes = "plain text\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "plain text\r\n" } }); p.shell_rows.stale = true; const before = try p.render(frame.allocator()); const plain = before.at(tx, body_y).style; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?5h" } }); p.shell_rows.stale = true; _ = frame.reset(.retain_capacity); const after = try p.render(frame.allocator()); @@ -3153,7 +3153,7 @@ const TerminalTests = struct { for (0..6) |i| { var buf: [40]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[38;5;{d}mrow-{d:0>2}\x1b[0m\r\n", .{ 16 + i, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } var frame = std.heap.ArenaAllocator.init(testing.allocator); @@ -3192,7 +3192,7 @@ const TerminalTests = struct { pane.tty_filter = false; pane.body.mode = .normal; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[31m000\x1b[0m\r\n\x1b[32m111\x1b[0m\r\n\r\n\x1b[34m333\x1b[0m\r\n\x1b[35m444\x1b[0m" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[31m000\x1b[0m\r\n\x1b[32m111\x1b[0m\r\n\r\n\x1b[34m333\x1b[0m\r\n\x1b[35m444\x1b[0m" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -3228,7 +3228,7 @@ const TerminalTests = struct { pane.tty_filter = false; pane.body.mode = .tty; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[31mAAA\x1b[0m\r\n\x1b[32mBBB\x1b[0m\r\n\x1b[34mCCC\x1b[0m" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -3267,7 +3267,7 @@ const TerminalTests = struct { for (0..6) |i| { var buf: [64]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[38;5;{d}mrow-{d}-abcdefgh\x1b[0m\r\n", .{ 30 + i, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } // One `Z` typed into the middle of row 3's own text. pane.ovl = .{ .row = 3, .rows = 1, .text = try p.gpa.dupe(u8, "row-3-abcZdefgh") }; @@ -3317,7 +3317,7 @@ const TerminalTests = struct { for (0..26) |i| { var buf: [64]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[38;5;{d}mrow-{d:0>2}-xyzzy\x1b[0m\r\n", .{ 20 + i, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } pane.ovl = .{ .row = 23, @@ -3376,7 +3376,7 @@ const TerminalTests = struct { for (0..26) |i| { var buf: [64]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[38;5;{d}mrow-{d:0>2}-xyzzy\x1b[0m\r\n", .{ 20 + i, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } p.shell_rows.stale = true; @@ -3464,7 +3464,7 @@ const TerminalTests = struct { for (0..26) |i| { var buf: [64]u8 = undefined; const bytes = std.fmt.bufPrint(&buf, "\x1b[38;5;{d}mrow-{d:0>2}-xyzzy\x1b[0m\r\n", .{ 20 + i, i }) catch unreachable; - p.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); } // A newline typed at column 0 of row 24, and `WW` typed on the blank row // below the output: the span covers rows 24, 25 and that blank row. @@ -3528,7 +3528,7 @@ test "cancelled normal gestures leave no count or subprefix through raw tty mode defer p.deinit(); const pane = p.panes[0].?; pane.body.mode = .normal; - p.update(.{ .output = .{ .pane = 0, .bytes = "abcdefghij\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "abcdefghij\r\n" } }); for (keys) |cp| p.update(.{ .key = .{ .cp = cp } }); try std.testing.expect(!std.meta.eql(modal.Normal.State{}, pane.body.normal)); p.update(.{ .key = .{ .cp = p.opts.tty_toggle, .ctrl = true } }); @@ -3621,7 +3621,7 @@ test "terminal overlays preserve trailing blank styles without coloring inserted while (p.nextEffect()) |_| {} const pane = p.panes[0].?; pane.tty_filter = false; - p.update(.{ .output = .{ .pane = 0, .bytes = "A\r\n\x1b[41m\x1b[2K\r\n\x1b[42m\x1b[2K\r\n\x1b[44m\x1b[2K\x1b[0m" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "A\r\n\x1b[41m\x1b[2K\r\n\x1b[42m\x1b[2K\r\n\x1b[44m\x1b[2K\x1b[0m" } }); pane.body.mode = .normal; pane.ovl = .{ .row = 0, .rows = 3, .text = try gpa.dupe(u8, "A\n\n") }; var frame = std.heap.ArenaAllocator.init(gpa); @@ -3713,7 +3713,7 @@ test "a terminal keeps its tag and two body rows through a squeeze, a drag and a p.sync(); try std.testing.expectEqual(floor, p.view().rects[term].h); var rows: ?u16 = null; - while (p.nextEffect()) |effect| if (effect == .resize_pty and effect.resize_pty.pane == term) { + while (p.nextEffect()) |effect| if (effect == .resize_pty and effect.resize_pty.pane.index() == term) { rows = effect.resize_pty.rows; }; try std.testing.expectEqual(@as(?u16, layout.min_body_rows), rows); @@ -3751,7 +3751,7 @@ const PdfAddon = struct { const j = pardes.host_io.KindJob.take(core, a.pane, a.serial, a.token) orelse continue; defer j.drop(); j.run(); - try land.step(.{ .job = .{ .pane = j.pane, .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); + try land.step(.{ .job = .{ .pane = .at(j.pane), .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); } } @@ -3760,7 +3760,7 @@ const PdfAddon = struct { items: [16]Asked = undefined, n: usize = 0, fn append(q: *@This(), j: anytype) void { - if (q.n < q.items.len) q.items[q.n] = .{ .pane = j.pane, .serial = j.serial, .token = j.token }; + if (q.n < q.items.len) q.items[q.n] = .{ .pane = j.pane.index(), .serial = j.serial, .token = j.token }; q.n += @intFromBool(q.n < q.items.len); } fn pop(q: *@This()) ?Asked { @@ -3777,7 +3777,7 @@ const PdfAddon = struct { } }.f, .job = struct { - fn f(_: ?*anyopaque, pane: u8, serial: u32, token: u32) void { + fn f(_: ?*anyopaque, pane: pardes.Pane.Id, serial: u32, token: u32) void { asked.append(.{ .pane = pane, .serial = serial, .token = token }); } }.f, @@ -3833,7 +3833,7 @@ const PdfAddon = struct { var words: [64]pardes.Kind.LoadedWord = undefined; var why: pardes.Kind.Buf(160) = .{}; const lib = pardes.Addon.readLoaded(pdf_lib, &words, &why); - try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + try land.step(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &lib } }); var buf: [4096]u8 = undefined; const real = try std.Io.Dir.cwd().realPathFile(std.testing.io, "docs/design.pdf", &buf); var line: [4200]u8 = undefined; @@ -3903,7 +3903,7 @@ const PdfAddon = struct { var words: [64]pardes.Kind.LoadedWord = undefined; var why: pardes.Kind.Buf(160) = .{}; const lib = pardes.Addon.readLoaded(@import("test_addons").image, &words, &why); - try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + try land.step(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &lib } }); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "dot.ppm", .data = "P6\n1 1\n255\n\x01\x02\x03" }); @@ -4202,7 +4202,7 @@ const PdfAddon = struct { var words: [64]pardes.Kind.LoadedWord = undefined; var why: pardes.Kind.Buf(160) = .{}; const lib = pardes.Addon.readLoaded(pdf_lib, &words, &why); - try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + try land.step(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &lib } }); const kind = a.core.kinds.n - 1; win.core().kinds.ops[kind] = a.core.kinds.ops[kind]; const id = try opened(a, land); @@ -4245,7 +4245,7 @@ const PdfAddon = struct { var words: [64]pardes.Kind.LoadedWord = undefined; var why: pardes.Kind.Buf(160) = .{}; const lib = pardes.Addon.readLoaded(pdf_lib, &words, &why); - try land.step(.{ .addon = .{ .op = .load, .pane = 0, .kind = &lib } }); + try land.step(.{ .addon = .{ .op = .load, .pane = .at(0), .kind = &lib } }); const kind = a.core.kinds.n - 1; win.core().kinds.ops[kind] = a.core.kinds.ops[kind]; const id = try opened(a, land); diff --git a/test/perf.zig b/test/perf.zig index 17df0bdc..880e6abe 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -732,7 +732,7 @@ fn measureTerm(op: TermOp, fx: TermFixture, reps: usize) !Cell { const t0 = nowNs(); switch (op) { .render => {}, - .output => core.update(.{ .output = .{ .pane = 0, .bytes = term_chunk } }), + .output => core.update(.{ .output = .{ .pane = .at(0), .bytes = term_chunk } }), .resize_rows => { rows = if (rows == screen_rows) screen_rows - 1 else screen_rows; core.update(.{ .resize = .{ .cols = cols, .rows = rows } }); @@ -782,7 +782,7 @@ fn bootTerm(fx: TermFixture) !*pardes.Pardes { var off: usize = 0; while (off < text.len) { const end = @min(off + 8192, text.len); - core.update(.{ .output = .{ .pane = 0, .bytes = text[off..end] } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = text[off..end] } }); pump(core); off = end; } @@ -925,7 +925,7 @@ fn measureUndoOutput(core: *pardes.Pardes, counter: *Allocations, arena: *std.he while (offset < bytes.len) { const stop = @min(offset + 4096, bytes.len); const update_begin = nowNs(); - core.update(.{ .output = .{ .pane = 0, .bytes = bytes[offset..stop] } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = bytes[offset..stop] } }); pump(core); updates[nupdates] = nowNs() - update_begin; nupdates += 1; @@ -1001,7 +1001,7 @@ fn measureTerminalSession(io: std.Io, mib: usize, reps: usize, json: bool) !void var offset: usize = 0; while (offset < bytes.len) { const end = @min(offset + 4096, bytes.len); - core.update(.{ .output = .{ .pane = 0, .bytes = bytes[offset..end] } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = bytes[offset..end] } }); pump(core); offset = end; } @@ -1043,7 +1043,7 @@ fn measureTerminalSession(io: std.Io, mib: usize, reps: usize, json: bool) !void try checkTerminalTail(core, try core.render(arena.allocator()), batches - 1); core.update(.{ .key = .{ .cp = 'f', .text = "f" } }); const input = core.nextEffect() orelse return error.TerminalInputMissing; - if (input != .write or input.write.pane != 0 or !std.mem.eql(u8, "f", input.write.bytes.slice())) + if (input != .write or input.write.pane.index() != 0 or !std.mem.eql(u8, "f", input.write.bytes.slice())) return error.TerminalInputWrong; if (core.nextEffect() != null) return error.UnexpectedTerminalEffect; std.mem.sort(u64, samples[0..batches], {}, std.sort.asc(u64)); @@ -1063,7 +1063,7 @@ fn measureTerminalSession(io: std.Io, mib: usize, reps: usize, json: bool) !void const update = try std.fmt.bufPrint(&update_buffer, "\r\n\x1b[32mTURN {d:0>10} READY\x1b[0m", .{tail_id}); _ = arena.reset(.retain_capacity); const begin = nowNs(); - core.update(.{ .output = .{ .pane = 0, .bytes = update } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = update } }); pump(core); const cursor_row = @as(i32, @intCast(pardes.Term.grid(pane.terminal).offset)) + @as(i32, pardes.Term.grid(pane.terminal).cursor_y); pane.ovl.?.row = if (phase == 0) 0 else cursor_row - 1; diff --git a/test/replicas.zig b/test/replicas.zig index 88881dd7..57b6ff23 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -102,7 +102,8 @@ const Host = struct { return h.draws; } - fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { + fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { + const pane = id.index(); const h: *Host = @ptrCast(@alignCast(ctx.?)); h.echo.append(gpa, .{ .pane = pane, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; } @@ -123,7 +124,8 @@ const Host = struct { /// it at once on the core's side, inside the perform that asked it, /// where no tape records it: a client replays that perform without it). /// At the setup, before any client, at once inside this call. - fn job(ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void { + fn job(ctx: ?*anyopaque, id: pardes.Pane.Id, serial: u32, token: u32) void { + const pane = id.index(); const h: *Host = @ptrCast(@alignCast(ctx.?)); const j = pardes.host_io.KindJob.take(h.core.?, pane, serial, token) orelse return; if (Draws.go(null, j)) return; @@ -807,7 +809,8 @@ const Soak = struct { return of(ctx).at_frame; } - fn ptyWrite(ctx: ?*anyopaque, pane: u8, bytes: []const u8) void { + fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { + const pane = id.index(); const s = of(ctx); s.echo.append(gpa, .{ .pane = pane, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; } @@ -827,7 +830,8 @@ const Soak = struct { /// its loop's event (with no `job`, Effect.perform runs it at once on /// the core's side, inside the perform that asked it, where no tape /// records it: a client replays that perform without it). - fn job(ctx: ?*anyopaque, pane: u8, serial: u32, token: u32) void { + fn job(ctx: ?*anyopaque, id: pardes.Pane.Id, serial: u32, token: u32) void { + const pane = id.index(); const core = of(ctx).l.core; const j = pardes.host_io.KindJob.take(core, pane, serial, token) orelse return; if (Draws.go(null, j)) return; @@ -1014,7 +1018,7 @@ const Soak = struct { cl.read_done = false; cl.gen += 1; cl.id = welcome.client; - cl.view = welcome.view; + cl.view = welcome.view.index(); cl.digest = welcome.digest; s.totals.joins += 1; try net.toClient(c, now, .dump, cl.gen, try whole.toOwnedSlice(gpa)); @@ -1185,7 +1189,7 @@ const Soak = struct { // program's output a frame a tick (tui.zig `waitInput`). .echo => { s.clock(); - core.update(.{ .output = .{ .pane = @intCast(m.who), .bytes = m.bytes } }); + core.update(.{ .output = .{ .pane = .at(m.who), .bytes = m.bytes } }); return false; }, .output => { @@ -1205,7 +1209,7 @@ const Soak = struct { if (s.program.at < s.program.bytes.len) try net.send(.{ .at = m.at + if (m.who == 1) @as(u64, 450) else 3_000, .n = 0, .kind = .output, .who = m.who, .bytes = &.{} }) else if (m.who == 1) s.flood_end = m.at; - core.update(.{ .output = .{ .pane = s.shell, .bytes = chunk } }); + core.update(.{ .output = .{ .pane = .at(s.shell), .bytes = chunk } }); took_output = true; }, .frame => { @@ -1271,7 +1275,7 @@ const Soak = struct { // A state job's answer: a step of the session's own. if (j.draw == null) { s.clock(); - core.update(.{ .job = .{ .pane = j.pane, .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); + core.update(.{ .job = .{ .pane = .at(j.pane), .serial = j.serial, .token = j.token, .gen = j.gen, .bytes = j.out } }); j.drop(); return false; } @@ -2139,7 +2143,7 @@ const EchoHost = struct { return false; } - fn ptyWrite(ctx: ?*anyopaque, _: u8, bytes: []const u8) void { + fn ptyWrite(ctx: ?*anyopaque, _: pardes.Pane.Id, bytes: []const u8) void { const h: *EchoHost = @ptrCast(@alignCast(ctx.?)); h.writes.append(gpa, .{ .seq = h.current, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; } @@ -2287,7 +2291,7 @@ fn runEcho(seed: u64, rtt_ms: u64, on: bool, et: *EchoTotals) !void { // The shell's first prompt. sh.put(prompt_bytes); - a.output(.{ .output = .{ .pane = shell, .bytes = sh.out.items } }); + a.output(.{ .output = .{ .pane = .at(shell), .bytes = sh.out.items } }); sh.out.clearRetainingCapacity(); var next_step: usize = 0; try push(&queue, &n, .{ .at = corpus.steps.items[0].gap, .n = 0, .kind = .input, .who = 1, .bytes = &.{} }); @@ -2351,7 +2355,7 @@ fn runEcho(seed: u64, rtt_ms: u64, on: bool, et: *EchoTotals) !void { } sh.key(m.bytes); if (sh.out.items.len > 0) { - a.output(.{ .output = .{ .pane = shell, .bytes = sh.out.items } }); + a.output(.{ .output = .{ .pane = .at(shell), .bytes = sh.out.items } }); sh.out.clearRetainingCapacity(); if (ty) |k| k.echoed = true; } diff --git a/test/rollback_bench.zig b/test/rollback_bench.zig index d750871e..24203308 100644 --- a/test/rollback_bench.zig +++ b/test/rollback_bench.zig @@ -80,7 +80,7 @@ fn build(s: Session, dir: []const u8, a: std.mem.Allocator) !*pardes.Pardes { var off: usize = 0; while (off < chunk.written().len) { const end = @min(off + 8192, chunk.written().len); - core.update(.{ .output = .{ .pane = 0, .bytes = chunk.written()[off..end] } }); + core.update(.{ .output = .{ .pane = .at(0), .bytes = chunk.written()[off..end] } }); pump(core); off = end; } diff --git a/test/session.zig b/test/session.zig index bf118533..1cbee9df 100644 --- a/test/session.zig +++ b/test/session.zig @@ -288,11 +288,11 @@ pub const Session = struct { .addon => pardesOf(s).perform(e), // A kind's job, here and at once, as the host's worker would; // held while a test holds them (`hold_jobs`). - .job => |j| if (!s.hold_jobs) pardesOf(s).perform(e) else if (pardes.host_io.KindJob.take(pardesOf(s), j.pane, j.serial, j.token)) |taken| s.held.append(pardesOf(s).gpa, taken) catch taken.drop(), + .job => |j| if (!s.hold_jobs) pardesOf(s).perform(e) else if (pardes.host_io.KindJob.take(pardesOf(s), j.pane.index(), j.serial, j.token)) |taken| s.held.append(pardesOf(s).gpa, taken) catch taken.drop(), .lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }), .watch => |w| { if (s.watch_fd < 0) s.watch_fd = pardes.file_watch.init(true); - if (s.watch_fd >= 0) _ = pardes.file_watch.applyEffect(pardesOf(s), std.Io.Threaded.global_single_threaded.io(), s.watch_fd, &s.watches, w.pane, w.on, w.mode); + if (s.watch_fd >= 0) _ = pardes.file_watch.applyEffect(pardesOf(s), std.Io.Threaded.global_single_threaded.io(), s.watch_fd, &s.watches, w.pane.index(), w.on, w.mode); }, else => {}, }; diff --git a/test/terminal.zig b/test/terminal.zig index fafaa605..e6c530d5 100644 --- a/test/terminal.zig +++ b/test/terminal.zig @@ -57,7 +57,7 @@ fn ptyWritesByPane(p: *Pardes, out: *std.ArrayList(u8)) ![]const u8 { var per: [pardes.MAX_PANES]std.ArrayList(u8) = @splat(.empty); defer for (&per) |*b| b.deinit(p.gpa); while (p.nextEffect()) |effect| { - if (effect == .write) try per[effect.write.pane].appendSlice(p.gpa, effect.write.bytes.slice()); + if (effect == .write) try per[effect.write.pane.index()].appendSlice(p.gpa, effect.write.bytes.slice()); } for (per, 0..) |b, id| if (b.items.len > 0) try out.print(p.gpa, "{d}:{s}\n", .{ id, b.items }); return out.items; @@ -163,15 +163,15 @@ test "terminal state keeps parser fragments and sends emulator replies through i const pane = p.panes[0].?; const state = of(pane.terminal.?); try std.testing.expect(state.stream.handler.inner.terminal == &state.vt); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[" } }); while (p.nextEffect()) |effect| { if (effect == .write) return error.PrematureTerminalReply; } - p.update(.{ .output = .{ .pane = 0, .bytes = "6n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "6n" } }); var answers: usize = 0; while (p.nextEffect()) |effect| { if (effect != .write) continue; - try std.testing.expectEqual(@as(u8, 0), effect.write.pane); + try std.testing.expectEqual(0, effect.write.pane.index()); try std.testing.expectEqualStrings("\x1b[1;1R", effect.write.bytes.slice()); answers += 1; } @@ -212,7 +212,7 @@ test "a restored command pane comes back finished, its command not run again" { const cmd = p.freeSlot().?; _ = try panes.newCommand(p, cmd, "/tmp", "make test"); p.panes[cmd].?.feedOutput(p, "ok\r\n"); - p.update(.{ .exited = .{ .pane = @intCast(cmd), .status = 2 } }); + p.update(.{ .exited = .{ .pane = .at(cmd), .status = 2 } }); try pardes.dump.dumpState(p); const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }, p.dump_out.?); defer restored.deinit(); @@ -248,7 +248,7 @@ test "a restored terminal keeps its history above a marker and gets a shell wher defer restored.deinit(); var spawned = false; while (restored.nextEffect()) |effect| switch (effect) { - .spawn => |sp| spawned = sp.pane == 0 and std.mem.eql(u8, sp.cwd.slice(), "/tmp"), + .spawn => |sp| spawned = sp.pane.index() == 0 and std.mem.eql(u8, sp.cwd.slice(), "/tmp"), else => {}, }; try std.testing.expect(spawned); @@ -323,7 +323,7 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu try testing.expect(pane.tty_filter); pane.tty_filter = false; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[31mA" ++ + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[31mA" ++ "\x1b[38;5;196mB" ++ "\x1b[38;2;255;0;0mC" ++ "\x1b[0;48;5;25mD" ++ @@ -384,7 +384,7 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu } } - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]4;1;#ff0000\x1b\\" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]4;1;#ff0000\x1b\\" } }); const osc_red = of(pane.terminal.?).vt.colorForXterm(.{ .palette = 1 }).?; try testing.expect(osc_red.eql(.{ .r = 255, .g = 0, .b = 0 })); pane.tty_filter = false; @@ -396,7 +396,7 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu // Dynamic default foreground/background colours key every default cell, // including the otherwise blank end of the row. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]10;#ff0000\x1b\\" ++ + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]10;#ff0000\x1b\\" ++ "\x1b]11;#5f5f5f\x1b\\" } }); const dyn_fg = of(pane.terminal.?).vt.colorForXterm(.{ .dynamic = .foreground }).?; const dyn_bg = of(pane.terminal.?).vt.colorForXterm(.{ .dynamic = .background }).?; @@ -410,7 +410,7 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu const explicit_before_reverse = dynamic.at(tx, body_y).style.fg; try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), explicit_before_reverse); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?5h" } }); _ = frame.reset(.retain_capacity); const reversed = try p.render(frame.allocator()); const reversed_blank = reversed.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; @@ -421,7 +421,7 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu try testing.expectEqual(plain.Color{ .rgb = p.theme().bg.? }, reversed_explicit.fg); try testing.expect(!std.meta.eql(reversed_explicit.fg, reversed_explicit.bg)); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5l" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?5l" } }); _ = frame.reset(.retain_capacity); const unreversed = try p.render(frame.allocator()); const unreversed_blank = unreversed.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; @@ -430,7 +430,7 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu // The cache is keyed by values, not a theme name. Replacing a custom // theme in place immediately recolours already-rendered indexed cells. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]104;1\x1b\\" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]104;1\x1b\\" } }); var custom = p.theme().*; custom.name = try p.gpa.dupe(u8, "same-name"); custom.palette = null; @@ -482,7 +482,7 @@ test "terminal Filter preserves exact palette-null light theme default roles" { try testing.expectEqual(plain.Color{ .rgb = light.fg.? }, ordinary.at(tx, body_y).style.fg); try testing.expectEqual(plain.Color{ .rgb = light.bg.? }, ordinary.at(tx, body_y).style.bg); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?5h" } }); _ = frame.reset(.retain_capacity); const reversed = try p.render(frame.allocator()); try testing.expectEqual(plain.Color{ .rgb = light.bg.? }, reversed.at(tx, body_y).style.fg); @@ -514,7 +514,7 @@ test "terminal Filter maps the default roles before it maps anything else" { } p.settings.theme = 0; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]11;#5f5f5f\x1b\\" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]11;#5f5f5f\x1b\\" } }); var moved = filterOf(&ansi.inkOf(p.theme()), &p.tty_filter_palette, &of(pane.terminal.?).vt); try testing.expect(!asGhostRgb(moved.default_bg).eql(asGhostRgb(moved.theme_bg))); try testing.expect(asGhostRgb(moved.default_bg).eql(keyedRgb(&moved, .{ .r = 0x5f, .g = 0x5f, .b = 0x5f }))); @@ -528,7 +528,7 @@ test "terminal Filter refuses a foreground that would collapse onto the page" { const pane = p.panes[0].?; try testing.expect(pane.tty_filter); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[38;2;255;255;255mW" ++ + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[38;2;255;255;255mW" ++ "\x1b[0;30mB" ++ "\x1b[0;31mR" } }); @@ -623,7 +623,7 @@ test "tty ansi colors follow the prompt hug into normal mode" { const pane = p.panes[0].?; pane.tty_filter = false; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;A\x1b\\\x1b[32mPP\x1b]133;B\x1b\\\x1b[31mR\x1b[34mB\x1b[0m out" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;A\x1b\\\x1b[32mPP\x1b]133;B\x1b\\\x1b[31mR\x1b[34mB\x1b[0m out" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -660,7 +660,7 @@ test "a prompt row hidden end to end paints nothing at all" { p.sync(); pane.tty_filter = false; pane.body.mode = .normal; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;A\x1b\\\x1b[32maaaaaaaaa\x1b]133;B\x1b\\\x1b[41;36m\u{754C}\x1b[0m\r\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b]133;A\x1b\\\x1b[32maaaaaaaaa\x1b]133;B\x1b\\\x1b[41;36m\u{754C}\x1b[0m\r\n" } }); var frame = std.heap.ArenaAllocator.init(testing.allocator); defer frame.deinit(); @@ -686,9 +686,9 @@ test "a program that asked for focus reports hears CSI I and CSI O as its pane t _ = try ptyWritesByPane(p, &out); p.view().active = 0; // Asking while its pane has the keyboard: told at once that it has. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1004h" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[?1004h" } }); try std.testing.expectEqualStrings("0:\x1b[I\n", try ptyWritesByPane(p, &out)); - p.update(.{ .output = .{ .pane = 1, .bytes = "\x1b[?1004h" } }); + p.update(.{ .output = .{ .pane = .at(1), .bytes = "\x1b[?1004h" } }); try std.testing.expectEqualStrings("", try ptyWritesByPane(p, &out)); // A click into the other pane moves the keyboard: out of one, into the // other. @@ -724,7 +724,7 @@ test "a terminal's kitty graphics image is drawn where its program placed it, an defer frame.deinit(); const pane = p.panes[0].?; pane.body.mode = .tty; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3;5H" ++ kittyTransmit("c=2,r=1") } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[3;5H" ++ kittyTransmit("c=2,r=1") } }); var s = try p.render(frame.allocator()); const places = imagePlaces(s); try std.testing.expectEqual(@as(usize, 1), places.len); @@ -740,20 +740,20 @@ test "a terminal's kitty graphics image is drawn where its program placed it, an try std.testing.expectEqual(plain.RectPx{ .x = 0, .y = 0, .w = 20, .h = 20 }, place.at.?); // Scrolled up by the shell's output, it goes up with the text. - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[24;1H\n\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[24;1H\n\n" } }); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); try std.testing.expectEqual(p.bodyTop(pane, r), imagePlaces(s)[0].dst.y); - p.update(.{ .output = .{ .pane = 0, .bytes = "\n" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\n" } }); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[1;1H" ++ kittyTransmit("c=2,r=1") } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[1;1H" ++ kittyTransmit("c=2,r=1") } }); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b_Ga=d,d=A\x1b\\" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b_Ga=d,d=A\x1b\\" } }); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); @@ -770,15 +770,15 @@ test "ED 3 takes away a kitty placement whose row it erased: it never comes back defer frame.deinit(); const pane = p.panes[0].?; pane.body.mode = .tty; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[1;1H" ++ kittyTransmit("c=2,r=1") } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[1;1H" ++ kittyTransmit("c=2,r=1") } }); const storage = &of(pane.terminal.?).vt.screens.active.kitty_images; try std.testing.expectEqual(@as(usize, 1), storage.placements.count()); // Scrolled into the history, it is not on screen; `clear` (ED 2 then // ED 3) erases that history and the placement with it. - p.update(.{ .output = .{ .pane = 0, .bytes = &lineBreaks(40) } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = &lineBreaks(40) } }); var s = try p.render(frame.allocator()); try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[H\x1b[2J\x1b[3J" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[H\x1b[2J\x1b[3J" } }); try std.testing.expectEqual(@as(usize, 0), storage.placements.count()); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); @@ -791,7 +791,7 @@ test "a finished command's output erased by a later ED 3 is not called whole, an defer p.deinit(); const pane = p.panes[0].?; const marked = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; - p.update(.{ .output = .{ .pane = 0, .bytes = marked ++ "\r\n\x1b]133;C;aid=pardes\x07printed\r\n\x1b]133;D;0;aid=pardes\x07" ++ marked } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = marked ++ "\r\n\x1b]133;C;aid=pardes\x07printed\r\n\x1b]133;D;0;aid=pardes\x07" ++ marked } }); const before = try commandOutput(pane.terminal, gpa, 100); defer gpa.free(before.text); try std.testing.expect(before.whole); @@ -799,8 +799,8 @@ test "a finished command's output erased by a later ED 3 is not called whole, an // The command's rows go into the history, and ED 3 erases them: its // pins are `garbage` now (ghostty 1.4), not moved to the screen's top, // where another program's text would be read as its output. - p.update(.{ .output = .{ .pane = 0, .bytes = &lineBreaks(20) } }); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3J" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = &lineBreaks(20) } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[3J" } }); const after = try commandOutput(pane.terminal, gpa, 100); defer gpa.free(after.text); try std.testing.expect(!after.whole); @@ -814,13 +814,13 @@ test "a selection whose row ED 3 erased goes; one on the screen stays" { defer p.deinit(); const pane = p.panes[0].?; pane.body.mode = .tty; - p.update(.{ .output = .{ .pane = 0, .bytes = &lineBreaks(20) } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = &lineBreaks(20) } }); pane.body.mode = .normal; const pages = &of(pane.terminal.?).vt.screens.active.pages; const top: i32 = @intCast(pages.total_rows - pane.rows); const row: i32 = if (in_history) top - 3 else top + 2; pane.body.vsel = .{ .active = true, .row = row, .col = 1 }; - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3J" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[3J" } }); try std.testing.expectEqual(!in_history, pane.body.vsel.active); if (!in_history) try std.testing.expectEqual(@as(i32, 2), pane.body.vsel.row); } @@ -840,7 +840,7 @@ test "a virtual kitty placement shows through its placeholder cells, which draw // yazi's way: a virtual placement two cells by one, then the two // placeholders, the image's id in their colour, row and column in // their diacritics. - p.update(.{ .output = .{ .pane = 0, .bytes = kittyTransmit("U=1,c=2,r=1,q=2") ++ + p.update(.{ .output = .{ .pane = .at(0), .bytes = kittyTransmit("U=1,c=2,r=1,q=2") ++ "\x1b[2;3H\x1b[38;2;0;0;7m\u{10EEEE}\u{0305}\u{0305}\u{10EEEE}\u{0305}\u{030D}\x1b[39mX" } }); const s = try p.render(frame.allocator()); const places = imagePlaces(s); @@ -864,7 +864,7 @@ test "a terminal's image is a placement whatever the frontend draws, and the Pet defer frame.deinit(); const pane = p.panes[0].?; pane.body.mode = .tty; - p.update(.{ .output = .{ .pane = 0, .bytes = kittyTransmit("c=2,r=1") } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = kittyTransmit("c=2,r=1") } }); const r = p.view().rects[0]; const tx = r.x + config.GUTTER; const y = p.bodyTop(pane, r); @@ -921,7 +921,7 @@ test "a program asking the cell size in pixels (CSI 16 t) is told the shell's" { var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); _ = try ptyWritesByPane(p, &out); - p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[16t" } }); + p.update(.{ .output = .{ .pane = .at(0), .bytes = "\x1b[16t" } }); try std.testing.expectEqualStrings("0:\x1b[6;20;10t\n", try ptyWritesByPane(p, &out)); } @@ -960,7 +960,7 @@ test "a replica's terminal pane draws as its authority's does" { var line: [256]u8 = undefined; for (0..60) |i| { const bytes = try std.fmt.bufPrint(&line, "\x1b[1;3{d}m{d}\x1b[0m 漢字 e\u{301} \x1b[48;2;{d};40;60mbg\x1b[0m and then some text that wraps past the width of the pane\r\n", .{ i % 8, i, i * 4 % 256 }); - a.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + a.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); delta.clearRetainingCapacity(); try testing.expect(try encode(&enc, &tr, &peer, ta, @intCast(i + 1), "", &delta.writer)); peer.ack((try rep.apply(gpa, std.Io.Threaded.global_single_threaded.io(), tb, delta.written())).seq); |
