diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/replica.zig | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/core/replica.zig b/core/replica.zig index fd691e04..479ee7c6 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -5246,3 +5246,354 @@ test "a client's n in a kind's pane is a motion: it steps to the next place a re try std.testing.expectEqual(@as(u32, 1), r.stats.fast); try agrees(a, r); } + +// ---- the line a far client draws: what may wait, and nothing else ---- + +/// The kinds of pane the walk below stands in: a file's, a plain kind's, +/// a shell's raw (its program has the keys) and a shell's in normal mode +/// (pardes's own keys over its history). `any` is a row's for all four. +const On = enum { any, file, kind, shell, shell_normal }; + +/// What a far client may wait a round trip for (the user's line, P14: a +/// side effect on the system may be slow; motions, edits and layout are +/// instant): a word (`Look`), a key (`C-x`, `A-x`, `g x`: after a prefix), +/// or a gesture (`click shell`), on a kind of pane, the stop's name +/// (`Tape.poisoned_by`), and why. A `*` in a row takes any text there. +/// The test below walks every word of the registry, every key and the +/// gestures between panes against it: what stops and is not here fails +/// it, and a row nothing stopped at says it is stale. +const may_wait = [_]struct { what: []const u8, on: On, stop: []const u8, why: []const u8 }{ + // A shell's program has the keys: what reaches it is its input (a far + // shell may feel like ssh). + .{ .what = "*", .on = .shell, .stop = "pty_write", .why = "a key into a shell's program" }, + // The focus report (DECSET 1004): a pane switch into or out of a shell + // whose program asked tells it, a pty write at the end of `update`. + .{ .what = "*", .on = .file, .stop = "pty_write", .why = "the focus report to the shell beside" }, + .{ .what = "*", .on = .kind, .stop = "pty_write", .why = "the focus report to the shell beside" }, + .{ .what = "*", .on = .shell_normal, .stop = "pty_write", .why = "the focus report to the shell" }, + // A file from the session's disk (P14). + .{ .what = "Look *", .on = .any, .stop = "resolve", .why = "a name looked for on the session's disk" }, + .{ .what = "LookNext", .on = .any, .stop = "resolve", .why = "a Look's walk: each word asked of the disk" }, + .{ .what = "LookPrev", .on = .any, .stop = "resolve", .why = "a Look's walk: each word asked of the disk" }, + .{ .what = "Mini *", .on = .any, .stop = "resolve", .why = "a file opened by name, from the session's disk" }, + .{ .what = "Get", .on = .any, .stop = "read_file", .why = "the file read again from the session's disk" }, + .{ .what = "Save", .on = .any, .stop = "write_file", .why = "a save" }, + // A program run, a shell opened or closed: the session's processes. + .{ .what = "Exec *", .on = .any, .stop = "kind", .why = "a program run: its name asked of the disk first" }, + .{ .what = "Exec *", .on = .any, .stop = "tty_taken", .why = "a program run in a shell: whether one has it, the session's" }, + .{ .what = "Term", .on = .any, .stop = "kind", .why = "a shell opened: its name asked of the disk first" }, + .{ .what = "A-n", .on = .any, .stop = "open a terminal", .why = "a shell opened" }, + .{ .what = "* A-n", .on = .any, .stop = "open a terminal", .why = "a shell opened (Alt-n after a prefix is still the global key)" }, + .{ .what = "Del", .on = .any, .stop = "close a terminal", .why = "a shell closed: its pty and emulator are the session's" }, + .{ .what = "DelAbove", .on = .any, .stop = "close a terminal", .why = "a shell closed" }, + .{ .what = "DelBelow", .on = .any, .stop = "close a terminal", .why = "a shell closed" }, + .{ .what = "Delcol", .on = .any, .stop = "close a terminal", .why = "a shell closed with its column" }, + .{ .what = "SPC d", .on = .any, .stop = "close a terminal", .why = "Del, a shell closed" }, + .{ .what = "C-w d", .on = .any, .stop = "close a terminal", .why = "Del, a shell closed" }, + // The session's own: it dumps, quits, execs itself, lets its frontend go. + .{ .what = "Exit", .on = .any, .stop = "write_dump", .why = "the session's: it dumps and quits" }, + .{ .what = "SPC q", .on = .any, .stop = "write_dump", .why = "Exit" }, + .{ .what = "Dump", .on = .any, .stop = "write_dump", .why = "a dump written" }, + .{ .what = "Upgrade", .on = .any, .stop = "upgrade", .why = "the session execs itself" }, + .{ .what = "Detach", .on = .any, .stop = "detach", .why = "the session lets its frontend go" }, + .{ .what = "focus lost and found", .on = .any, .stop = "newer_build", .why = "a window found again asks whether a newer build is installed: the session's disk" }, + // What the session alone holds: its closed panes, Recent and History + // (every session of its user writes them), the config and theme files. + .{ .what = "Restore", .on = .any, .stop = "the authority's own state", .why = "the session's dumps" }, + .{ .what = "Panes", .on = .any, .stop = "the authority's own state", .why = "the session's closed panes" }, + .{ .what = "Recent", .on = .any, .stop = "the authority's own state", .why = "Recent, the user's" }, + .{ .what = "History", .on = .any, .stop = "the authority's own state", .why = "History, the user's" }, + .{ .what = "Plumb", .on = .any, .stop = "the authority's own state", .why = "the plumbing rules, the session's file" }, + .{ .what = "Config", .on = .any, .stop = "the authority's own state", .why = "the config file, the session's" }, + .{ .what = "DumpConfig", .on = .any, .stop = "the authority's own state", .why = "the config file written" }, + .{ .what = "ThemeFile *", .on = .any, .stop = "the authority's own state", .why = "a theme file, the session's" }, + .{ .what = "DumpThemes", .on = .any, .stop = "the authority's own state", .why = "the theme files written" }, +}; + +/// The keys the walk types: every printable one, and the specials a +/// frontend reads; each plain, with Ctrl and with Alt. +const walk_keys = keys: { + @setEvalBranchQuota(20000); + var ks: []const pardes.Key = &.{}; + for (0x20..0x7f) |c| ks = ks ++ .{pardes.Key{ .cp = c, .text = &[1]u8{c} }}; + const K = pardes.Key; + for ([_]u21{ K.enter, K.escape, K.tab, K.backspace, K.up, K.down, K.left, K.right, K.home, K.end, K.page_up, K.page_down, K.delete, K.insert }) |cp| ks = ks ++ .{pardes.Key{ .cp = cp }}; + for (0..12) |n| ks = ks ++ .{pardes.Key{ .cp = K.f1 + n }}; + var all: []const pardes.Key = ks; + for (ks) |k| all = all ++ .{pardes.Key{ .cp = k.cp, .ctrl = true }}; + for (ks) |k| all = all ++ .{pardes.Key{ .cp = k.cp, .alt = true }}; + break :keys all; +}; + +/// Every prefix key the normal mode has (config's `*_prefix`), the leader +/// and Ctrl-w: each is typed before every key of `walk_keys`. +const walk_prefixes = prefixes: { + @setEvalBranchQuota(200000); + const config = pardes.config; + var ks: []const pardes.Key = &.{}; + for (@typeInfo(config).@"struct".decl_names) |d| { + if (std.mem.indexOf(u8, d, "_prefix") == null and !std.mem.eql(u8, d, "leader")) continue; + const v = @field(config, d); + if (@TypeOf(v) == comptime_int) { + ks = ks ++ .{pardes.Key{ .cp = v, .text = &[1]u8{v} }}; + } else if (@TypeOf(v) == []const config.Chord) { + const c = v[0]; + ks = ks ++ .{pardes.Key{ .cp = c.cp, .ctrl = c.ctrl, .alt = c.alt, .text = if (c.ctrl or c.alt) "" else &[1]u8{c.cp} }}; + } + } + break :prefixes ks; +}; + +/// The argument a word that means nothing without one is walked with. +const walk_samples = [_]struct { []const u8, []const u8 }{ + .{ "Look", "mise.toml" }, .{ "Exec", "echo" }, .{ "Mount", "x" }, .{ "Unmount", "x" }, + .{ "Msg", "hi" }, .{ "ThemeFile", "t.zon" }, .{ "Edit", "s/a/b/" }, .{ "Mini", "x" }, + .{ "Find", "x" }, .{ "Grep", "x" }, .{ "Rename", "x" }, .{ "WsSymbols", "x" }, + .{ "Theme", "acme" }, .{ "Font", "Mono 12" }, .{ "TaglineSize", "100" }, .{ "WindowOpacity", "100" }, + .{ "MessageLinger", "1000" }, .{ "InactiveDim", "0" }, .{ "GripWidth", "100" }, +}; + +fn keyName(buf: []u8, k: pardes.Key) []const u8 { + const K = pardes.Key; + const mod: []const u8 = if (k.ctrl) "C-" else if (k.alt) "A-" else ""; + const special: ?[]const u8 = switch (k.cp) { + ' ' => "SPC", + K.enter => "Enter", + K.escape => "Esc", + K.tab => "Tab", + K.backspace => "BS", + K.up => "Up", + K.down => "Down", + K.left => "Left", + K.right => "Right", + K.home => "Home", + K.end => "End", + K.page_up => "PgUp", + K.page_down => "PgDn", + K.delete => "Del", + K.insert => "Ins", + else => null, + }; + if (special) |s| return std.fmt.bufPrint(buf, "{s}{s}", .{ mod, s }) catch unreachable; + if (k.cp >= K.f1 and k.cp < K.f1 + 25) return std.fmt.bufPrint(buf, "{s}F{d}", .{ mod, k.cp - K.f1 + 1 }) catch unreachable; + return std.fmt.bufPrint(buf, "{s}{u}", .{ mod, k.cp }) catch unreachable; +} + +/// A session's host with every entry a step may ask, each doing nothing: +/// a client's step stops, or goes on, as against a real session's (a +/// client's tape offers only the entries its session's host has). The +/// loop's own (`now`, `wait_input`, the frame's) no step asks. +const FullHost = struct { + fn Noop(comptime F: type) type { + const info = @typeInfo(@typeInfo(@typeInfo(F).optional.child).pointer.child).@"fn"; + const R = info.return_type.?; + const P = info.param_types; + return switch (P.len) { + 1 => struct { + fn f(_: ?*anyopaque) R { + return std.mem.zeroes(R); + } + }, + 2 => struct { + fn f(_: ?*anyopaque, _: P[1].?) R { + return std.mem.zeroes(R); + } + }, + 3 => struct { + fn f(_: ?*anyopaque, _: P[1].?, _: P[2].?) R { + return std.mem.zeroes(R); + } + }, + 4 => struct { + fn f(_: ?*anyopaque, _: P[1].?, _: P[2].?, _: P[3].?) R { + return std.mem.zeroes(R); + } + }, + 5 => struct { + fn f(_: ?*anyopaque, _: P[1].?, _: P[2].?, _: P[3].?, _: P[4].?) R { + return std.mem.zeroes(R); + } + }, + else => @compileError("a host entry of more arguments"), + }; + } + const vtable = table: { + var v: pardes.Host.VTable = .{}; + for (@typeInfo(pardes.Host.VTable).@"struct".field_names) |name| { + if (std.mem.eql(u8, name, "now") or std.mem.eql(u8, name, "wait_input") or std.mem.eql(u8, name, "present") or + std.mem.eql(u8, name, "post_present") or std.mem.eql(u8, name, "poll_frame")) continue; + @field(v, name) = &Noop(@FieldType(pardes.Host.VTable, name)).f; + } + break :table v; + }; + fn host() pardes.Host { + return .{ .vtable = &vtable }; + } +}; + +/// One pane's walk: each input (or few) applied by the client at once, or +/// stopped, then taken back, so the next starts where it did. +const Walk = struct { + r: *Replica, + on: On, + seen: *[may_wait.len]bool, + hit: *[may_wait.len]bool, + unlisted: u32 = 0, + tried: u32 = 0, + + /// `pattern` with at most one `*`, which takes any text. + fn matches(pattern: []const u8, what: []const u8) bool { + const star = std.mem.indexOfScalar(u8, pattern, '*') orelse return std.mem.eql(u8, pattern, what); + return what.len >= pattern.len - 1 and std.mem.startsWith(u8, what, pattern[0..star]) and std.mem.endsWith(u8, what, pattern[star + 1 ..]); + } + + /// The name of what stopped the inputs, "" when none did. + fn try_(w: *Walk, what: []const u8, evs: []const pardes.Event) !void { + const r = w.r; + w.tried += 1; + r.stats.stops = @splat(.{}); + const before = r.stats.stopped; + for (evs) |ev| _ = try r.input(ev); + const stopped = r.stats.stopped != before; + // Taken back: the copy put back, nothing pending. + r.toConfirmed(); + for (r.pending.items) |p| r.freePending(p); + r.pending.clearRetainingCapacity(); + r.blocked = null; + r.dirty = false; + if (r.diverged) |why| { + std.debug.print("may_wait: `{s}` on {t}: the client failed: {s}\n", .{ what, w.on, why }); + return error.Diverged; + } + for (may_wait, 0..) |row, i| if ((row.on == .any or row.on == w.on) and matches(row.what, what)) { + w.seen[i] = true; + }; + if (!stopped) return; + for (r.stats.stops) |st| if (st.n > 0) { + const listed = for (may_wait, 0..) |row, i| { + if ((row.on == .any or row.on == w.on) and matches(row.what, what) and std.mem.eql(u8, row.stop, st.name)) { + w.hit[i] = true; + break true; + } + } else false; + if (!listed) { + w.unlisted += 1; + std.debug.print("may_wait: `{s}` on {t} stopped at `{s}`: far away it waits a round trip; keep its step off the device, or list it in may_wait with why\n", .{ what, w.on, st.name }); + } + }; + } +}; + +test "a far client's every word and key, on a file's pane, a kind's and a shell's, and the gestures between them beside a shell that asked for focus events: what waits is in may_wait, with why, and nothing else waits" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 120, .rows = 40, .boot = .tty }, FullHost.host(), true); + defer a.deinit(); + var cwd: [std.Io.Dir.max_path_bytes]u8 = undefined; + var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; + const dir = std.mem.span(@as([*:0]u8, @ptrCast(std.c.getcwd(&cwd, cwd.len) orelse return error.NoCwd))); + // The shell the session booted (none without a terminal), its program + // asking for focus events; a file; a plain kind's pane. + const shell: ?usize = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break id; + } else null; + if (shell) |id| { + a.output(.{ .output = .{ .pane = @intCast(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(.{ .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); + + var seen: [may_wait.len]bool = @splat(false); + var hit: [may_wait.len]bool = @splat(false); + var unlisted: u32 = 0; + var tried: u32 = 0; + const Builtin = pardes.builtins.registry.Builtin(); + const stands = [_]struct { On, ?usize }{ .{ .file, file }, .{ .kind, kind }, .{ .shell, shell }, .{ .shell_normal, shell } }; + for (stands) |stand| { + const on, const at = stand; + const id = at orelse continue; + a.core.view().active = id; + if (on == .shell_normal) _ = try a.local(.{ .key = .{ .cp = a.core.opts.tty_toggle, .ctrl = true } }); + defer if (on == .shell_normal) { + a.core.view().active = id; + _ = a.local(.{ .key = .{ .cp = a.core.opts.tty_toggle, .ctrl = true } }) catch {}; + }; + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try joinedFresh(a); + defer r.deinit(); + r.core().kinds.ops[r.core().kinds.byName("still").?] = &Still.ops; + try deliver(a, &screens, peer, r, .flush); + r.settle(); + try std.testing.expectEqual(id, r.core().view().active); + if (on == .shell) try std.testing.expect(pardes.Term.modes(r.core().panes[id].?.terminal).focus); + var w: Walk = .{ .r = r, .on = on, .seen = &seen, .hit = &hit }; + // Every word, with a sample argument where one is required. + for (std.enums.values(Builtin)) |b| { + const name = @tagName(b); + const arg: ?[]const u8 = for (walk_samples) |s| { + if (std.mem.eql(u8, s[0], name)) break s[1]; + } else null; + if (pardes.builtins.registry.requiresArg(b) and arg == null) { + std.debug.print("may_wait: `{s}` requires an argument: give it one in walk_samples\n", .{name}); + return error.NoSample; + } + const cmd = if (arg) |x| try std.fmt.bufPrint(&line, "{s} {s}", .{ name, x }) else name; + try w.try_(cmd, &.{.{ .command = cmd }}); + } + // Every key, plain, with Ctrl and with Alt, and after each prefix. + var name_buf: [64]u8 = undefined; + for (walk_keys) |k| try w.try_(keyName(&name_buf, k), &.{.{ .key = k }}); + for (walk_prefixes) |pk| { + var pre_buf: [16]u8 = undefined; + const pre = keyName(&pre_buf, pk); + for (walk_keys) |k| { + var both: [96]u8 = undefined; + try w.try_(try std.fmt.bufPrint(&both, "{s} {s}", .{ pre, keyName(&name_buf, k) }), &.{ .{ .key = pk }, .{ .key = k } }); + } + } + // The gestures between panes: a click on each pane's body, each + // pane's grip dragged up two rows, the window's keyboard lost and + // lost and found again. + const v = r.core().view(); + for ([_]struct { []const u8, ?usize }{ .{ "file", file }, .{ "kind", kind }, .{ "shell", shell } }) |target| { + const tname, const tid = target; + const t = tid orelse continue; + const rect = v.rects[t]; + const body_row = rect.y + r.core().panes[t].?.tag_rows + 1; + const click = [_]pardes.Event{ + .{ .mouse = .{ .button = .left, .kind = .press, .col = rect.x + 2, .row = body_row } }, + .{ .mouse = .{ .button = .left, .kind = .release, .col = rect.x + 2, .row = body_row } }, + }; + try w.try_(try std.fmt.bufPrint(&line, "click {s}", .{tname}), &click); + const up = rect.y -| 2; + const grip = [_]pardes.Event{ + .{ .mouse = .{ .button = .left, .kind = .press, .col = rect.x, .row = rect.y } }, + .{ .mouse = .{ .button = .left, .kind = .drag, .col = rect.x, .row = up } }, + .{ .mouse = .{ .button = .left, .kind = .release, .col = rect.x, .row = up } }, + }; + try w.try_(try std.fmt.bufPrint(&line, "grip {s}", .{tname}), &grip); + } + try w.try_("focus lost", &.{.{ .focus = false }}); + try w.try_("focus lost and found", &.{ .{ .focus = false }, .{ .focus = true } }); + unlisted += w.unlisted; + tried += w.tried; + } + // A row is stale where every kind of pane was walked: with no terminal + // compiled in (core-test), a shell's rows stop nothing. + var stale: u32 = 0; + if (shell != null) for (may_wait, seen, hit) |row, s, h| if (s and !h) { + stale += 1; + std.debug.print("may_wait: the row `{s}` on {t} at `{s}` is stale: nothing walked stopped there (delete it)\n", .{ row.what, row.on, row.stop }); + }; + if (unlisted + stale > 0) std.debug.print("may_wait: {d} inputs walked, {d} stopped unlisted, {d} rows stale\n", .{ tried, unlisted, stale }); + try std.testing.expectEqual(@as(u32, 0), unlisted); + try std.testing.expectEqual(@as(u32, 0), stale); +} |
