diff options
Diffstat (limited to 'src/pardes.zig')
| -rw-r--r-- | src/pardes.zig | 159 |
1 files changed, 145 insertions, 14 deletions
diff --git a/src/pardes.zig b/src/pardes.zig index 2bee1f98..b5e729bb 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -61,6 +61,8 @@ pub const config = @import("config.zig"); pub const pdf_enabled = panes.Pdf.enabled; pub const pdf = panes.Pdf.pdf; pub const memory = @import("memory.zig"); +pub const tape = @import("tape.zig"); +pub const walk = @import("walk.zig"); pub const image = @import("image.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); @@ -165,6 +167,8 @@ pub const Turn = struct { /// a step. Nests: a directory listing that resolves each entry yields /// inside a yield, and only the outermost one lets go. pub fn yield(t: *Turn) void { + // A replica makes no syscall: each is a site of the tape's. + if (tape.replaying()) @panic("tape: a syscall that is no tape site, made replaying"); const io = t.io orelse return; yield_depth += 1; if (yield_depth != 1) return; @@ -504,6 +508,7 @@ test { _ = @import("Finder.zig"); _ = @import("Tables.zig"); _ = @import("Handover.zig"); + _ = @import("walk.zig"); } pub const Builtin = builtins.registry.Builtin(); @@ -759,6 +764,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest pane.file.?.tree_context = true; pane.file.?.scroll = 8; pane.msg_len = 0; + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expect(p.settings.tree_context_tag_style); const full_height = p.surface.body_layers[0].viewport.h; @@ -766,6 +772,7 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest try std.testing.expectEqual(FontRole.tagline, p.surface.body_layers[0].cells[0].style.font_role); pane.msg[0] = '!'; pane.msg_len = 1; + p.sync(); _ = try p.render(p.scratch.allocator()); // An overlay takes nothing: the message is a chip OVER the body's first // row, so the viewport keeps every row it had. @@ -783,10 +790,12 @@ test "TreeContext tag style preserves message rows and its opt-out survives rest try std.testing.expectEqualStrings("!", band.cells[p.tagCapacity(band.viewport.w) - 2].grapheme()); pane.msg_len = 0; p.leader_on = true; + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expectEqual(full_height, p.surface.body_layers[0].viewport.h); p.leader_on = false; try std.testing.expect(p.executeBuiltinLine(0, "TreeContextTagStyle")); + p.sync(); _ = try p.render(p.scratch.allocator()); try std.testing.expectEqual(@as(u16, 0), p.surface.body_layers[0].rows); try std.testing.expectEqual(@as(u16, 0), pane.body_rows); @@ -3763,6 +3772,7 @@ pub const Effect = union(enum) { fn Buf(comptime n: usize) type { return struct { + pub const replica = .{ .len = .{.{ "data", "len" }} }; data: [n]u8 = undefined, len: u16 = 0, pub const cap = n; @@ -4701,6 +4711,26 @@ pub const Pardes = struct { /// The message row's entry points, called from everywhere as /// `p.setMessage(...)`; they live with the rest of it in Messages.zig. + /// What the replay digest leaves out (src/walk.zig): what drawing + /// keeps, what is recomputed, and what only the session that performs + /// effects has. + pub const replica = .{ + .local = .{ + // A frame, and the animations and timings that make it. + "surface", "presentation", "frames", "chrome_animation", "lifts", "lift_serials", + "cursor_glide", "cursor_view", "cursor_crossing", "cursor_held", "frame_arena", "scratch", + "needs_frame", "present_skipped", "shell_continuous", "native_image_patches", + // Drained by every pump before its digest; a replica's queues + // are its own. + "effects", "in_q", + }, + .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, + // The host's handles, the 9P server's fids and log, the store file + // every window of the user shares (and Recent read from it). + .authority = .{ "host", "fs", "store", "recent" }, + .len = .{ .{ "jumps", "njumps" }, .{ "look_src", "n_look_src" }, .{ "leader_keys", "leader_n" }, .{ "col_x", "ncol" }, .{ "col_w", "ncol" }, .{ "session_dir", "session_dir_len" } }, + }; + pub const setStatus = Messages.setStatus; pub const setMessage = Messages.setMessage; pub const reportError = Messages.reportError; @@ -4739,12 +4769,26 @@ pub const Pardes = struct { return p; } + /// A new session, its tape written from here when PARDES_RECORD asks. pub fn init(gpa: std.mem.Allocator, opts: Options) !*Pardes { + tape.startFromEnv(); + const taped = tape.enter(null, .{ .init = opts }); + const p = initSession(gpa, opts) catch |err| { + tape.leave(null, taped); + return err; + }; + tape.leave(p, taped); + return p; + } + + fn initSession(gpa: std.mem.Allocator, opts: Options) !*Pardes { // A new session draws a tab at the default until `Tab` says otherwise. config.tab_now = config.tab_width; const p = try create(gpa, opts); errdefer p.deinit(); p.boot = Boot.of(opts); + // Asked once, at the start, whoever asks first after. + _ = p.sessionDir(); switch (p.boot) { .document => { const path = opts.file.?; @@ -4877,7 +4921,9 @@ pub const Pardes = struct { /// The store read once at start: a new session's, Recent from it. pub fn loadRecent(p: *Pardes) void { - p.store = Store.init(p.opts.store_path); + // A replica's store is its own, in memory: the recording session's + // file is every window's of its user (src/tape.zig). + p.store = Store.init(if (tape.replaying()) null else p.opts.store_path); p.store.load(p); } @@ -4941,6 +4987,9 @@ pub const Pardes = struct { } pub fn deinit(p: *Pardes) void { + const taped = tape.enter(p, .{ .deinit = tape.current(p) }); + tape.forget(p); + defer tape.leave(null, taped); if (comptime hosted) if (std.c.getenv("PARDES_FRAMES")) |path| p.frames.dump(std.mem.span(path)); // Every file open now is closed with its dot, and the list kept. for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane); @@ -5001,6 +5050,8 @@ pub const Pardes = struct { /// shell feedback after write_dump: remember where the dump landed (the /// topbar grows a `Restore <path>` word pointing at it) pub fn setLastDump(p: *Pardes, path: []const u8) void { + const taped = tape.enter(p, .{ .set_last_dump = path }); + defer tape.leave(p, taped); p.pointRestoreAt(path); ctlfs.events.notePath(p, "dump", path); } @@ -5028,6 +5079,8 @@ pub const Pardes = struct { /// the shell polls this each frame: a pending Restore's dump path, or null pub fn takeRestore(p: *Pardes) ?[]const u8 { + const taped = tape.enter(p, .take_restore); + defer tape.leave(p, taped); const r = p.restore_req; p.restore_req = null; return r; @@ -5386,6 +5439,8 @@ pub const Pardes = struct { /// The shell reports the spawned pane's working directory (and later cwd /// changes it observes, e.g. via /proc/<pid>/cwd before each frame). pub fn setCwd(p: *Pardes, id: usize, cwd: []const u8) void { + const taped = tape.enter(p, .{ .set_cwd = .{ .id = id, .cwd = cwd } }); + defer tape.leave(p, taped); const pane = p.panes[id] orelse return; // A command pane stays the directory's it was run for (newCommand). if (pane.command != null) return; @@ -5415,6 +5470,8 @@ pub const Pardes = struct { } pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_marks: bool) void { + const taped = tape.enter(p, .{ .acknowledge_shell = .{ .id = id, .executable = executable, .prompt_marks = prompt_marks } }); + defer tape.leave(p, taped); if (id < MAX_PANES) if (p.panes[id]) |pane| { ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran pane.shell_failed = false; @@ -5511,10 +5568,14 @@ pub const Pardes = struct { /// file reloading under it, a theme file rewritten on disk. `pump` draws /// only when it has a reason to, and this is the reason. pub fn invalidate(p: *Pardes) void { + const taped = tape.enter(p, .invalidate); + defer tape.leave(p, taped); p.needs_frame = true; } pub fn reloadWatchedFile(p: *Pardes, id: u8, bytes: []const u8) bool { + const taped = tape.enter(p, .{ .reload_watched_file = .{ .id = id, .bytes = bytes } }); + defer tape.leave(p, taped); p.invalidate(); look.invalidateLookHover(p, id); const applied = p.applyWatchedFileChanged(id, bytes); @@ -5531,6 +5592,8 @@ pub const Pardes = struct { } pub fn nextEffect(p: *Pardes) ?Effect { + const taped = tape.enter(p, .next_effect); + defer tape.leave(p, taped); p.refillPendingWrites(); if (p.effects_len == 0) { p.effects_head = 0; @@ -5667,6 +5730,8 @@ pub const Pardes = struct { /// on it, or written to it from now on, hears `error shell gone` /// rather than wait for a prompt that never comes. pub fn shellFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .shell_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); const pane = p.panes[id] orelse return; pane.shell_failed = true; ctlfs.pty.shellGone(p, pane, false); @@ -5688,6 +5753,8 @@ pub const Pardes = struct { /// A shell started again in a terminal whose shell runs (pty/ctl's /// exec) did not start: the host kept the running one, so only said. pub fn restartFailed(p: *Pardes, id: u8, err: anyerror) void { + const taped = tape.enter(p, .{ .restart_failed = .{ .id = id, .err = err } }); + defer tape.leave(p, taped); if (p.panes[id] == null) return; p.sayShellFailure(id, err); } @@ -5700,9 +5767,7 @@ pub const Pardes = struct { // The script is there; the program its `#!` names is not. var said: [320]u8 = undefined; var interp: [256]u8 = undefined; - var buf: [std.fs.max_path_bytes]u8 = undefined; - const Shell = if (comptime hosted) @import("host_io.zig").Shell else void; - const name: ?[]const u8 = if (comptime hosted) (if (Shell.find(pane.shell orelse p.shellBin(), &buf)) |f| Shell.missingInterpreter(f, &interp) else null) else null; + const name: ?[]const u8 = if (comptime hosted) tape.value(.interpreter, missingInterpreter, .{ pane.shell orelse p.shellBin(), &interp }) else null; p.reportFailure(id, if (name) |n| std.fmt.bufPrint(&said, "shell: interpreter {s} not found", .{n}) catch "shell: interpreter not found" else "shell: interpreter not found"); } else { p.reportError(id, "shell", err); @@ -5712,6 +5777,13 @@ pub const Pardes = struct { p.noteLateFailure(id); } + /// The program the shell's `#!` names that is not there, in `out`. + fn missingInterpreter(bin: []const u8, out: []u8) ?[]const u8 { + const Shell = @import("host_io.zig").Shell; + var buf: [std.fs.max_path_bytes]u8 = undefined; + return Shell.missingInterpreter(Shell.find(bin, &buf) orelse return null, out); + } + /// A Save makes the directories its file goes in, and any above them, /// first: one rule, for the pane's own name and `Save path` alike. A /// directory it cannot make leaves the write to fail and say why. @@ -5729,11 +5801,13 @@ pub const Pardes = struct { const local = if (comptime hosted) filesystem.localPath(dir) else null; const kind: ?std.Io.File.Kind = if (comptime hosted) (if (local) |l| exec.kindOf(l) else .directory) else .directory; const missing = err == error.FileNotFound or err == error.NotDir or kind != .directory; - const denied = missing and (if (comptime hosted) (if (local) |l| filesystem.deniedAbove(l) else false) else false); + const denied = missing and (if (comptime hosted) (if (local) |l| tape.value(.denied, filesystem.deniedAbove, .{l}) else false) else false); return .{ .dir = dir, .kind = kind, .missing = missing, .denied = denied }; } pub fn saveFailed(p: *Pardes, id: u8, path: []const u8, err: anyerror) void { + const taped = tape.enter(p, .{ .save_failed = .{ .id = id, .path = path, .err = err } }); + defer tape.leave(p, taped); if (p.panes[id]) |pane| if (pane.file) |*f| { // The `-%` spelling fs.zig already uses for "make this dirty". f.saved_revision = f.revision -% 1; @@ -5755,6 +5829,8 @@ pub const Pardes = struct { /// log, and the 9P write that asked for it fails with it, as a failed /// Save's does. pub fn dumpFailed(p: *Pardes, path: []const u8, err: anyerror) void { + const taped = tape.enter(p, .{ .dump_failed = .{ .path = path, .err = err } }); + defer tape.leave(p, taped); var what: [limits.host_path_cap + 8]u8 = undefined; const operation = std.fmt.bufPrint(&what, "Dump {s}", .{path}) catch "Dump"; // Into a DumpDir that is not there: said so, ENOENT, as a Save is; @@ -5797,6 +5873,8 @@ pub const Pardes = struct { } pub fn perform(p: *Pardes, e: Effect) void { + const taped = tape.enter(p, .{ .perform = e }); + defer tape.leave(p, taped); // A Save performed on one twin is every twin's (File.syncTwins). defer panes.File.syncTwins(p); const v = p.host.vtable; @@ -5927,16 +6005,23 @@ pub const Pardes = struct { } } - pub fn pump(p: *Pardes, h: Host) !void { - p.host = h; + pub fn pump(p: *Pardes, host: Host) !void { + p.host = host; + const taped = tape.enter(p, .pump); + defer tape.leave(p, taped); + // The host as the tape sees it, while it records. + const h = p.host; const v = h.vtable; if (v.wait_input) |f| { // Asleep until input or until core animation next needs a // frame; 0 is no timeout at all. // A render-time animation draws every loop: the wait only takes // what input is there. + // The clock read whether or not it is wanted: what a replica + // waits on is its own (a picture decoding wakes only a frame + // that draws it), and the tape has the same calls either way. + const now = if (v.now) |clock| clock(h.ctx) else p.now_ns; const timeout: u32 = if (p.continuous()) 1 else if (p.nextWake()) |due| wake: { - const now = if (v.now) |clock| clock(h.ctx) else p.now_ns; const ms = ((due -| now) + std.time.ns_per_ms - 1) / std.time.ns_per_ms; break :wake @intCast(std.math.clamp(ms, 1, std.math.maxInt(u32))); } else 0; @@ -5972,6 +6057,7 @@ pub const Pardes = struct { const surface = try p.render(p.frame_arena.allocator()); p.frames.mark_render = FrameStats.nowNs(); if (v.present) |f| f(h.ctx, surface); + Stream.askAhead(p); // Settled before post_present: a shell ticks there, and what that // tick moves owes the next frame. const skipped = p.present_skipped; @@ -5990,6 +6076,8 @@ pub const Pardes = struct { /// that changes a pane costs a frame, which is why a round trip on a /// local socket costs microseconds and not a vsync. pub fn serveFs(p: *Pardes, req: ctlfs.Req) ctlfs.Reply { + const taped = tape.enter(p, .{ .serve_fs = req }); + defer tape.leave(p, taped); // The held write of an Edit whose commands run, asked again by a // retry of its park: still waiting, or its answer, never run twice. if (p.fs.edit_hold) |*h| if (h.names(req)) { @@ -6028,6 +6116,8 @@ pub const Pardes = struct { } pub fn update(p: *Pardes, ev: Event) void { + const taped = tape.enter(p, .{ .update = ev }); + defer tape.leave(p, taped); defer panes.File.syncTwins(p); defer p.reportFocus(); // A tick with nothing animating is the one event that cannot change @@ -6074,6 +6164,7 @@ pub const Pardes = struct { .tick => { Stream.step(p); Messages.advanceMessages(p); + for (p.panes) |slot| if (slot) |pane| if (pane.file) |*f| f.tables.step(pane.gpa, f); for (0..p.panes.len) |id| exec.flushReplEnter(p, id, false); }, else => {}, @@ -6661,10 +6752,16 @@ pub const Pardes = struct { pub fn sessionDir(p: *Pardes) []const u8 { if (p.session_dir_len) |n| return p.session_dir[0..n]; if (comptime !hosted) return "/"; - if (std.c.getcwd(&p.session_dir, p.session_dir.len) == null) return "/"; - const n: u16 = @intCast(std.mem.indexOfScalar(u8, &p.session_dir, 0) orelse return "/"); - p.session_dir_len = n; - return p.session_dir[0..n]; + // The directory the session that recorded was started in. + const dir = tape.value(.cwd, processDir, .{&p.session_dir}) orelse return "/"; + std.mem.copyForwards(u8, &p.session_dir, dir); + p.session_dir_len = @intCast(dir.len); + return p.session_dir[0..dir.len]; + } + + fn processDir(buf: *[limits.host_path_cap]u8) ?[]const u8 { + if (std.c.getcwd(buf, buf.len) == null) return null; + return buf[0 .. std.mem.indexOfScalar(u8, buf, 0) orelse return null]; } /// The directory a word run from pane `id`'s text or tag runs in, or, @@ -7007,6 +7104,8 @@ pub const Pardes = struct { /// What was said of a failure just now on pane `id` is also the waiting /// 9P write's (9p_io, `fs.late_failure`). pub fn noteLateFailure(p: *Pardes, id: usize) void { + const taped = tape.enter(p, .{ .note_late_failure = id }); + defer tape.leave(p, taped); // Said by reportFailure just now: its words fitted whole, the // reason kept; else what the message row has. const said = if (p.fs.said_at == p.fs.failures and p.fs.said_len > 0) @@ -7182,9 +7281,11 @@ pub const Pardes = struct { // ---- dumb undo/redo: whole-state snapshots, one per edit op ---- pub fn acknowledgePanelPresentation(p: *Pardes, tracks: []const animation.Track) void { + const taped = tape.enter(p, .{ .acknowledge_panel_presentation = tracks }); + defer tape.leave(p, taped); p.presentation.acknowledge(p, tracks); if (p.pointer_inside and p.drag != .none) { - if (p.presentation.pointer(p.screen_w, p.screen_h, p.pointer_raw_col, p.pointer_raw_row)) |mapped| { + if (p.pointerCell(p.pointer_raw_col, p.pointer_raw_row)) |mapped| { p.hover_col = mapped.col; p.hover_row = mapped.row; mouse.dragUpdate(p, mapped.col, mapped.row, p.pointer_body_hit); @@ -7193,6 +7294,14 @@ pub const Pardes = struct { look.refreshLookHoverFromRaw(p); } + /// The cell of the session's grid a pointer at this cell of the frame + /// is over, seen through the frame (a pane sliding in): the recording + /// session's answer, whose frames these were (src/tape.zig). Null over + /// a pane on its way out. + pub fn pointerCell(p: *const Pardes, col: u16, row: u16) ?Presentation.CellPosition { + return tape.value(.pointer, Presentation.pointer, .{ &p.presentation, p.screen_w, p.screen_h, col, row }); + } + pub fn abandonPanelAnimations(p: *Pardes) void { p.presentation.cancel(); look.cancelLookHover(p); @@ -7588,6 +7697,10 @@ pub const Pardes = struct { state.reply_len = 0; }; const r = p.rects[id]; + // The session's part of the pane's view geometry (File.geometry), + // and its context declarations. + if (pane.file) |*f| panes.File.refreshContext(p, pane, f); + pane.view = .{ .wrap = p.settings.wrap, .layered = p.settings.tree_context_tag_style, .body_h = p.row_metrics.body_h, .tagline_h = p.row_metrics.tagline_h, .rect_h = r.h }; // The tag takes a row per line, leaving the body one row if it // had any; a folded pane shows its first line only. // Measured on the stack: this runs on every event, and not @@ -7661,6 +7774,9 @@ pub const Pardes = struct { // columns do (Dir.fit). if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) panes.Dir.fit(p, pane, pane.cols); panes.terminal.releasePendingCommandIfReady(p, id, pane); + // Which bands a pane shows is the step's, as its geometry is: + // what is drawn under them, and where a click lands, read it. + Messages.collectNotices(p, pane, r, id); // The greeting `ls` of a new terminal in a directory is typed at // its shell's FIRST prompt, or never: typed later it would land // in whatever runs then, or in the middle of a line someone @@ -7675,6 +7791,9 @@ pub const Pardes = struct { }, }; } + // A header's caret kept in its view, as a pane's is: the step's, not + // the frame's (paintHeaders draws what this leaves). + tagline.revealHeader(p); } pub fn theme(p: *const Pardes) *const Theme { @@ -7780,6 +7899,8 @@ pub const Pardes = struct { /// the look-hover delay) is jumped to its end in one step and drawn /// once. Idle, time is not banked: the next animation starts from now. pub fn advance(p: *Pardes, now_ns: u64) void { + const taped = tape.enter(p, .{ .advance = now_ns }); + defer tape.leave(p, taped); // The first time a shell's clock is seen, core time starts there. const started = p.clock_started; p.clock_started = true; @@ -7787,7 +7908,10 @@ pub const Pardes = struct { if (!started) p.stepped_ns = now_ns; const frame = animation.frame_ns; var moving: u32 = 0; - while (p.nextWake()) |due| { + // The session's ticks are the recording session's: a picture still + // decoding or a fade still moving asks for frames the way a message + // waiting to go does, and a replica that draws nothing has none. + while (tape.value(.wake, nextWake, .{p})) |due| { if (due > now_ns) break; // Only waiting until `due`: its counters move there at once. const held = (due - p.stepped_ns) / frame; @@ -8136,8 +8260,12 @@ test "a prompt chip is a band with its caret, still on the grid, and only the ch // Inside it is chrome: mapped as body it would land on Sel row 0, which is // the TAG row, so a sweep there would highlight the tagline instead. + // The sweep closed the prompt, and its chip with it: open it again. pane.clearPointerSelection(0); pane.sel[0] = .{}; + p.startPrompt(pane, &pane.body, .{ .search = "/" }); + p.sync(); + try std.testing.expectEqual(left, pane.notices.left[0]); p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = r.x + r.w - 3, .row = row } }); p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = r.x + r.w - 2, .row = row } }); p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = r.x + r.w - 2, .row = row } }); @@ -8523,6 +8651,9 @@ test "Esc back into a file leaves its view where it was" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "src/memory.zig" }); defer p.deinit(); + // Rows are lines: the centring below counts lines, and a wrapped line + // above the cursor would push it out of the view it centres. + p.settings.wrap = false; p.update(.{ .resize = .{ .cols = 80, .rows = 24 } }); p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell under the doc const shell = p.active; |
