//! Views: several clients on one session, kakoune's way (docs/next-daemon.md, //! "Phase 4 notes"). Each view has its own layout, focus, selections and //! scroll over the panes every view shares: their text, terminals, tags, //! registers and messages. //! //! A view's share of the session is `Pardes.View`, one of `Pardes.views`, //! and the live one is `views[view]` (`Pardes.view()`): going into another //! is an index. Each view's share of a pane (`PaneView`) is still swapped: //! the live view's is in the pane's own fields, the others' in `Pane.pv` //! (copied out when another view goes live, `park`, and back, `unpark`), //! so the ~3,000 references to a pane's cursor, mode, scroll and geometry //! read it as they always did. A pane closing leaves every view's layout //! at once (`gone`). //! //! The views are in the digest; the panes' shares of the views not live //! (Pane's `pv`) are not: a pane's slot is written whenever its view goes //! out, so the live view's is stale by design, and a share that diverged //! shows when its view is live again. A view drawn out of turn (`drawOne`, //! `within`) is put back after exactly as it was. const std = @import("std"); const pardes = @import("root.zig"); const Pardes = pardes.Pardes; const View = Pardes.View; const Pane = pardes.Pane; const Text = pardes.panes.Text; const File = pardes.panes.File; const layout = pardes.layout; const MAX_PANES = pardes.MAX_PANES; /// Views at once: a window or terminal each. pub const max = 4; /// How long a view kept for its client's rejoin (`away`) is that client's /// alone: a join with no room is refused meanwhile, and takes it after /// (Pardes.openView). A client that said it joins again dials at once, and /// its join waits for its dump to cross: the soak's far link (1 Mbit/s, /// 200 ms each way) has taken up to 18.3 s from one of its joins to the /// next (a flood, joins for its lag). Past 30 s, it is taken as gone. pub const away_kept_ns: u64 = 30 * std.time.ns_per_s; /// A pane's fields that are each view's own: where it is in the view's /// layout, which of its texts has the keyboard, how far it is scrolled (a /// terminal's history too), and what the view's step measured of it (its /// tag's rows, its bands). const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "docked_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "layers", "notices", "pan", "follow_scroll", "scrolled_to" }; /// The body's: its cursor, selections and mode. Its undo stays the pane's. const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; /// Named field by field, not taken from Pane's type: a pane holds its own. pub const PaneView = struct { focus: Pane.Focus = .body, vweight: f32 = 1, collapsed: bool = false, docked_to: ?u32 = null, hscroll: i32 = 0, tag_scroll: u16 = 0, tag_top: u16 = 0, tag_rows: u16 = 1, view: Pane.SessionView = .{}, layers: File.Layers = .{}, notices: Pane.Notices = .{}, pan: u16 = 0, follow_scroll: usize = 0, scrolled_to: ?usize = null, body: Part(Text, &body_fields) = .{}, /// A document's scroll (its top: File.Top) and size; a terminal's size /// is its pty's, which every view shows (sized by its last typist, /// `Pardes.sync`). scroll: usize = 0, scroll_row: u32 = 0, cols: u16 = 1, rows: u16 = 1, /// In a dump (`Shares`): what a pane's step works out again. Its `view` /// is dumped: the step after `unpark` reads it before it syncs. pub const replica = .{ .derived = .{ "notices", "pan" } }; }; /// The panes' shares of a view not live, as a session's dump keeps them /// (dump.zig): the live view's are the panes' own fields. pub const Shares = struct { k: u8, panes: []const Share = &.{}, /// Its view, a slot (walk.zig `.index`). pub const replica = .{ .index = .{.{ "k", "slots" }} }; pub fn slots(_: *const Shares, k: usize) bool { return k < max; } pub const Share = struct { id: u16, pv: PaneView }; }; /// A view not live as a dump before `Pardes.views` kept it: its share of /// the session beside its panes' (read once, by name, into `views[k]`). pub const OldDumped = struct { k: u8, view: View = .{}, panes: []const Shares.Share = &.{}, }; fn bit(k: usize) u8 { return @as(u8, 1) << @intCast(k); } /// The panes' shares of the views not live, as a dump keeps them: the /// slices `gpa`'s, what they hold the session's. pub fn dumped(p: *const Pardes, gpa: std.mem.Allocator) ![]Shares { var out: std.ArrayList(Shares) = .empty; errdefer { for (out.items) |d| gpa.free(d.panes); out.deinit(gpa); } for (0..max) |k| { if (k == p.live or !isOpen(p, @intCast(k))) continue; var shares: std.ArrayList(Shares.Share) = .empty; errdefer shares.deinit(gpa); // A view not live has a share of every pane: written when it went // out, made with one made since (`installed`). for (p.panes, 0..) |s, id| if (s) |pane| { try shares.append(gpa, .{ .id = @intCast(id), .pv = pane.pv[k] }); }; try out.append(gpa, .{ .k = @intCast(k), .panes = try shares.toOwnedSlice(gpa) }); } return out.toOwnedSlice(gpa); } /// The panes' shares a dump kept (`dumped`) parked again in `p`, a /// session just loaded with its views: each pane its share in each view /// (one a view had none of as the live view has it). What they hold is /// the session's then, the slices the caller's. A view the core says open /// that the dump has no shares of is not. pub fn undumped(p: *Pardes, others: []const Shares) void { var take: u8 = 0; for (others) |d| if (d.k < max and d.k != p.live and isOpen(p, d.k) and take & bit(d.k) == 0) { take |= bit(d.k); }; for (0..max) |k| if (k != p.live and isOpen(p, @intCast(k)) and take & bit(k) == 0) dropView(p, @intCast(k)); p.views_open &= take | bit(p.live); p.views_away &= p.views_open; // The live view's slot owning nothing, as `installed` makes it. for (p.panes) |s| if (s) |pane| { if (take != 0) pane.pv[p.live] = fresh(pane); pane.pv_known |= bit(p.live); }; var left = take; for (others) |d| { if (left & bit(d.k) == 0) { // Refused (another build's, or twice): what it holds goes. for (d.panes) |sh| if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); continue; } left &= ~bit(d.k); for (d.panes) |sh| { const pane = (if (sh.id < MAX_PANES) p.panes[sh.id] else null) orelse { if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); continue; }; if (pane.pv_known & bit(d.k) != 0) { if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels); continue; } pane.pv[d.k] = sh.pv; pane.pv_known |= bit(d.k); } for (p.panes) |s| if (s) |pane| if (pane.pv_known & bit(d.k) == 0) { pane.pv[d.k] = fresh(pane); pane.pv_known |= bit(d.k); }; } } /// The views a dump before `Pardes.views` kept (`OldDumped`): each its /// share of the session into `views[k]`, the rest as `undumped`. What /// each view holds is the session's then; one refused is freed. pub fn undumpedOld(p: *Pardes, others: []const OldDumped, gpa: std.mem.Allocator) !void { var shares: std.ArrayList(Shares) = .empty; defer shares.deinit(gpa); var took: u8 = 0; for (others) |d| { var view = d.view; if (d.k >= max or d.k == p.live or !isOpen(p, d.k) or took & bit(d.k) != 0) { freeView(p, &view); continue; } took |= bit(d.k); p.views[d.k] = view; try shares.append(gpa, .{ .k = d.k, .panes = d.panes }); } undumped(p, shares.items); } /// The live view alone, as view 0: every other closed. A session loaded /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { p.views_away = 0; for (0..max) |k| if (k != p.live) close(p, @intCast(k)); const k = p.live; if (k == 0) return; std.mem.swap(?View, &p.views[0], &p.views[k]); for (p.panes) |s| if (s) |pane| { // The live slot is stale: as `installed` makes it, owning nothing. pane.pv[0] = fresh(pane); pane.pv_known = if (pane.pv_known & bit(k) != 0) 1 else 0; if (pane.sized_by == k) pane.sized_by = 0; if (pane.prompt_view == k) pane.prompt_view = 0; }; p.live = 0; p.views_open = 1; } /// The fields of `T` named, as a struct of their own, each of its type and /// default. fn Part(comptime T: type, comptime names: []const [:0]const u8) type { @setEvalBranchQuota(100_000); const s = @typeInfo(T).@"struct"; var types: [names.len]type = undefined; var attrs: [names.len]std.lang.Type.Struct.FieldAttributes = undefined; for (names, 0..) |name, i| { const at = for (s.field_names, 0..) |n, j| { if (std.mem.eql(u8, n, name)) break j; } else @compileError("views: " ++ @typeName(T) ++ " has no field " ++ name); types[i] = s.field_types[at]; attrs[i] = .{ .default_value_ptr = s.field_attrs[at].default_value_ptr }; } return @Struct(.auto, null, names, &types, &attrs); } fn copy(comptime names: []const [:0]const u8, dst: anytype, src: anytype) void { inline for (names) |name| @field(dst, name) = @field(src, name); } pub fn isOpen(p: *const Pardes, k: u8) bool { return k < max and p.views_open & (@as(u8, 1) << @intCast(k)) != 0; } /// Kept for its client's rejoin (`away`). pub fn isAway(p: *const Pardes, k: u8) bool { return k < max and p.views_away & (@as(u8, 1) << @intCast(k)) != 0; } /// The views someone looks at: open, none kept for a rejoin. pub fn count(p: *const Pardes) usize { return @popCount(p.views_open & ~p.views_away); } /// An open view but `k` someone looks at (`count`), if there is one. fn looked(p: *const Pardes, k: u8) ?u8 { for (0..max) |i| if (i != k and isOpen(p, @intCast(i)) and !isAway(p, @intCast(i))) return @intCast(i); return null; } /// More than one view: the one test the single client's path pays. pub fn several(p: *const Pardes) bool { return p.views_open & (p.views_open -% 1) != 0; } /// Pane `pane`'s share of the live view. fn extract(pane: *const Pane) PaneView { var v: PaneView = .{ .cols = pane.cols, .rows = pane.rows }; copy(&pane_fields, &v, pane); copy(&body_fields, &v.body, &pane.body); if (pane.file) |f| { v.scroll = f.scroll; v.scroll_row = f.scroll_row; } return v; } /// Its share of view `v` put back. fn apply(pane: *Pane, v: *const PaneView) void { copy(&pane_fields, pane, v); copy(&body_fields, &pane.body, &v.body); if (!pane.isTerminal()) { pane.cols = v.cols; pane.rows = v.rows; } const f = if (pane.file) |*file| file else return; // The text may have changed while the view was out: what it held is // brought back inside it. const lines = File.nlines(pane.gpa, f); f.scroll = @min(v.scroll, lines -| 1); f.scroll_row = v.scroll_row; if (pane.body.cur_row >= lines) { pane.body.vsel.active = false; pane.body.nsel = 0; } _ = pardes.edit.clampFileCursor(&pane.body); } /// A copy of the live view's share for another view, owning nothing of the /// live one's (its extra selections stay behind). fn fresh(pane: *const Pane) PaneView { var v = extract(pane); v.body.sels = &.{}; v.body.nsel = 0; return v; } /// A new pane is in each other view as it was made, there to be shown. pub fn installed(p: *Pardes, pane: *Pane) void { pane.sized_by = p.live; if (!several(p)) return; // The live view's slot too, owning nothing: a pane closed before its // view next parks is torn down with that slot never written, and // `tornDown` frees what the slots of the views not live hold. for (0..max) |k| if (isOpen(p, @intCast(k))) { pane.pv[k] = fresh(pane); }; pane.pv_known = p.views_open; } /// A pane going: the other views' selections of it go too. pub fn tornDown(p: *Pardes, pane: *Pane) void { for (0..max) |k| if (k != p.live and pane.pv_known & (@as(u8, 1) << @intCast(k)) != 0) { const sels = pane.pv[k].body.sels; if (sels.len > 0) p.gpa.free(sels); pane.pv[k].body.sels = &.{}; }; } /// Pane `id` closed: it leaves the layout of every view not live, and the /// keyboard of one it had moves to that view's first pane left (the live /// view's is the closing's to move, panes.removePane). pub fn gone(p: *Pardes, id: usize) void { if (!several(p)) return; for (&p.views, 0..) |*slot, k| { if (k == p.live) continue; const v = &(slot.* orelse continue); for (v.cols[0..v.ncol]) |*col| { var n: usize = 0; for (col.panes[0..col.n]) |other| if (other != id) { col.panes[n] = other; n += 1; }; col.n = n; } if (v.active != id) continue; v.active = for (v.cols[0..v.ncol]) |col| { if (col.n > 0) break col.panes[0]; } else for (p.panes, 0..) |s, i| { if (s != null and i != id) break i; } else 0; } } fn park(p: *Pardes) void { for (p.panes) |s| if (s) |pane| { // The slot owns the live text's selections now: known, so that // the pane's end or the view's frees them (a pane made while one // view was open had no view known). pane.pv[p.live] = extract(pane); pane.pv_known |= bit(p.live); }; } fn unpark(p: *Pardes, k: u8) void { p.live = k; for (p.panes) |s| if (s) |pane| { apply(pane, &pane.pv[k]); // The live body owns the selections now; the slot, stale while its // view is live, holds none (a pane closed before the view parks // again would otherwise free them twice). pane.pv[k].body.sels = &.{}; pane.pv[k].body.nsel = 0; }; layout.compute(p); // Its terminals' cursors moved while it was out: each pan follows. for (p.panes, 0..) |s, id| if (s) |pane| { pane.pan = pane.panFor(p, p.view().rects[id]); }; } /// The pane with the keyboard is one the live view shows: focused where its /// layout has it not (a Look at a file another view opened, a jump, a 9P /// focus), it goes into the view's active column, as kakoune shows a buffer /// in the window that asked for it. pub fn show(p: *Pardes) void { const v = p.view(); if (p.panes[v.active] == null or layout.findPane(p, v.active) != null) return; const c = layout.firstColumn(p) orelse layout.columnBySerial(p, v.active_column) orelse v.ncol - 1; layout.acmePlace(p, c, null, v.active); } /// Into view `k`: the live one's pane shares parked, `k`'s put in their place. pub fn to(p: *Pardes, k: u8) void { std.debug.assert(isOpen(p, k)); if (k == p.live) return; park(p); unpark(p, k); } /// A new view, as the live one is laid out and focused, with its panes /// where they are scrolled and its cursors where they are; the rest of it /// (column tags, gestures) as a session starts. Null when there /// are `max` already. pub fn open(p: *Pardes) ?u8 { const k: u8 = for (0..max) |i| { if (!isOpen(p, @intCast(i))) break @intCast(i); } else return null; const live = p.view(); p.views[k] = .{ .screen_w = live.screen_w, .screen_h = live.screen_h }; const v = &p.views[k].?; inline for (sized ++ laid_out ++ .{"frontend"}) |name| @field(v, name) = @field(live, name); // A query's mark on the jumps is the view's that asked it. v.jumps.force = false; v.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); for (p.panes) |s| { const pane = s orelse continue; pane.pv[k] = fresh(pane); pane.pv_known |= bit(k); } p.views_open |= bit(k); return k; } /// How a view is laid out and focused: what a new one takes of the live /// one (`open`), and what one going leaves the view that stays (`closeKeeping`). const laid_out = .{ "ncol", "cols", "active", "active_column", "jumps" }; const sized = .{ "screen_w", "screen_h", "cell_pixels", "row_metrics" }; /// View `k` gone, and the view that stays live laid out as `k` was: its /// columns, its focus, its panes where they were scrolled and its cursors /// where they were. For a session's last client while no one else watches /// it (replica.Peers.leave): the client's rejoin (an Upgrade's, a diverged /// one's) copies the live view (`open`), so it finds its own again. pub fn closeKeeping(p: *Pardes, k: u8) void { if (!isOpen(p, k) or count(p) == 1) return; to(p, k); // Never into one kept for a rejoin (`away`): that one is its client's. const other = looked(p, k).?; const v = &p.views[other].?; // Its size too: the terminals it sized keep theirs, no shell redrawn for // a size it is given back at the rejoin. const force = v.jumps.force; inline for (sized ++ laid_out) |name| @field(v, name) = @field(p.views[k].?, name); v.jumps.force = force; for (p.panes) |s| if (s) |pane| { if (pane.sized_by == k) pane.sized_by = other; if (pane.pv_known & bit(other) != 0 and pane.pv[other].body.sels.len > 0) p.gpa.free(pane.pv[other].body.sels); pane.pv[other] = fresh(pane); pane.pv_known |= bit(other); }; close(p, k); } /// What view `v`'s own share holds, freed. fn freeView(p: *Pardes, v: *View) void { for (&v.col_tags) |*t| t.deinit(p.gpa); if (v.chord_arg) |a| p.gpa.free(a); } /// View `k` closed outright (not live): its share and its panes' shares freed. fn dropView(p: *Pardes, k: u8) void { if (p.views[k]) |*v| freeView(p, v); p.views[k] = null; for (p.panes) |s| if (s) |pane| { if (pane.pv_known & bit(k) != 0) { const sels = pane.pv[k].body.sels; if (sels.len > 0) p.gpa.free(sels); } pane.pv[k].body.sels = &.{}; pane.pv_known &= ~bit(k); // A name it was drafting goes with it; a terminal it sized keeps // its size (Pardes.sizes), for no view that is not typed in. if (pane.prompt == .name and pane.prompt_view == k) pane.closePrompt(); if (pane.sized_by == k) pane.sized_by = null; }; p.views_open &= ~bit(k); p.views_away &= ~bit(k); } /// View `k` gone, and all it held; the live one moves to another first. /// The last view someone looks at stays: a session without a client keeps /// its own. One kept for a rejoin (`away`) goes whenever it is closed. pub fn close(p: *Pardes, k: u8) void { if (!isOpen(p, k) or @popCount(p.views_open) == 1 or (!isAway(p, k) and count(p) == 1)) return; if (k == p.live) { const other: u8 = looked(p, k) orelse for (0..max) |i| { if (i != k and isOpen(p, @intCast(i))) break @intCast(i); } else unreachable; to(p, other); } dropView(p, k); } /// View `k`'s client gone a moment, to join again for it (an Upgrade's /// rejoin, a divergence's): the view kept as it is, layout, focus and the /// panes opened in it, until its client is back (Pardes.rejoinView) or a /// join wants its place (Pardes.openView). Dumped with the rest, so an /// Upgrade's new build has it too. The live view is never one: the session /// goes into another someone looks at, or into a copy of this one of its own. pub fn away(p: *Pardes, k: u8) void { if (!isOpen(p, k)) return; if (k == p.live) if (looked(p, k) orelse open(p)) |other| to(p, other); p.views_away |= bit(k); p.views[k].?.away_ns = p.clock.now_ns; } /// The session's end: what each view holds. pub fn deinit(p: *Pardes) void { for (&p.views) |*slot| if (slot.*) |*v| freeView(p, v); } pub const Present = struct { ctx: ?*anyopaque, f: ?*const fn (?*anyopaque, *const pardes.Surface) bool }; /// The view this process's frontend shows: the one open view that is no /// replica client's (`undrawn`) and kept for none (`away`), if there is one. pub fn shown(p: *const Pardes) ?u8 { for (0..max) |i| if (isOpen(p, @intCast(i)) and (p.undrawn | p.views_away) & bit(i) == 0) return @intCast(i); return null; } fn drawLive(drawn: *Drawn, p: *Pardes) anyerror!void { _ = p.frame_arena.reset(.{ .retain_with_limit = pardes.memory.limits.frame_kept }); const surface = try p.render(p.frame_arena.allocator()); if (drawn.present.f) |f| drawn.shown = f(drawn.present.ctx, surface); } const Drawn = struct { present: Present, shown: bool = true }; /// View `k` drawn and presented outside any step (the pump's other views, /// a replica client's own): the live view put back after exactly as it /// was, so the digest is the same. No post_present. Whether the frontend /// showed it (Host.present). pub fn drawOne(p: *Pardes, k: u8, present: Present) !bool { var drawn: Drawn = .{ .present = present }; try within(p, k, &drawn, drawLive); return drawn.shown; } /// `f(ctx, p)` with view `k` live, outside any step, and the live view put /// back after exactly as it was: how `drawOne` draws one while another /// client's input was the session's last (a frontend reads what it needs /// of its view from the frame: surface.Facts). What going into /// `k` and drawing it made of it (its layout worked out, a cursor brought /// inside a changed text) is not kept: it is done again, in every replica /// alike, when `k` is next live by an input. pub fn within(p: *Pardes, k: u8, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) !void { if (k == p.live or !isOpen(p, k)) return f(ctx, p); const live = p.live; const kept = p.views[k]; var sels: [MAX_PANES]struct { []Text.SelRange, u16 } = undefined; for (p.panes, 0..) |s, id| if (s) |pane| { sels[id] = .{ pane.pv[k].body.sels, pane.pv[k].body.nsel }; }; park(p); unpark(p, k); defer { p.views[k] = kept; p.live = live; for (p.panes, 0..) |s, id| if (s) |pane| { // `k`'s slot its selections again (else a frame drawn would // take them from a view, and a replica that draws nothing // would keep them); the live view's fields its own again, and // its slot, stale while it is live, owning nothing. pane.pv[k].body.sels = sels[id][0]; pane.pv[k].body.nsel = sels[id][1]; const pv = &pane.pv[live]; copy(&pane_fields, pane, pv); copy(&body_fields, &pane.body, &pv.body); if (!pane.isTerminal()) { pane.cols = pv.cols; pane.rows = pv.rows; } if (pane.file) |*file| { file.scroll = pv.scroll; file.scroll_row = pv.scroll_row; } pv.body.sels = &.{}; pv.body.nsel = 0; }; } try f(ctx, p); } // ---- tests ---- const testing = std.testing; const Key = pardes.Key; test "a view not live with its band open, dumped and loaded, draws before its next step: its band over its body as dumped, its tag's rows its rows at rest" { const gpa = std.testing.allocator; const dump = @import("dump.zig"); const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 30 }); defer p.deinit(); const pane = try p.setTestFile("one\n"); p.sync(); const rest = pane.tag_rows; // SPC's level unrolled in view 0: its band over the body's top rows. p.update(.{ .key = .{ .cp = ' ' } }); p.update(.{ .key = .{ .cp = Key.down } }); p.sync(); const covered = pane.view.covered; try testing.expect(covered > 0 and pane.tag_rows == rest); // Another view live, view 0 parked, as a client's is when a join's // (a replica's rollback's) dump is made. const k = p.openView().?; p.toView(k); const bytes = try dump.byteDump(p); defer gpa.free(bytes); const q = try dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 30 }, bytes, null); defer q.deinit(); try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, q)); // View 0 drawn with no step first (a client's draw, a pump's other // views): its band over its body as it was. try testing.expectEqual(covered, q.panes[0].?.pv[0].view.covered); _ = try drawOne(q, 0, .{ .ctx = null, .f = null }); // A share whose covered rows are not counted draws the same band. q.panes[0].?.pv[0].view.covered = 0; _ = try drawOne(q, 0, .{ .ctx = null, .f = null }); } test "a picker's band in a short column, dumped and loaded: its rows are the never-loaded session's, and so is the next layout" { const gpa = std.testing.allocator; const dump = @import("dump.zig"); const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer p.deinit(); p.settings.jumps = .walk; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "alpha.zig", .data = "const alpha = 1;\n" }); var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; const pane = try p.setTestFile("one\n"); gpa.free(pane.file.?.path); pane.file.?.path = try std.Io.Dir.path.join(gpa, &.{ dir, "one.txt" }); const host = p.view().active; p.newScratchBelow(host); const low = p.view().active; p.view().active = host; p.sync(); // Find in the pane itself (it reads): its head and its act row over // the band, which the short column holds under what it wants. p.stream.engine = .walk; pardes.Pick.open(p, host, .find, "", "", ""); p.sync(); const at = pardes.Band.covered(p, pane, host); try testing.expect(at.band > 0 and at.band < pardes.Band.takes(p, host)); try testing.expect(at.text >= 2); const bytes = try dump.byteDump(p); defer gpa.free(bytes); const q = try dump.load(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, bytes, null); defer q.deinit(); try testing.expectEqual(pane.view.covered, q.panes[host].?.view.covered); try testing.expectEqual(pane.tag_rows, q.panes[host].?.tag_rows); // The next step lays the column out from the band's floor (layout // floorRows, from the state alone). p.update(.{ .resize = .{ .cols = 40, .rows = 40 } }); q.update(.{ .resize = .{ .cols = 40, .rows = 40 } }); try testing.expectEqual(p.view().rects[host], q.view().rects[host]); try testing.expectEqual(p.view().rects[low], q.view().rects[low]); try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, q)); } fn typeKeys(p: *Pardes, text: []const u8) void { for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); } fn escape(p: *Pardes) void { p.update(.{ .key = .{ .cp = Key.escape } }); } fn numbered(gpa: std.mem.Allocator, n: usize) ![]u8 { var out: std.ArrayList(u8) = .empty; for (1..n + 1) |i| try out.print(gpa, "line {d}\n", .{i}); return out.toOwnedSlice(gpa); } /// The cursor put on `row`, as a jump puts it, and the step after. fn cursorTo(p: *Pardes, pane: *Pane, row: i32) void { pane.body.vsel.active = false; pane.body.nsel = 0; pane.body.cur_row = row; pane.body.cur_col = 0; pane.body.cur_pinned = true; pane.ensureCursorVisible(p.settings.tab); p.sync(); } test "each view has its own cursor and scroll, and an edit in one is in the other" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const text = try numbered(gpa, 200); defer gpa.free(text); const pane = try p.setTestFile(text); try testing.expectEqual(@as(?View.Id, .at(1)), p.openView()); cursorTo(p, pane, 119); const scrolled = pane.file.?.scroll; try testing.expect(scrolled > 0); // The other view is where the session was when it came. p.toView(.at(1)); try testing.expectEqual(@as(i32, 0), pane.body.cur_row); try testing.expectEqual(@as(usize, 0), pane.file.?.scroll); typeKeys(p, "ix"); escape(p); try testing.expectEqual(Text.Mode.normal, pane.body.mode); p.toView(.at(0)); try testing.expectEqual(@as(i32, 119), pane.body.cur_row); try testing.expectEqual(scrolled, pane.file.?.scroll); try testing.expect(std.mem.startsWith(u8, pane.file.?.content, "xline 1\n")); // Its mode is its own: insert here leaves the other in normal. typeKeys(p, "i"); try testing.expectEqual(Text.Mode.insert, pane.body.mode); p.toView(.at(1)); try testing.expectEqual(Text.Mode.normal, pane.body.mode); } // A pane made while one view is open is its share there alone; the // ranges beside its primary that a key makes are the live text's, and // the view's slot owns them once another view is live (park): freed with // the pane, or with that view. test "a text's ranges beside its primary, made while one view was open, are freed once another view has been live" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const text = try numbered(gpa, 20); defer gpa.free(text); const pane = try p.setTestFile(text); // A second cursor below, and a key typed in insert mode at both. typeKeys(p, "Cix"); escape(p); try testing.expect(pane.body.sels.len > 0); try testing.expectEqual(@as(?View.Id, .at(1)), p.openView()); p.toView(.at(1)); } test "each view has its own layout and focus over the same panes" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); _ = try p.setTestFile("one\ntwo\n"); const k = p.openView().?; const ncol = p.view().ncol; p.toView(k); p.update(.{ .command = "Newcol" }); p.update(.{ .command = "New" }); const made = p.view().active; try testing.expect(made != 0); try testing.expectEqual(ncol + 1, p.view().ncol); p.toView(.at(0)); try testing.expectEqual(ncol, p.view().ncol); try testing.expectEqual(@as(usize, 0), p.view().active); try testing.expect(layout.findPane(p, made) == null); try testing.expectEqual(pardes.Rect{}, p.view().rects[made]); // The pane is the session's: focused here, this view shows it too. p.view().active = made; p.sync(); try testing.expect(layout.findPane(p, made) != null); try testing.expect(p.view().rects[made].h > 0); // Closed here, it leaves the other view's layout, and its keyboard. try p.removePane(made, null); p.sync(); p.toView(k); try testing.expect(layout.findPane(p, made) == null); try testing.expect(p.panes[p.view().active] != null); try testing.expect(layout.findPane(p, p.view().active) != null); } test "registers are the session's: yanked in one view, put in another" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const pane = try p.setTestFile("alpha\nbeta\n"); const k = p.openView().?; typeKeys(p, "xy"); p.toView(k); cursorTo(p, pane, 1); typeKeys(p, "p"); try testing.expectEqualStrings("alpha\nbeta\nalpha\n", pane.file.?.content); } test "a terminal shown at two sizes takes the size of the view that typed into it last" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const term = p.panes[p.view().active].?; try testing.expect(term.isTerminal()); const small = .{ term.cols, term.rows }; const k = p.openView().?; p.toView(k); p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); // Shown bigger here, it keeps the size of the view that typed last. try testing.expect(p.view().rects[p.view().active].w > 80); try testing.expectEqual(small, .{ term.cols, term.rows }); p.update(.{ .key = .{ .cp = Key.right } }); try testing.expect(term.cols > small[0] and term.rows > small[1]); const big = .{ term.cols, term.rows }; p.toView(.at(0)); p.sync(); try testing.expectEqual(big, .{ term.cols, term.rows }); p.update(.{ .key = .{ .cp = Key.right } }); try testing.expectEqual(small, .{ term.cols, term.rows }); // Its pty in pixels at the cell of the view that sizes it, whichever // is live. p.views[k.index()].?.cell_pixels = .{ .w = 10, .h = 20 }; p.update(.{ .key = .{ .cp = Key.right } }); p.toView(k); p.update(.{ .key = .{ .cp = Key.right } }); p.toView(.at(0)); try testing.expectEqual(pardes.PtySize{ .cols = big[0], .rows = big[1], .w_px = big[0] * 10, .h_px = big[1] * 20 }, p.ptySize(.at(p.view().active), null)); p.update(.{ .key = .{ .cp = Key.right } }); try testing.expectEqual(pardes.PtySize{ .cols = small[0], .rows = small[1], .w_px = small[0] * 8, .h_px = small[1] * 16 }, p.ptySize(.at(p.view().active), null)); // That view gone, the size stays until the one left types into it; a // view opened in its place sizes nothing it has not typed into. p.toView(k); p.closeView(.at(0)); try testing.expectEqual(k.index(), p.live); try testing.expectEqual(small, .{ term.cols, term.rows }); const again = p.openView().?; p.toView(again); p.update(.{ .resize = .{ .cols = 100, .rows = 30 } }); try testing.expectEqual(small, .{ term.cols, term.rows }); p.toView(k); p.update(.{ .key = .{ .cp = Key.right } }); try testing.expectEqual(big, .{ term.cols, term.rows }); } test "a view closing leaves the others as they were" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); const text = try numbered(gpa, 100); defer gpa.free(text); const pane = try p.setTestFile(text); const a = p.openView().?; const b = p.openView().?; try testing.expectEqual(@as(usize, 3), count(p)); p.toView(b); p.update(.{ .command = "Newcol" }); cursorTo(p, pane, 40); typeKeys(p, "xy"); p.toView(a); cursorTo(p, pane, 70); p.toView(.at(0)); cursorTo(p, pane, 10); const ncol = p.view().ncol; // The live one closing: the session goes into another. p.toView(b); p.closeView(b); try testing.expect(p.live != b.index()); try testing.expectEqual(@as(usize, 2), count(p)); p.toView(a); try testing.expectEqual(@as(i32, 70), pane.body.cur_row); p.closeView(.at(0)); try testing.expectEqual(a.index(), p.live); try testing.expectEqual(ncol, p.view().ncol); // The last stays. p.closeView(a); try testing.expectEqual(@as(usize, 1), count(p)); try testing.expectEqual(@as(i32, 70), pane.body.cur_row); } test "the last client's view going leaves the session its layout, which a rejoin copies" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); defer p.deinit(); const text = try numbered(gpa, 100); defer gpa.free(text); const pane = try p.setTestFile(text); const k = p.openView().?; p.toView(k); p.update(.{ .resize = .{ .cols = 120, .rows = 40 } }); p.update(.{ .command = "Newcol" }); p.update(.{ .command = "New" }); const made = p.view().active; const ncol = p.view().ncol; p.view().active = 0; p.sync(); cursorTo(p, pane, 60); p.leaveView(k); try testing.expectEqual(@as(usize, 1), count(p)); try testing.expectEqual(ncol, p.view().ncol); try testing.expect(layout.findPane(p, made) != null); try testing.expectEqual(@as(i32, 60), pane.body.cur_row); try testing.expectEqual(@as(u16, 120), p.view().screen_w); // Its client back (a view copied from the live one): all of it again. const again = p.openView().?; p.toView(again); try testing.expectEqual(ncol, p.view().ncol); try testing.expect(layout.findPane(p, made) != null); try testing.expectEqual(@as(usize, 0), p.view().active); try testing.expectEqual(@as(i32, 60), pane.body.cur_row); } test "a client's view kept for its rejoin: through an Upgrade's dump, each client back in its own; a join with no room lets one go only once it has been kept away_kept_ns" { const gpa = testing.allocator; const opts: pardes.Options = .{ .tty_only = true, .cols = 100, .rows = 30 }; const p = try Pardes.init(gpa, opts); defer p.deinit(); _ = try p.setTestFile("one\ntwo\n"); // Two clients, each a pane of its own in its own view. const ks: [2]View.Id = .{ p.openView().?, p.openView().? }; var made: [2]usize = undefined; for (ks, &made) |k, *m| { p.toView(k); p.update(.{ .command = "New" }); m.* = p.view().active; } // Both let go to upgrade, the live view one of theirs: theirs are kept, // and the session's own is live. for (ks) |k| p.awayView(k); try testing.expectEqual(@as(usize, 1), count(p)); try testing.expect(!isAway(p, p.live)); // The new build loads the dump; each client is back in its own view. const bytes = try pardes.dump.dumpBytes(p); defer gpa.free(bytes); const q = try pardes.dump.load(gpa, opts, bytes, null); defer q.deinit(); for (ks, made, 0..) |k, m, i| { try testing.expect(isAway(q, k.index())); try testing.expect(q.rejoinView(k)); q.toView(k); try testing.expectEqual(m, q.view().active); try testing.expect(layout.findPane(q, m) != null); try testing.expect(layout.findPane(q, made[1 - i]) == null); } try testing.expect(!q.rejoinView(ks[0])); // A client that said it joins again is coming back (a far one's join // for its lag waits seconds for its dump): a join with no room is // refused, its view not let go, until `away_kept_ns` has passed. p.toView(.at(0)); try testing.expectEqual(@as(?View.Id, .at(3)), p.openView()); try testing.expectEqual(@as(?View.Id, null), p.openView()); p.advance(p.clock.now_ns + away_kept_ns - 1); try testing.expectEqual(@as(?View.Id, null), p.openView()); try testing.expect(isAway(p, ks[0].index()) and isAway(p, ks[1].index())); // One that never comes back: its view goes once the bound has passed. p.advance(p.clock.now_ns + 1); try testing.expectEqual(ks[0], p.openView()); try testing.expect(!isAway(p, ks[0].index()) and isAway(p, ks[1].index())); } test "drawing another view leaves the live one as it was, to the digest" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const text = try numbered(gpa, 100); defer gpa.free(text); const pane = try p.setTestFile(text); const k = p.openView().?; p.toView(k); cursorTo(p, pane, 60); typeKeys(p, "dd"); p.toView(.at(0)); cursorTo(p, pane, 95); const before = pardes.walk.digest(Pardes, p); _ = try drawOne(p, k.index(), .{ .ctx = null, .f = null }); try testing.expectEqual(@as(u8, 0), p.live); try testing.expectEqual(before, pardes.walk.digest(Pardes, p)); p.toView(k); try testing.expectEqual(@as(i32, 60), pane.body.cur_row); } test "a view drawn while another is live keeps its own cursors" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const pane = try p.setTestFile("ab\ncd\nef\n"); const k = p.openView().?; p.toView(k); p.update(.{ .key = .{ .cp = 'C', .text = "C" } }); try testing.expectEqual(@as(u16, 1), pane.body.nsel); p.toView(.at(0)); const before = pardes.walk.digest(Pardes, p); _ = try drawOne(p, k.index(), .{ .ctx = null, .f = null }); try testing.expectEqual(before, pardes.walk.digest(Pardes, p)); p.toView(k); try testing.expectEqual(@as(u16, 1), pane.body.nsel); } test "a view drawn after another view's step moved the top into a long wrapped line, the two alike before it: drawn from its own top, line 0, not from the row the step found in the other's line" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); // A short first line, and line 10 wrapped over more rows than the body. var text: std.ArrayList(u8) = .empty; defer text.deinit(gpa); try text.appendSlice(gpa, "short\n"); for (1..10) |i| try text.print(gpa, "line {d}\n", .{i}); for (0..500) |w| try text.print(gpa, "w{d:0>4} ", .{w}); try text.appendSlice(gpa, "\nlast\n"); const pane = try p.setTestFile(text.items); const k = p.openView().?; // View 0's step puts the cursor at line 10's end: the top goes into // that line's rows, a row far past line 0's five bytes. View k is // where view 0 was: its top line 0, its size view 0's. pane.body.cur_row = 10; pane.body.cur_col = @intCast(pardes.panes.File.sourceLine(pane, 10).len - 1); pane.body.cur_pinned = true; pane.ensureCursorVisible(p.settings.tab); p.sync(); try testing.expectEqual(@as(usize, 10), pane.file.?.scroll); try testing.expect(pane.file.?.scroll_row > 0); const Seen = struct { short: bool = false, fn present(ctx: ?*anyopaque, s: *const pardes.Surface) bool { const seen: *@This() = @ptrCast(@alignCast(ctx.?)); var row: [512]u8 = undefined; for (0..s.rows) |y| { var n: usize = 0; for (s.cells[y * s.cols ..][0..s.cols]) |c| { if (n + c.len > row.len) break; @memcpy(row[n..][0..c.len], c.text[0..c.len]); n += c.len; } if (std.mem.indexOf(u8, row[0..n], "short") != null) seen.short = true; } return true; } }; var seen: Seen = .{}; _ = try drawOne(p, k.index(), .{ .ctx = &seen, .f = Seen.present }); try testing.expect(seen.short); } test "a view's step after another view's step moved the top, the two alike before it: it scrolls from its own top's row, as a core loaded from the session (none of its geometry) does" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); // Line 3 wrapped over several rows, line 10 over more than the body. var text: std.ArrayList(u8) = .empty; defer text.deinit(gpa); for (0..12) |i| switch (i) { 3, 10 => { for (0..@as(usize, if (i == 3) 60 else 500)) |w| try text.print(gpa, "w{d:0>4} ", .{w}); try text.append(gpa, '\n'); }, else => try text.print(gpa, "line {d}\n", .{i}), }; const pane = try p.setTestFile(text.items); const f = &pane.file.?; // Both views' top on line 3's third row, the cursor on it. pane.body.cur_row = 3; pane.body.cur_col = 150; f.scroll = 3; f.scroll_row = 2; p.sync(); const k = p.openView().?; // View 0's step: the cursor to line 10's end, the top into its rows. pane.body.cur_row = 10; pane.body.cur_col = @intCast(pardes.panes.File.sourceLine(pane, 10).len - 1); pane.body.cur_pinned = true; pane.ensureCursorVisible(p.settings.tab); p.sync(); try testing.expect(f.scroll == 10 and f.scroll_row > 2); // The session as a client joining it loads it: no geometry made. const bytes = try pardes.dump.dumpBytes(p); defer gpa.free(bytes); const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); defer loaded.deinit(); // View k's PageDown in each, from line 3's third row. for ([_]*Pardes{ p, loaded }) |core| { core.toView(k); core.update(.{ .key = .{ .cp = pardes.Key.page_down } }); } const lf = &loaded.panes[0].?.file.?; try testing.expectEqual(lf.scroll, f.scroll); try testing.expectEqual(lf.scroll_row, f.scroll_row); } test "a view's top on a wrapped line's third row, the line taken out by another view and the next one empty: its top row past its line's, read as its last, and the session's dump loads as the session" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); var text: std.ArrayList(u8) = .empty; defer text.deinit(gpa); for (0..30) |i| switch (i) { 3 => { for (0..60) |w| try text.print(gpa, "w{d:0>4} ", .{w}); try text.append(gpa, '\n'); }, 4 => try text.append(gpa, '\n'), else => try text.print(gpa, "line {d}\n", .{i}), }; const pane = try p.setTestFile(text.items); const f = &pane.file.?; // View k: its top on line 3's third row, its cursor well inside the body. pane.body.cur_row = 10; f.scroll = 3; f.scroll_row = 2; p.sync(); const k = p.openView().?; // View 0 takes line 3 out: view k's top line is the empty one after it. cursorTo(p, pane, 3); typeKeys(p, "xd"); p.toView(k); // A step of view k's own: its cursor inside the band, its top kept. escape(p); try testing.expectEqual(@as(usize, 3), f.scroll); try testing.expectEqual(@as(usize, 0), pardes.panes.File.sourceLine(pane, 3).len); try testing.expectEqual(@as(u32, 2), f.scroll_row); try testing.expectEqual(pardes.panes.File.Top{ .line = 3, .row = 0 }, pardes.panes.File.top(p.settings.tab, pane)); // A client joining loads the session's dump as the session. const bytes = try pardes.dump.dumpBytes(p); defer gpa.free(bytes); const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); defer loaded.deinit(); try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, loaded)); } test "a view not live keeps its place in a text another view edits" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const text = try numbered(gpa, 200); defer gpa.free(text); const pane = try p.setTestFile(text); const k = p.openView().?; p.toView(k); cursorTo(p, pane, 100); const scrolled = pane.file.?.scroll; p.toView(.at(0)); // Three lines put above it, one taken below it. typeKeys(p, "xyppp"); cursorTo(p, pane, 150); typeKeys(p, "xd"); p.toView(k); try testing.expectEqual(@as(i32, 103), pane.body.cur_row); try testing.expectEqual(scrolled + 3, pane.file.?.scroll); try testing.expect(std.mem.startsWith(u8, pardes.panes.File.sourceLine(pane, 103), "line 101")); } test "a terminal shown in a view shorter than it keeps its cursor in sight" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 20 }); defer p.deinit(); const id = p.view().active; const term = p.panes[id].?; const k = p.openView().?; p.toView(k); p.update(.{ .resize = .{ .cols = 100, .rows = 50 } }); p.update(.{ .key = .{ .cp = Key.right } }); const rows = term.rows; try testing.expect(rows > 30); var out: std.ArrayList(u8) = .empty; 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 = .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)); p.sync(); try testing.expectEqual(rows, term.rows); try testing.expect(term.pan > 0); var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); const s = try p.render(arena.allocator()); const r = p.view().rects[id]; // The cursor's row, the last, is on screen in the short view, inside // the pane, and so is the line it is on. const cursor = s.cursor.?; try testing.expect(cursor.y >= r.y and cursor.y < r.y + r.h); var row_text: std.ArrayList(u8) = .empty; defer row_text.deinit(gpa); for (0..s.cols) |x| try row_text.appendSlice(gpa, s.cells[@as(usize, cursor.y) * s.cols + x].grapheme()); try testing.expect(std.mem.indexOf(u8, row_text.items, "LAST$") != null); // Back in the tall view, its top is where it was. p.toView(k); try testing.expectEqual(@as(u16, 0), term.pan); } test "each view has its own place in a terminal's history: a wheel in one scrolls it back, the other stays on the output and follows it" { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const id = p.view().active; const term = p.panes[id].?; var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); for (0..100) |i| try out.print(gpa, "line-{d}\r\n", .{i}); p.update(.{ .output = .{ .pane = .at(id), .bytes = out.items } }); p.sync(); const bottom = term.scroll(); try testing.expect(bottom > 0); const k = p.openView().?; p.toView(k); const r = p.view().rects[id]; const at = .{ .col = r.x + r.w / 2, .row = p.bodyTop(term, r) + 1 }; for (0..3) |_| p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = at.col, .row = at.row } }); p.sync(); const back = term.scroll(); try testing.expect(back < bottom); // The session's view is where it was: on the output. p.toView(.at(0)); p.sync(); try testing.expectEqual(bottom, term.scroll()); // Output comes: the view on the output follows it, the one scrolled // back stays on its row. p.update(.{ .output = .{ .pane = .at(id), .bytes = "more-1\r\nmore-2\r\n" } }); p.sync(); try testing.expectEqual(bottom + 2, term.scroll()); p.toView(k); p.sync(); try testing.expectEqual(back, term.scroll()); // A session's dump keeps each view's place. const bytes = try pardes.dump.dumpBytes(p); defer gpa.free(bytes); const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); defer loaded.deinit(); try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, loaded)); const lt = loaded.panes[id].?; loaded.toView(k); try testing.expectEqual(back, lt.scroll()); loaded.toView(.at(0)); try testing.expectEqual(bottom + 2, lt.scroll()); } test "a session's dump keeps its views: loaded, each where it was; revived for one client, the live one alone" { const gpa = testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const text = try numbered(gpa, 200); defer gpa.free(text); const pane = try p.setTestFile(text); const id = for (p.panes, 0..) |s, i| { if (s == pane) break i; } else unreachable; const k = p.openView().?; cursorTo(p, pane, 119); p.toView(k); p.update(.{ .command = "Newcol" }); p.toView(.at(0)); p.toView(k); cursorTo(p, pane, 30); const ncol = p.view().ncol; const bytes = try pardes.dump.dumpBytes(p); defer gpa.free(bytes); const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null); defer loaded.deinit(); try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, loaded)); const again = try pardes.dump.dumpBytes(loaded); defer gpa.free(again); try testing.expectEqualSlices(u8, bytes, again); const lp = loaded.panes[id].?; loaded.toView(.at(0)); try testing.expectEqual(@as(i32, 119), lp.body.cur_row); try testing.expectEqual(ncol - 1, loaded.view().ncol); loaded.toView(k); try testing.expectEqual(@as(i32, 30), lp.body.cur_row); try testing.expectEqual(ncol, loaded.view().ncol); // For one client: the view live when dumped, as view 0. const one = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes); defer one.deinit(); try testing.expectEqual(@as(usize, 1), count(one)); try testing.expectEqual(@as(u8, 0), one.live); try testing.expectEqual(ncol, one.view().ncol); try testing.expectEqual(@as(i32, 30), one.panes[id].?.body.cur_row); try testing.expectEqual(@as(?View.Id, .at(1)), one.openView()); } test "a pane's id and a view's id are two types: neither is taken for the other, nor for an integer" { // An enum coerces into nothing but itself (an enum literal into it, it // into a union of it): two distinct enum types never stand for each // other, so `p.toView(pane_id)` or `p.setMessage(view_id, ..)` does not // compile. What is left to show is that they are two, and a byte each. comptime { std.debug.assert(Pane.Id != View.Id); for (.{ Pane.Id, View.Id }) |T| { const e = @typeInfo(T).@"enum"; std.debug.assert(e.tag_type == u8 and e.mode == .nonexhaustive and @sizeOf(T) == 1); } } for (0..MAX_PANES) |i| try testing.expectEqual(i, Pane.Id.at(i).index()); for (0..max) |k| try testing.expectEqual(k, View.Id.at(k).index()); }