From 0919fcef12f973d3129b32f79a331ff8e45cb7ac Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 25 Nov 2026 00:24:12 -0300 Subject: Command: `:` opens a command line in the focused pane's tag, its rows the tag's own words then the commands a helix hand types, each the pardes word it runs (walk UI, pick-ui SPEC.md §8, item 1 of line E2): `:` from a body is a text prompt of its own (Prompt.zig's `.command`, its state Command.zig in TagState.prompt.command), drawn after a `:` key cell in the tag's head, the plain word the line is (`= Exec Save`, `= Exec Theme acme`, `= Look :12`, `= Exec Edit |sort`), the count (`16 commands`, or what a command takes: ``, `⏎ runs it`); its rows in the band: the tag's own words in the tag's order (its text after the prefix but `|`, then `Kill` for a terminal whose tag has none), each `in the tag`, the last underlined as the rule off the rest, then `write w`, `quit q`, `theme`, `set`, `sort`, `reflow`, `goto g`, `open o`, `pipe`, `reload`, `toggle` with what each takes on the right (``, ``, ``, ...), filtered by the field's first word with the query pane's scorer (Names.match) in their own order, the query's bytes in the match tint, eleven around the current one; beside them the current row's doc (a builtin's from the registry, a command's own, naming the word it runs); Tab and S-Tab write the next or the last row into the field, a command with a space after it, the rows staying the line's from before so Tab steps on through them (a click on a row the same, TagState.point); Enter keeps the line in `:`'s history (Pardes.prompt_history.command) and register `:`, puts the tag at rest and runs it as B2 runs a word (exec_cmd, look_cmd): `write [path]` Save, `quit` Del, `theme X` Theme X, `set W v` the setting's word with its value (any case typed), `toggle W` the word bare, `reload` Get, `goto N` Look `:N`, `open F` Look F, `sort`, `reflow [N]` and `pipe cmd` each selection piped as `|` does (`sort`, `fmt -w N`, 80 by default; Pipe.submit, a file's text only), anything else the line Exec'd (a builtin by name, else a command pane); what cannot run is said (`goto: a line number`, `set: no such setting`); `:` on the empty field gives the keyboard to the tag's own text as `:` did before (tagline.enterTag), and `:` there gives it back; `lsp-restart` is not a command: pardes has no word that restarts a language server; Esc in a tag keeps today's meaning (Last): four tests pin it after a click in a tag (tty collapse, tty tagcmd's rename, mouse's double-click, mode's tag Exec); THE TEST SITES that typed `:` from a body to reach the tag now type `::` (44): core/Pipe.zig "| in a tag pipes the tag's own text", test/panes.zig "paste into pane commands and prompts" and "a tag that exactly fills its row", test/behaviour/tags.zig 24 (lines 28 41 53 66 84 93 95 99 108 122 128 137 139 143 156 158 160 162 173 182 194 223 253 440), test/behaviour/keys.zig 41 271 373, test/behaviour/panes.zig 253, test/process/tty_editing.zig 112 1376 1392 2379 2393 2436 2453 2459 2540 2579, tty_terminals.zig 429 453, tty_layout.zig 439; those typing `:` in a tag to come back, or window mode's `:`, are as they were; golden ttytaken: the echoed command two cells to the right (the `:` band takes the terminal's rows for a step and gives them back, so its pty shrinks and grows and bash redraws its line: A's band pushes a terminal's body as any pane's); TEST FIRST: Command.zig ": opens a field in the tag after its key cell ..." and ": on the empty field gives the keyboard to the tag's own text ..." (on the base `:` gives the tag the keyboard: no prompt); core-test 1021/1188 (167 skipped), pane-test 182/184, behaviour-test 274/277 (the failures A's, as on E's tip), shell-test 1235/1238, process-test the base's five (find, builtins, leader, tagnav, ttyhelp) and none of mine Co-Authored-By: Claude Opus 5.5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Command.zig | 529 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 529 insertions(+) create mode 100644 core/Command.zig (limited to 'core/Command.zig') diff --git a/core/Command.zig b/core/Command.zig new file mode 100644 index 00000000..6f781b51 --- /dev/null +++ b/core/Command.zig @@ -0,0 +1,529 @@ +//! `:` (walk UI, pick-ui SPEC.md §8): a line typed in the focused pane's +//! tag after a `:` key cell, a text prompt of its own (Prompt.zig's +//! `.command`). Its rows are the tag's own words in the tag's order (`Kill` +//! after them for a terminal), marked `in the tag` and ruled off, then the +//! commands a helix hand types, each the pardes word it runs: `write` +//! Save, `quit` Del, `theme X` Theme X, `set` and `toggle` a setting's +//! word, `reload` Get, `goto N` Look `:N`, `open F` Look F, `sort`, +//! `reflow` and `pipe` a pipe of the selections. The field filters them by +//! the query pane's scorer (Names.match), in their order; Tab and S-Tab put +//! the next or the last into the field, a command with a space after it, +//! and the doc beside the rows is the current one's. Enter runs the line as +//! B2 runs a word: a command as its pardes word, anything else Exec'd. `:` +//! on the empty field gives the keyboard to the tag's own text, as `:` did +//! before there was a field; `:` there gives it back. +//! +//! A view's own, as its prompt is (TagState.prompt.command): which rows +//! the hits (TagState.hits) are of, and where in the field Tab writes. +const Command = @This(); + +const std = @import("std"); +const pardes = @import("root.zig"); +const config = @import("config.zig"); +const tagline = @import("tagline.zig"); +const panes = @import("panes.zig"); +const exec = @import("exec.zig"); +const builtins = @import("builtins.zig"); +const Names = @import("Names.zig"); +const Field = @import("Field.zig"); +const Band = @import("Band.zig"); +const TagState = @import("TagState.zig"); +const Prompt = @import("Prompt.zig"); +const Pipe = @import("Pipe.zig"); +const Pardes = pardes.Pardes; +const Pane = panes.Pane; +const File = panes.File; +const Key = pardes.Key; + +/// What the rows (TagState.hits) are of: the names a line starts with +/// (the tag's words, then the commands), or none: a command whose +/// argument is no list (`goto`'s line). +pub const Rows = enum(u8) { names, none }; + +rows: Rows = .names, +/// The names' first rows are the tag's words: how many there were when +/// the field was filtered (the commands' come after them). +words: u8 = 0, +/// Where the query starts in the field: Tab writes the current row from +/// there. +from: u16 = 0, +/// Tab (or a click) wrote the current row into the field: the rows are +/// the line's from before it, which Tab steps on through. +completed: bool = false, + +/// A command typed after `:`, its short name, what it takes, and what it +/// does: the pardes word it is. +const Typed = struct { name: []const u8, alias: []const u8 = "", arg: []const u8 = "", doc: []const u8 }; + +const typed = [_]Typed{ + .{ .name = "write", .alias = "w", .doc = "Write the pane's text to its file: Save (a path after it, Save there)." }, + .{ .name = "quit", .alias = "q", .doc = "Close the pane, asking once over unsaved text: Del." }, + .{ .name = "theme", .arg = "theme", .doc = "Change the theme: Theme and its name." }, + .{ .name = "set", .arg = "setting", .doc = "Set a setting: its word and the value (`set Wrap off` is `Wrap off`)." }, + .{ .name = "sort", .doc = "Sort the selections' lines: each piped through sort, as `|sort` does." }, + .{ .name = "reflow", .arg = "width", .doc = "Wrap the selections' lines at a width, 80 if none: each piped through `fmt -w`." }, + .{ .name = "goto", .alias = "g", .arg = "line", .doc = "Go to a line: Look `:N`." }, + .{ .name = "open", .alias = "o", .arg = "file", .doc = "Open a file: Look at its name." }, + .{ .name = "pipe", .arg = "command", .doc = "Pipe the selections through a command, as `|` does." }, + .{ .name = "reload", .doc = "Read the file again: Get." }, + .{ .name = "toggle", .arg = "setting", .doc = "Flip a setting, or step it to its next value: its word bare." }, +}; + +/// The tag's words `:` lists at most. +const max_words = 32; +/// A row's text at most, its name and what it says beside it. +const row_max = 96; +/// A doc's rows, the fewest the names' band has. +const doc_rows = 4; + +/// `:` in pane `pane`'s body: the field empty in its tag, every row. +pub fn open(p: *Pardes, pane: *Pane) void { + Prompt.open(p, pane, .command); + if (p.view().tag_state.kind != .prompt) return; + filter(p, pane); +} + +/// A key `:` takes before its prompt's own (Prompt.key): `:` on the empty +/// field, Tab and S-Tab, Enter. False for the rest. +pub fn key(p: *Pardes, id: usize, pane: *Pane, k: Key) bool { + const s = &p.view().tag_state; + const plain = !k.ctrl and !k.alt; + if (plain and !k.shift and k.cp == ':' and s.field.len == 0) { + TagState.toRest(p); + tagline.enterTag(p, pane); + return true; + } + if (plain and k.cp == Key.tab) { + step(p, pane, if (k.shift) -1 else 1); + return true; + } + if (plain and k.cp == Key.enter) { + run(p, id, pane); + return true; + } + return false; +} + +/// The line in the field read: its first word, the command it names, and +/// what follows the space after it (null without one). +const Line = struct { name: []const u8, command: ?usize, arg: ?[]const u8 }; + +fn read(line: []const u8) Line { + const space = std.mem.indexOfScalar(u8, line, ' '); + const name = line[0 .. space orelse line.len]; + const command = for (typed, 0..) |t, i| { + if (std.mem.eql(u8, name, t.name) or (t.alias.len > 0 and std.mem.eql(u8, name, t.alias))) break i; + } else null; + return .{ .name = name, .command = command, .arg = if (space) |at| line[at + 1 ..] else null }; +} + +/// The rows for the line in the field: the names its first word matches, +/// until a command's space; then its argument's. The matches in the rows' +/// own order, the first current. +pub fn filter(p: *Pardes, pane: *Pane) void { + const s = &p.view().tag_state; + const c = &s.prompt.command; + const line = s.field.text(); + const l = read(line); + c.completed = false; + c.from = 0; + c.words = 0; + s.rows = .{}; + s.nhits = 0; + s.ntotal = 0; + c.rows = if (l.command != null and l.arg != null) .none else .names; + if (c.rows == .none) return; + const q = l.name; + var words = tagWords(pane); + while (words.next()) |w| { + if (c.words == max_words) break; + consider(s, q, w, c.words); + c.words += 1; + } + for (typed, 0..) |t, i| consider(s, q, t.name, c.words + i); +} + +/// Row `at` of its source among the hits when query `q` matches its +/// `text`. +fn consider(s: *TagState, q: []const u8, text: []const u8, at: usize) void { + s.ntotal += 1; + if (q.len > 0 and !matches(q, text)) return; + if (s.nhits == TagState.max_hits) return; + s.hits[s.nhits] = @intCast(at); + s.nhits += 1; +} + +fn matches(q: []const u8, text: []const u8) bool { + var low: [row_max]u8 = undefined; + const t = text[0..@min(text.len, low.len)]; + return Names.match(q, t, std.ascii.lowerString(&low, t), 0, null, null) != null; +} + +/// The tag's own words, in its order: its text after the prefix but `|`, +/// then for a terminal `Kill` (its job's), unless the tag has it. +const Words = struct { + words: std.mem.TokenIterator(u8, .any), + kill: bool, + + fn next(w: *Words) ?[]const u8 { + while (w.words.next()) |word| { + if (std.mem.eql(u8, word, "|")) continue; + if (std.mem.eql(u8, word, "Kill")) w.kill = false; + return word; + } + if (!w.kill) return null; + w.kill = false; + return "Kill"; + } +}; + +fn tagWords(pane: *Pane) Words { + return .{ .words = std.mem.tokenizeAny(u8, tagline.curTail(pane), " \t\n"), .kill = pane.isTerminal() and pane.command == null }; +} + +/// The name row `at` stands for: a tag's word, or a command's name. +fn nameOf(pane: *Pane, c: *const Command, at: usize) ?[]const u8 { + if (at >= c.words) return if (at - c.words < typed.len) typed[at - c.words].name else null; + var words = tagWords(pane); + var i: usize = 0; + while (words.next()) |w| : (i += 1) if (i == at) return w; + return null; +} + +/// Tab (`by` 1) and S-Tab (-1): the next row, or the last, written into +/// the field from where the query starts, a command's name with a space +/// after it; the rows stay the line's from before. +fn step(p: *Pardes, pane: *Pane, by: i32) void { + const s = &p.view().tag_state; + const c = &s.prompt.command; + if (s.nhits == 0) return; + const n: i32 = s.nhits; + if (c.completed) { + s.rows.at = @intCast(@mod(@as(i32, s.rows.at) + by, n)); + } else if (by < 0) s.rows.at = @intCast(n - 1); + fill(p, pane); +} + +/// The current row written into the field after the query's start. +fn fill(p: *Pardes, pane: *Pane) void { + const s = &p.view().tag_state; + const c = &s.prompt.command; + const at = s.hits[s.rows.at]; + const name = nameOf(pane, c, at) orelse return; + var line: [Field.max]u8 = undefined; + var w: std.Io.Writer = .fixed(&line); + w.writeAll(s.field.text()[0..@min(c.from, s.field.len)]) catch return; + w.writeAll(name) catch return; + if (at >= c.words) w.writeByte(' ') catch return; + s.field.set(w.buffered()); + c.completed = true; + s.prompt.recall = 0; +} + +/// A click on row `index` of the rows: it is current, written into the +/// field as Tab writes it. +pub fn point(p: *Pardes, index: u16) void { + const s = &p.view().tag_state; + const pane = p.panes[TagState.hostOf(p) orelse return] orelse return; + if (index >= s.nhits) return; + s.rows.at = index; + fill(p, pane); +} + +/// What a line runs: the pardes word a command is, Looked at or Exec'd, +/// or a pipe of the selections; anything else, the line Exec'd. `said` +/// when it can run nothing, and why. +const Run = struct { + verb: enum { exec, look, pipe } = .exec, + word: []const u8 = "", + said: ?[]const u8 = null, +}; + +fn plan(line_in: []const u8, buf: []u8) Run { + const line = std.mem.trim(u8, line_in, " "); + const l = read(line); + const i = l.command orelse return .{ .word = line }; + const arg = std.mem.trim(u8, l.arg orelse "", " "); + const name = typed[i].name; + const word: Run = if (std.mem.eql(u8, name, "write")) + .{ .word = if (arg.len == 0) "Save" else std.fmt.bufPrint(buf, "Save {s}", .{arg}) catch return .{ .said = "write: the path is too long" } } + else if (std.mem.eql(u8, name, "quit")) + .{ .word = "Del" } + else if (std.mem.eql(u8, name, "reload")) + .{ .word = "Get" } + else if (std.mem.eql(u8, name, "theme")) + (if (arg.len == 0) Run{ .said = "theme: which? Themes lists them" } else .{ .word = std.fmt.bufPrint(buf, "Theme {s}", .{arg}) catch return .{ .said = "theme: no such theme" } }) + else if (std.mem.eql(u8, name, "set") or std.mem.eql(u8, name, "toggle")) + setting(name, arg, buf) + else if (std.mem.eql(u8, name, "goto")) + (if (arg.len == 0 or !allDigits(arg)) Run{ .said = "goto: a line number" } else .{ .verb = .look, .word = std.fmt.bufPrint(buf, ":{s}", .{arg}) catch return .{ .said = "goto: a line number" } }) + else if (std.mem.eql(u8, name, "open")) + (if (arg.len == 0) Run{ .said = "open: which file?" } else .{ .verb = .look, .word = arg }) + else if (std.mem.eql(u8, name, "sort")) + .{ .verb = .pipe, .word = "sort" } + else if (std.mem.eql(u8, name, "reflow")) + (if (arg.len > 0 and !allDigits(arg)) Run{ .said = "reflow: a width in cells" } else .{ .verb = .pipe, .word = std.fmt.bufPrint(buf, "fmt -w {s}", .{if (arg.len == 0) "80" else arg}) catch return .{ .said = "reflow: a width in cells" } }) + else if (std.mem.eql(u8, name, "pipe")) + (if (arg.len == 0) Run{ .said = "pipe: through what?" } else .{ .verb = .pipe, .word = arg }) + else + unreachable; + return word; +} + +/// `set Word value` and `toggle Word`: the setting's word (any case +/// typed), with the value, or bare. +fn setting(name: []const u8, arg: []const u8, buf: []u8) Run { + const space = std.mem.indexOfScalar(u8, arg, ' '); + const which = arg[0 .. space orelse arg.len]; + const value = std.mem.trim(u8, if (space) |at| arg[at + 1 ..] else "", " "); + const found = for (config.Settings.settings) |st| { + if (std.ascii.eqlIgnoreCase(st.word, which)) break st; + } else return .{ .said = if (which.len == 0) "set: which setting?" else "set: no such setting" }; + if (std.mem.eql(u8, name, "toggle") or value.len == 0) return .{ .word = found.word }; + return .{ .word = std.fmt.bufPrint(buf, "{s} {s}", .{ found.word, value }) catch return .{ .said = "set: the value is too long" } }; +} + +fn allDigits(s: []const u8) bool { + for (s) |b| if (!std.ascii.isDigit(b)) return false; + return true; +} + +/// Enter: the line kept in `:`'s history and register, the tag at rest, +/// and what it says run, in pane `id`, as B2 runs a word. +fn run(p: *Pardes, id: usize, pane: *Pane) void { + const s = &p.view().tag_state; + var kept: [Field.max]u8 = undefined; + const line = kept[0..s.field.len]; + @memcpy(line, s.field.text()); + TagState.toRest(p); + if (std.mem.trim(u8, line, " ").len == 0) return; + p.prompt_history.command.add(line); + p.registers.put(p.gpa, ':', line, 0, 1, true) catch |err| return p.reportError(.at(id), "command", err); + var buf: [Field.max + 16]u8 = undefined; + const r = plan(line, &buf); + if (r.said) |why| return p.setMessage(.at(id), why); + switch (r.verb) { + .exec => exec.runBuiltin(p, config.exec_cmd, id, "", r.word), + .look => exec.runBuiltin(p, config.look_cmd, id, "", r.word), + .pipe => { + const f = pane.file orelse return p.setMessage(.at(id), "only a file's text is piped"); + if (!panes.Output.fileTraits(f.output).saves) return p.setMessage(.at(id), "only a file's text is piped"); + p.registers.put(p.gpa, '|', r.word, 0, 1, true) catch |err| return p.reportError(.at(id), "command", err); + Pipe.submit(p, id, &pane.body, .replace, r.word); + }, + } +} + +/// The plain word the line in the field is (Prompt.head): `Exec Save`, +/// `Look :12`, `Exec Edit |sort`; null when it runs nothing. +pub fn plainWord(w: *std.Io.Writer, line: []const u8) ?[]const u8 { + var buf: [Field.max + 16]u8 = undefined; + const r = plan(line, &buf); + if (r.said != null) return null; + switch (r.verb) { + .exec => w.writeAll(r.word) catch return null, + .look => w.writeAll(r.word) catch return null, + .pipe => w.print("Edit |{s}", .{r.word}) catch return null, + } + return if (r.verb == .look) "Look" else "Exec"; +} + +/// The count after the field: how many rows, or what the command takes. +pub fn count(p: *Pardes, arena: std.mem.Allocator, text: *std.ArrayList(u8)) !void { + const s = &p.view().tag_state; + const c = &s.prompt.command; + switch (c.rows) { + .names => try text.print(arena, " {d} command{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), + .none => { + const l = read(s.field.text()); + const t = typed[l.command orelse return]; + try text.print(arena, " {s}", .{if (t.arg.len == 0) "⏎ runs it" else try std.fmt.allocPrint(arena, "<{s}>", .{t.arg})}); + }, + } +} + +/// The rows the band takes: the rows shown, and beside the names' the +/// current one's doc. +pub fn takes(p: *Pardes) u16 { + const s = &p.view().tag_state; + const c = &s.prompt.command; + if (c.rows == .none) return 0; + const shown: u16 = @max(1, @min(Band.Cursor.shown, s.nhits)); + return @max(doc_rows, shown); +} + +/// The band's steps: the rows around the current one, each its name, the +/// query's bytes in the match tint, what it is said on its right (`in the +/// tag`, a command's ``), the tag's last word ruled off; and the +/// current row's doc. +pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { + const s = &p.view().tag_state; + const c = &s.prompt.command; + const pane = p.panes[TagState.hostOf(p) orelse return 0] orelse return 0; + if (c.rows == .none or out.len < 2) return 0; + if (s.nhits == 0) { + const none = try arena.alloc(Band.Row, 1); + none[0] = .{ .text = "no word or command matches", .dim = true }; + out[0] = .{ .rows = none }; + return 1; + } + const tab = p.settings.tab; + const win = s.rows.window(s.nhits); + const q = if (c.completed) "" else read(s.field.text()).name; + var widest: usize = 0; + for (s.hits[win.lo..win.hi]) |at| widest = @max(widest, File.displayWidth(tab, nameOf(pane, c, at) orelse "") + aliasWidth(c, at)); + const rows = try arena.alloc(Band.Row, win.hi - win.lo); + for (rows, win.lo..) |*row, i| { + const at = s.hits[i]; + const name = nameOf(pane, c, at) orelse ""; + var text: std.ArrayList(u8) = .empty; + try text.appendSlice(arena, name); + if (at >= c.words and typed[at - c.words].alias.len > 0) try text.print(arena, " {s}", .{typed[at - c.words].alias}); + const said: []const u8 = if (at < c.words) "in the tag" else if (typed[at - c.words].arg.len > 0) try std.fmt.allocPrint(arena, "<{s}>", .{typed[at - c.words].arg}) else ""; + if (said.len > 0) { + for (0..widest + 2 - File.displayWidth(tab, text.items)) |_| try text.append(arena, ' '); + try text.appendSlice(arena, said); + } + var marks: [Names.max_query * 4]u16 = undefined; + var nmarks: usize = 0; + if (q.len > 0) { + var low: [row_max]u8 = undefined; + const t = name[0..@min(name.len, low.len)]; + _ = Names.match(q, t, std.ascii.lowerString(&low, t), 0, &marks, &nmarks); + } + // The tag's last word ruled off from the commands under it. + const last_word = at + 1 == c.words and i + 1 < s.nhits; + row.* = .{ .text = text.items, .on = i == s.rows.at, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .under = last_word }; + } + out[0] = .{ .rows = rows }; + out[1] = .{ .rows = try docRows(arena, pane, c, s.hits[s.rows.at]), .doc = true }; + return 2; +} + +fn aliasWidth(c: *const Command, at: usize) usize { + if (at < c.words) return 0; + const alias = typed[at - c.words].alias; + return if (alias.len == 0) 0 else alias.len + 1; +} + +/// Row `at`'s doc: its name, then what it does. +fn docRows(arena: std.mem.Allocator, pane: *Pane, c: *const Command, at: usize) ![]const Band.Row { + const rows = try arena.alloc(Band.Row, 2); + if (at >= c.words) { + const t = typed[at - c.words]; + const head = try std.fmt.allocPrint(arena, ":{s}{s}{s}{s}", .{ t.name, if (t.alias.len > 0) " " else "", t.alias, if (t.arg.len > 0) try std.fmt.allocPrint(arena, " <{s}>", .{t.arg}) else "" }); + rows[0] = .{ .text = head, .key = @intCast(1 + t.name.len) }; + rows[1] = .{ .text = t.doc }; + return rows; + } + const name = nameOf(pane, c, at) orelse ""; + rows[0] = .{ .text = try std.fmt.allocPrint(arena, "{s} in the tag", .{name}), .key = @intCast(name.len) }; + rows[1] = .{ .text = if (std.meta.stringToEnum(pardes.Builtin, name)) |b| builtins.registry.doc(b).text else "A word of this pane's tag: ⏎ runs it, as B2 on it does." }; + return rows; +} + +fn typeKeys(p: *Pardes, keys: []const u8) void { + for (keys) |ch| p.update(.{ .key = .{ .cp = ch, .text = &[_]u8{ch} } }); +} + +fn tagOf(p: *Pardes) ![]const u8 { + return tagline.tagText(p, p.scratch.allocator(), p.panes[p.view().active].?); +} + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +/// Band row `y`'s text under the live view's active pane. +fn bandRow(p: *Pardes, arena: std.mem.Allocator, y: u16) ![]const u8 { + const id = p.view().active; + const band = (try Band.of(p, arena, p.panes[id].?, id)).?; + return (try band.line(arena, p.settings.tab, y)).text; +} + +test ": opens a field in the tag after its key cell: the tag's own words first, in its order, marked and ruled off, then the commands with what they take; typing filters both; Tab completes, a command with a space; the doc is the current row's; Enter runs a command as its pardes word" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const pane = try p.setTestFile("b\na\nc\n"); + const rest = try std.testing.allocator.dupe(u8, try tagOf(p)); + defer std.testing.allocator.free(rest); + typeKeys(p, ":"); + const s = &p.view().tag_state; + try std.testing.expectEqual(TagState.Kind.prompt, s.kind); + try std.testing.expectEqual(Prompt.Op.command, s.prompt.op); + try std.testing.expectEqual(panes.Pane.Focus.body, pane.focus); + // The head: its key cell, the field, the count, the hints. + try std.testing.expect(has(try tagOf(p), " : 16 commands Tab complete · ⏎ run · C-r history · esc")); + // The rows: the tag's words in its order, `in the tag`, the last ruled + // off; then the commands, each what it takes. + try std.testing.expect(has(try bandRow(p, a, 0), "▶ Save ")); + try std.testing.expect(has(try bandRow(p, a, 0), "in the tag")); + try std.testing.expect(has(try bandRow(p, a, 1), " Term ")); + // The doc beside them is the current row's: Save's. + try std.testing.expect(has(try bandRow(p, a, 0), "Save in the tag")); + const band = (try Band.of(p, a, pane, p.view().active)).?; + const first = band.steps[0].rows; + try std.testing.expect(first[4].under and !first[3].under); + try std.testing.expect(has(first[5].text, "write w")); + try std.testing.expect(has(first[7].text, "theme") and has(first[7].text, "")); + // Typing filters both, by the scorer, in their order. + typeKeys(p, "th"); + try std.testing.expect(has(try tagOf(p), " : th = Exec th 1 command ")); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ theme ")); + // Tab: the command, a space after it; the field's plain word is its + // pardes word once it is whole. + p.update(.{ .key = .{ .cp = Key.tab } }); + try std.testing.expectEqualStrings("theme ", s.field.text()); + typeKeys(p, "acme"); + try std.testing.expect(has(try tagOf(p), " : theme acme = Exec Theme acme ")); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); + try std.testing.expectEqualStrings("acme", p.theme().name); + try std.testing.expectEqualStrings("theme acme", p.prompt_history.command.get(1).?); + try std.testing.expectEqualStrings("theme acme", p.registers.value(':', 0).?); + try std.testing.expectEqualStrings(rest, try tagOf(p)); + // `:sort`: the selections piped through sort, as `|sort` does. + while (p.nextEffect()) |_| {} + typeKeys(p, "%:sort"); + try std.testing.expect(has(try tagOf(p), " : sort = Exec Edit |sort 1 command")); + p.update(.{ .key = .{ .cp = Key.enter } }); + var asked: ?u32 = null; + while (p.nextEffect()) |effect| if (effect == .pipe) { + asked = effect.pipe.id; + }; + try std.testing.expectEqualStrings("sort", p.pipe.pipeRequest(asked orelse return error.MissingPipeEffect).?.command); + // `:goto 2`: Look `:2`. + typeKeys(p, ":goto 2"); + try std.testing.expect(has(try tagOf(p), " : goto 2 = Look :2 ")); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); + // S-Tab: the last row; Tab after it, round to the first. A tag word + // takes no space. + typeKeys(p, ":"); + p.update(.{ .key = .{ .cp = Key.tab, .shift = true } }); + try std.testing.expectEqualStrings("toggle ", s.field.text()); + p.update(.{ .key = .{ .cp = Key.tab } }); + try std.testing.expectEqualStrings("Save", s.field.text()); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); +} + +test ": on the empty field gives the keyboard to the tag's own text, and : there gives it back" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile("one\n"); + typeKeys(p, "::"); + try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); + try std.testing.expectEqual(panes.Pane.Focus.tag, pane.focus); + typeKeys(p, ":"); + try std.testing.expectEqual(panes.Pane.Focus.body, pane.focus); + // A field with text in it takes `:` as a character. + typeKeys(p, ":a:"); + try std.testing.expectEqualStrings("a:", p.view().tag_state.field.text()); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); + try std.testing.expectEqual(panes.Pane.Focus.body, pane.focus); +} -- cgit v1.3 From ac450e5868f528b312abb2b46191cd8bce5bff3d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 25 Nov 2026 00:46:11 -0300 Subject: Command: once a `:` command takes its argument the rows are what it takes, the rest of the line the query, and `:theme ` dresses the whole window in the current row (walk UI, pick-ui SPEC.md §8, item 2 of line E2): `theme ` lists every theme (colors.themes, in the ring's order, `in use` beside the session's), `set ` and `toggle ` the settings this frontend has with their values now (the root ctl's text, ninep/ctl.zig settingsText), `set Wrap ` that setting's words (Settings.choices: `on off`, `walk new-pane`, `off cat frog`; `now` beside the one it has), `open ` the recent files (recent.zig, 200 at most, from the pane's directory, `open` beside a file a pane shows: the directory walk is the pickers' and not in this tree), `pipe ` the pipe history, newest first; filtered as the names are (Names.match, in their order), counted (`273 themes · the window wears the current one`, `2 values`, `1 recent file`), Tab and S-Tab writing the row after the command (`set Wrap off`); THE PREVIEW: colors.wear(p, index) is the one function a state that previews themes calls (agent F's Themes picker can call it as it is): the live view's TagState.worn (an index of colors.themes, in the digest and the dump, held to the themes on a load: replica `.index` isTheme) and every pane flagged previewing, so none dims; Core.theme() returns the worn theme and chromeTheme() its chrome, worked out once a theme (Pardes.worn_chrome, a cache kept out as body_chrome is, made by wear and by a frame's first step for a view that wears another), nothing loaded or set: every pane, tag and band recolours on the next frame; TagState.toRest takes it off, so Esc puts the window's own back, and Enter runs `Theme `; a load meanwhile works out the session's own chrome (colors.finishThemeInitialization no longer reads theme()); TagState.max_hits is at least the themes' count (273: 70 of pardes's, 203 generated) so their rows fit; test/perf.zig --search measures a key in `:` and Tab in `:theme ` with their frames; TEST FIRST: Command.zig ":theme lists the themes and the whole window wears the current one, none dimmed; Tab steps it on, Esc puts the window's own back, Enter keeps the one worn; :set lists the settings with their values, then a setting's values; :pipe the pipes typed last; :open the recent files"; item 1's test reads the theme worn for `:theme acme`; core-test 1022/1189 (167 skipped) Co-Authored-By: Claude Opus 5.5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Command.zig | 385 +++++++++++++++++++++++++++++++++++++++++++++--------- core/Core.zig | 7 +- core/Prompt.zig | 5 +- core/TagState.zig | 15 ++- core/colors.zig | 23 +++- core/draw.zig | 1 + core/root.zig | 2 +- test/perf.zig | 24 ++++ 8 files changed, 389 insertions(+), 73 deletions(-) (limited to 'core/Command.zig') diff --git a/core/Command.zig b/core/Command.zig index 6f781b51..5364b898 100644 --- a/core/Command.zig +++ b/core/Command.zig @@ -8,8 +8,13 @@ //! `reflow` and `pipe` a pipe of the selections. The field filters them by //! the query pane's scorer (Names.match), in their order; Tab and S-Tab put //! the next or the last into the field, a command with a space after it, -//! and the doc beside the rows is the current one's. Enter runs the line as -//! B2 runs a word: a command as its pardes word, anything else Exec'd. `:` +//! and the doc beside the rows is the current one's. Once a command takes +//! its argument the rows are what it takes, the rest of the line the +//! query: `:theme ` the themes, the whole window wearing the current one +//! (colors.wear); `:set ` the settings and their values now, then `:set +//! Wrap ` its values; `:open ` the recent files; `:pipe ` the pipes typed +//! last. Enter runs the line as B2 runs a word: a command as its pardes +//! word (`:theme`'s the theme the window wears), anything else Exec'd. `:` //! on the empty field gives the keyboard to the tag's own text, as `:` did //! before there was a field; `:` there gives it back. //! @@ -30,17 +35,24 @@ const Band = @import("Band.zig"); const TagState = @import("TagState.zig"); const Prompt = @import("Prompt.zig"); const Pipe = @import("Pipe.zig"); +const colors = @import("colors.zig"); const Pardes = pardes.Pardes; const Pane = panes.Pane; const File = panes.File; const Key = pardes.Key; /// What the rows (TagState.hits) are of: the names a line starts with -/// (the tag's words, then the commands), or none: a command whose -/// argument is no list (`goto`'s line). -pub const Rows = enum(u8) { names, none }; +/// (the tag's words, then the commands); a command's argument (themes, by +/// their index in colors.themes; settings, in config.Settings.settings; +/// the values of `setting`; the recent files, newest first; the pipe +/// history, newest first); or none, an argument that is no list (`goto`'s +/// line). +pub const Rows = enum(u8) { names, themes, settings, values, files, pipes, none }; rows: Rows = .names, +/// `:set X `'s setting, by its index in config.Settings.settings: the +/// values are its. +setting: u8 = 0, /// The names' first rows are the tag's words: how many there were when /// the field was filtered (the commands' come after them). words: u8 = 0, @@ -53,20 +65,20 @@ completed: bool = false, /// A command typed after `:`, its short name, what it takes, and what it /// does: the pardes word it is. -const Typed = struct { name: []const u8, alias: []const u8 = "", arg: []const u8 = "", doc: []const u8 }; +const Typed = struct { name: []const u8, alias: []const u8 = "", arg: []const u8 = "", rows: Rows = .none, doc: []const u8 }; const typed = [_]Typed{ .{ .name = "write", .alias = "w", .doc = "Write the pane's text to its file: Save (a path after it, Save there)." }, .{ .name = "quit", .alias = "q", .doc = "Close the pane, asking once over unsaved text: Del." }, - .{ .name = "theme", .arg = "theme", .doc = "Change the theme: Theme and its name." }, - .{ .name = "set", .arg = "setting", .doc = "Set a setting: its word and the value (`set Wrap off` is `Wrap off`)." }, + .{ .name = "theme", .arg = "theme", .rows = .themes, .doc = "Change the theme: Theme and its name." }, + .{ .name = "set", .arg = "setting", .rows = .settings, .doc = "Set a setting: its word and the value (`set Wrap off` is `Wrap off`)." }, .{ .name = "sort", .doc = "Sort the selections' lines: each piped through sort, as `|sort` does." }, .{ .name = "reflow", .arg = "width", .doc = "Wrap the selections' lines at a width, 80 if none: each piped through `fmt -w`." }, .{ .name = "goto", .alias = "g", .arg = "line", .doc = "Go to a line: Look `:N`." }, - .{ .name = "open", .alias = "o", .arg = "file", .doc = "Open a file: Look at its name." }, - .{ .name = "pipe", .arg = "command", .doc = "Pipe the selections through a command, as `|` does." }, + .{ .name = "open", .alias = "o", .arg = "file", .rows = .files, .doc = "Open a file: Look at its name." }, + .{ .name = "pipe", .arg = "command", .rows = .pipes, .doc = "Pipe the selections through a command, as `|` does." }, .{ .name = "reload", .doc = "Read the file again: Get." }, - .{ .name = "toggle", .arg = "setting", .doc = "Flip a setting, or step it to its next value: its word bare." }, + .{ .name = "toggle", .arg = "setting", .rows = .settings, .doc = "Flip a setting, or step it to its next value: its word bare." }, }; /// The tag's words `:` lists at most. @@ -118,8 +130,10 @@ fn read(line: []const u8) Line { } /// The rows for the line in the field: the names its first word matches, -/// until a command's space; then its argument's. The matches in the rows' -/// own order, the first current. +/// until a command's space; then its argument's, the rest of the line the +/// query (`:set`'s setting's values after the setting and a space). The +/// matches in the rows' own order, the first current; the window wears it +/// while they are themes. pub fn filter(p: *Pardes, pane: *Pane) void { const s = &p.view().tag_state; const c = &s.prompt.command; @@ -131,16 +145,84 @@ pub fn filter(p: *Pardes, pane: *Pane) void { s.rows = .{}; s.nhits = 0; s.ntotal = 0; - c.rows = if (l.command != null and l.arg != null) .none else .names; - if (c.rows == .none) return; - const q = l.name; - var words = tagWords(pane); - while (words.next()) |w| { - if (c.words == max_words) break; - consider(s, q, w, c.words); - c.words += 1; + c.rows = .names; + var q = l.name; + if (l.command) |i| if (l.arg) |arg| { + c.rows = typed[i].rows; + c.from = @intCast(l.name.len + 1); + q = arg; + // `set Wrap `: Wrap's values, those a setting of words takes. + if (c.rows == .settings) if (std.mem.indexOfScalar(u8, arg, ' ')) |space| { + c.rows = .none; + if (std.mem.eql(u8, typed[i].name, "set")) if (settingNamed(arg[0..space])) |k| if (config.Settings.choices(config.Settings.settings[k].action) != null) { + c.rows = .values; + c.setting = @intCast(k); + c.from += @intCast(space + 1); + q = arg[space + 1 ..]; + }; + }; + }; + switch (c.rows) { + .none => {}, + .names => { + var words = tagWords(pane); + while (words.next()) |w| { + if (c.words == max_words) break; + consider(s, q, w, c.words); + c.words += 1; + } + for (typed, 0..) |t, i| consider(s, q, t.name, c.words + i); + }, + .themes => for (colors.themes, 0..) |t, i| consider(s, q, t.name, i), + .settings => { + const caps = builtins.viewCapabilities(p); + for (config.Settings.settings, 0..) |st, i| if (st.enabled(caps)) consider(s, q, st.word, i); + }, + .values => { + var values = valuesOf(c.setting); + var i: usize = 0; + while (values.next()) |v| : (i += 1) consider(s, q, v, i); + }, + .files => { + const dir = panes.paneDir(pane); + for (p.recent.entries.items, 0..) |e, i| consider(s, q, shownPath(e.path, dir), i); + }, + .pipes => { + const r = &p.prompt_history.pipe; + for (0..r.n) |i| consider(s, q, r.get(@intCast(i + 1)).?, i); + }, } - for (typed, 0..) |t, i| consider(s, q, t.name, c.words + i); + preview(p); +} + +/// The setting named `word` (any case), by its index. +fn settingNamed(word: []const u8) ?usize { + for (config.Settings.settings, 0..) |st, i| if (std.ascii.eqlIgnoreCase(st.word, word)) return i; + return null; +} + +/// The words setting `k` takes (`on, off`; `off, cat, frog`). +fn valuesOf(k: u8) std.mem.SplitIterator(u8, .sequence) { + const words = config.Settings.choices(config.Settings.settings[@min(k, config.Settings.settings.len - 1)].action) orelse ""; + return std.mem.splitSequence(u8, words, ", "); +} + +/// A recent file as its row shows it: under the pane's directory, from +/// there. +fn shownPath(path: []const u8, dir: []const u8) []const u8 { + const d = std.mem.trimEnd(u8, dir, "/"); + if (d.len > 0 and path.len > d.len + 1 and std.mem.startsWith(u8, path, d) and path[d.len] == '/') return path[d.len + 1 ..]; + return path; +} + +/// The window wears the current row while the rows are themes; else its +/// own theme. +fn preview(p: *Pardes) void { + const s = &p.view().tag_state; + const c = &s.prompt.command; + const worn: ?usize = if (c.rows == .themes and s.nhits > 0) s.hits[s.rows.at] else null; + if (worn == null and s.worn == null) return; + colors.wear(p, worn); } /// Row `at` of its source among the hits when query `q` matches its @@ -181,13 +263,30 @@ fn tagWords(pane: *Pane) Words { return .{ .words = std.mem.tokenizeAny(u8, tagline.curTail(pane), " \t\n"), .kill = pane.isTerminal() and pane.command == null }; } -/// The name row `at` stands for: a tag's word, or a command's name. -fn nameOf(pane: *Pane, c: *const Command, at: usize) ?[]const u8 { - if (at >= c.words) return if (at - c.words < typed.len) typed[at - c.words].name else null; - var words = tagWords(pane); - var i: usize = 0; - while (words.next()) |w| : (i += 1) if (i == at) return w; - return null; +/// What row `at` of the rows stands for, as Tab writes it: a tag's word, +/// a command's name, a theme's, a setting's word, a value, a recent file, +/// a pipe; null past them (a damaged image's). +fn rowOf(p: *Pardes, pane: *Pane, c: *const Command, at: usize) ?[]const u8 { + switch (c.rows) { + .none => return null, + .names => { + if (at >= c.words) return if (at - c.words < typed.len) typed[at - c.words].name else null; + var words = tagWords(pane); + var i: usize = 0; + while (words.next()) |w| : (i += 1) if (i == at) return w; + return null; + }, + .themes => return if (at < colors.themes.len) colors.themes[at].name else null, + .settings => return if (at < config.Settings.settings.len) config.Settings.settings[at].word else null, + .values => { + var values = valuesOf(c.setting); + var i: usize = 0; + while (values.next()) |v| : (i += 1) if (i == at) return v; + return null; + }, + .files => return if (at < p.recent.entries.items.len) shownPath(p.recent.entries.items[at].path, panes.paneDir(pane)) else null, + .pipes => return p.prompt_history.pipe.get(@intCast(@min(at + 1, Prompt.Ring.max + 1))), + } } /// Tab (`by` 1) and S-Tab (-1): the next row, or the last, written into @@ -209,15 +308,16 @@ fn fill(p: *Pardes, pane: *Pane) void { const s = &p.view().tag_state; const c = &s.prompt.command; const at = s.hits[s.rows.at]; - const name = nameOf(pane, c, at) orelse return; + const name = rowOf(p, pane, c, at) orelse return; var line: [Field.max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); w.writeAll(s.field.text()[0..@min(c.from, s.field.len)]) catch return; w.writeAll(name) catch return; - if (at >= c.words) w.writeByte(' ') catch return; + if (c.rows == .names and at >= c.words) w.writeByte(' ') catch return; s.field.set(w.buffered()); c.completed = true; s.prompt.recall = 0; + preview(p); } /// A click on row `index` of the rows: it is current, written into the @@ -239,7 +339,7 @@ const Run = struct { said: ?[]const u8 = null, }; -fn plan(line_in: []const u8, buf: []u8) Run { +fn plan(p: *Pardes, line_in: []const u8, buf: []u8) Run { const line = std.mem.trim(u8, line_in, " "); const l = read(line); const i = l.command orelse return .{ .word = line }; @@ -252,9 +352,9 @@ fn plan(line_in: []const u8, buf: []u8) Run { else if (std.mem.eql(u8, name, "reload")) .{ .word = "Get" } else if (std.mem.eql(u8, name, "theme")) - (if (arg.len == 0) Run{ .said = "theme: which? Themes lists them" } else .{ .word = std.fmt.bufPrint(buf, "Theme {s}", .{arg}) catch return .{ .said = "theme: no such theme" } }) + theme(p, arg, buf) else if (std.mem.eql(u8, name, "set") or std.mem.eql(u8, name, "toggle")) - setting(name, arg, buf) + settingWord(name, arg, buf) else if (std.mem.eql(u8, name, "goto")) (if (arg.len == 0 or !allDigits(arg)) Run{ .said = "goto: a line number" } else .{ .verb = .look, .word = std.fmt.bufPrint(buf, ":{s}", .{arg}) catch return .{ .said = "goto: a line number" } }) else if (std.mem.eql(u8, name, "open")) @@ -270,15 +370,21 @@ fn plan(line_in: []const u8, buf: []u8) Run { return word; } +/// `theme`: the theme the window wears (the current row), else as typed. +fn theme(p: *Pardes, arg: []const u8, buf: []u8) Run { + const s = &p.view().tag_state; + const name = if (s.worn) |i| colors.themes[i].name else arg; + if (name.len == 0) return .{ .said = "theme: which? Themes lists them" }; + return .{ .word = std.fmt.bufPrint(buf, "Theme {s}", .{name}) catch return .{ .said = "theme: no such theme" } }; +} + /// `set Word value` and `toggle Word`: the setting's word (any case /// typed), with the value, or bare. -fn setting(name: []const u8, arg: []const u8, buf: []u8) Run { +fn settingWord(name: []const u8, arg: []const u8, buf: []u8) Run { const space = std.mem.indexOfScalar(u8, arg, ' '); const which = arg[0 .. space orelse arg.len]; const value = std.mem.trim(u8, if (space) |at| arg[at + 1 ..] else "", " "); - const found = for (config.Settings.settings) |st| { - if (std.ascii.eqlIgnoreCase(st.word, which)) break st; - } else return .{ .said = if (which.len == 0) "set: which setting?" else "set: no such setting" }; + const found = config.Settings.settings[settingNamed(which) orelse return .{ .said = if (which.len == 0) "set: which setting?" else "set: no such setting" }]; if (std.mem.eql(u8, name, "toggle") or value.len == 0) return .{ .word = found.word }; return .{ .word = std.fmt.bufPrint(buf, "{s} {s}", .{ found.word, value }) catch return .{ .said = "set: the value is too long" } }; } @@ -295,12 +401,13 @@ fn run(p: *Pardes, id: usize, pane: *Pane) void { var kept: [Field.max]u8 = undefined; const line = kept[0..s.field.len]; @memcpy(line, s.field.text()); + // What it runs, read while the rows are there (the theme worn). + var buf: [Field.max + 16]u8 = undefined; + const r = plan(p, line, &buf); TagState.toRest(p); if (std.mem.trim(u8, line, " ").len == 0) return; p.prompt_history.command.add(line); p.registers.put(p.gpa, ':', line, 0, 1, true) catch |err| return p.reportError(.at(id), "command", err); - var buf: [Field.max + 16]u8 = undefined; - const r = plan(line, &buf); if (r.said) |why| return p.setMessage(.at(id), why); switch (r.verb) { .exec => exec.runBuiltin(p, config.exec_cmd, id, "", r.word), @@ -309,6 +416,7 @@ fn run(p: *Pardes, id: usize, pane: *Pane) void { const f = pane.file orelse return p.setMessage(.at(id), "only a file's text is piped"); if (!panes.Output.fileTraits(f.output).saves) return p.setMessage(.at(id), "only a file's text is piped"); p.registers.put(p.gpa, '|', r.word, 0, 1, true) catch |err| return p.reportError(.at(id), "command", err); + p.prompt_history.pipe.add(r.word); Pipe.submit(p, id, &pane.body, .replace, r.word); }, } @@ -316,9 +424,9 @@ fn run(p: *Pardes, id: usize, pane: *Pane) void { /// The plain word the line in the field is (Prompt.head): `Exec Save`, /// `Look :12`, `Exec Edit |sort`; null when it runs nothing. -pub fn plainWord(w: *std.Io.Writer, line: []const u8) ?[]const u8 { +pub fn plainWord(p: *Pardes, w: *std.Io.Writer, line: []const u8) ?[]const u8 { var buf: [Field.max + 16]u8 = undefined; - const r = plan(line, &buf); + const r = plan(p, line, &buf); if (r.said != null) return null; switch (r.verb) { .exec => w.writeAll(r.word) catch return null, @@ -334,6 +442,11 @@ pub fn count(p: *Pardes, arena: std.mem.Allocator, text: *std.ArrayList(u8)) !vo const c = &s.prompt.command; switch (c.rows) { .names => try text.print(arena, " {d} command{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), + .themes => try text.print(arena, " {d} theme{s} · the window wears the current one", .{ s.nhits, if (s.nhits == 1) "" else "s" }), + .settings => try text.print(arena, " {d} setting{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), + .values => try text.print(arena, " {d} value{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), + .files => try text.print(arena, " {d} recent file{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), + .pipes => try text.print(arena, " {d} pipe{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), .none => { const l = read(s.field.text()); const t = typed[l.command orelse return]; @@ -349,13 +462,14 @@ pub fn takes(p: *Pardes) u16 { const c = &s.prompt.command; if (c.rows == .none) return 0; const shown: u16 = @max(1, @min(Band.Cursor.shown, s.nhits)); - return @max(doc_rows, shown); + return if (c.rows == .names) @max(doc_rows, shown) else shown; } -/// The band's steps: the rows around the current one, each its name, the -/// query's bytes in the match tint, what it is said on its right (`in the -/// tag`, a command's ``), the tag's last word ruled off; and the -/// current row's doc. +/// The band's steps: the rows around the current one, each what it +/// stands for, the query's bytes in the match tint, what it is said on its +/// right (`in the tag`, a command's ``, a setting's value now, +/// the theme in use), the tag's last word ruled off from the commands; and +/// beside the names the current one's doc. pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { const s = &p.view().tag_state; const c = &s.prompt.command; @@ -363,23 +477,25 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { if (c.rows == .none or out.len < 2) return 0; if (s.nhits == 0) { const none = try arena.alloc(Band.Row, 1); - none[0] = .{ .text = "no word or command matches", .dim = true }; + none[0] = .{ .text = if (c.rows == .names) "no word or command matches" else "none matches", .dim = true }; out[0] = .{ .rows = none }; return 1; } const tab = p.settings.tab; const win = s.rows.window(s.nhits); - const q = if (c.completed) "" else read(s.field.text()).name; + const line = s.field.text(); + const q = if (c.completed) "" else if (c.rows == .names) read(line).name else line[@min(c.from, line.len)..]; + const now = if (c.rows == .settings) try pardes.ctl.settingsText(p) else ""; var widest: usize = 0; - for (s.hits[win.lo..win.hi]) |at| widest = @max(widest, File.displayWidth(tab, nameOf(pane, c, at) orelse "") + aliasWidth(c, at)); + for (s.hits[win.lo..win.hi]) |at| widest = @max(widest, File.displayWidth(tab, rowOf(p, pane, c, at) orelse "") + aliasWidth(c, at)); const rows = try arena.alloc(Band.Row, win.hi - win.lo); for (rows, win.lo..) |*row, i| { const at = s.hits[i]; - const name = nameOf(pane, c, at) orelse ""; + const name = rowOf(p, pane, c, at) orelse ""; var text: std.ArrayList(u8) = .empty; try text.appendSlice(arena, name); - if (at >= c.words and typed[at - c.words].alias.len > 0) try text.print(arena, " {s}", .{typed[at - c.words].alias}); - const said: []const u8 = if (at < c.words) "in the tag" else if (typed[at - c.words].arg.len > 0) try std.fmt.allocPrint(arena, "<{s}>", .{typed[at - c.words].arg}) else ""; + if (c.rows == .names and at >= c.words and typed[at - c.words].alias.len > 0) try text.print(arena, " {s}", .{typed[at - c.words].alias}); + const said = try saidOf(p, arena, pane, c, at, name, now); if (said.len > 0) { for (0..widest + 2 - File.displayWidth(tab, text.items)) |_| try text.append(arena, ' '); try text.appendSlice(arena, said); @@ -392,33 +508,80 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { _ = Names.match(q, t, std.ascii.lowerString(&low, t), 0, &marks, &nmarks); } // The tag's last word ruled off from the commands under it. - const last_word = at + 1 == c.words and i + 1 < s.nhits; + const last_word = c.rows == .names and at + 1 == c.words and i + 1 < s.nhits; row.* = .{ .text = text.items, .on = i == s.rows.at, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .under = last_word }; } out[0] = .{ .rows = rows }; - out[1] = .{ .rows = try docRows(arena, pane, c, s.hits[s.rows.at]), .doc = true }; + if (c.rows != .names) return 1; + out[1] = .{ .rows = try docRows(p, arena, pane, c, s.hits[s.rows.at]), .doc = true }; return 2; } +/// What row `at` says on its right: a tag's word `in the tag`, what a +/// command takes, the theme in use, a setting's value now (its line of +/// the settings' text `now`), the value it has, a file open in a pane. +fn saidOf(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, c: *const Command, at: usize, name: []const u8, now: []const u8) ![]const u8 { + switch (c.rows) { + .names => { + if (at < c.words) return "in the tag"; + const t = typed[at - c.words]; + return if (t.arg.len > 0) try std.fmt.allocPrint(arena, "<{s}>", .{t.arg}) else ""; + }, + .themes => return if (p.custom_theme == null and at == p.settings.theme) "in use" else "", + .settings => { + var lines = std.mem.splitScalar(u8, now, '\n'); + while (lines.next()) |l| if (l.len > name.len and std.mem.startsWith(u8, l, name) and l[name.len] == ' ') return l[name.len + 1 ..]; + return ""; + }, + .values => { + var buf: [64]u8 = undefined; + const value = valueNow(p, c.setting, &buf); + return if (std.mem.eql(u8, value, name)) "now" else ""; + }, + .files => { + _ = pane; + return if (at < p.recent.entries.items.len and p.recent.entries.items[at].open) "open" else ""; + }, + .pipes, .none => return "", + } +} + +/// Setting `k`'s value now, as its word is written: `on`, `walk`. +fn valueNow(p: *Pardes, k: u8, buf: []u8) []const u8 { + const st = config.Settings.settings[@min(k, config.Settings.settings.len - 1)]; + const value: []const u8 = switch (st.action) { + .toggle => |field| switch (field) { + inline else => |f| if (@field(p.settings, @tagName(f))) "on" else "off", + }, + .choice => |which| switch (which) { + inline else => |field| @tagName(@field(p.settings, @tagName(field))), + }, + else => "", + }; + return std.fmt.bufPrint(buf, "{s}", .{value}) catch ""; +} + fn aliasWidth(c: *const Command, at: usize) usize { - if (at < c.words) return 0; + if (c.rows != .names or at < c.words) return 0; const alias = typed[at - c.words].alias; return if (alias.len == 0) 0 else alias.len + 1; } -/// Row `at`'s doc: its name, then what it does. -fn docRows(arena: std.mem.Allocator, pane: *Pane, c: *const Command, at: usize) ![]const Band.Row { +/// Row `at`'s doc: its name, then what it does. Read-only text a click on +/// does nothing to (its rows index none of the rows). +fn docRows(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, c: *const Command, at: usize) ![]const Band.Row { const rows = try arena.alloc(Band.Row, 2); + const none = std.math.maxInt(u16); if (at >= c.words) { const t = typed[at - c.words]; const head = try std.fmt.allocPrint(arena, ":{s}{s}{s}{s}", .{ t.name, if (t.alias.len > 0) " " else "", t.alias, if (t.arg.len > 0) try std.fmt.allocPrint(arena, " <{s}>", .{t.arg}) else "" }); - rows[0] = .{ .text = head, .key = @intCast(1 + t.name.len) }; - rows[1] = .{ .text = t.doc }; + rows[0] = .{ .text = head, .key = @intCast(1 + t.name.len), .index = none }; + rows[1] = .{ .text = t.doc, .index = none }; return rows; } - const name = nameOf(pane, c, at) orelse ""; - rows[0] = .{ .text = try std.fmt.allocPrint(arena, "{s} in the tag", .{name}), .key = @intCast(name.len) }; - rows[1] = .{ .text = if (std.meta.stringToEnum(pardes.Builtin, name)) |b| builtins.registry.doc(b).text else "A word of this pane's tag: ⏎ runs it, as B2 on it does." }; + const name = rowOf(p, pane, c, at) orelse ""; + rows[0] = .{ .text = try std.fmt.allocPrint(arena, "{s} in the tag", .{name}), .key = @intCast(name.len), .index = none }; + rows[1] = .{ .text = if (std.meta.stringToEnum(pardes.Builtin, name)) |b| builtins.registry.doc(b).text else "A word of this pane's tag: ⏎ runs it, as B2 on it does.", .index = none }; return rows; } @@ -478,10 +641,11 @@ test ": opens a field in the tag after its key cell: the tag's own words first, p.update(.{ .key = .{ .cp = Key.tab } }); try std.testing.expectEqualStrings("theme ", s.field.text()); typeKeys(p, "acme"); - try std.testing.expect(has(try tagOf(p), " : theme acme = Exec Theme acme ")); + const worn = colors.themes[s.worn.?].name; + try std.testing.expect(has(try tagOf(p), try std.fmt.allocPrint(a, " : theme acme = Exec Theme {s} ", .{worn}))); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqual(TagState.Kind.rest, s.kind); - try std.testing.expectEqualStrings("acme", p.theme().name); + try std.testing.expectEqualStrings(worn, p.theme().name); try std.testing.expectEqualStrings("theme acme", p.prompt_history.command.get(1).?); try std.testing.expectEqualStrings("theme acme", p.registers.value(':', 0).?); try std.testing.expectEqualStrings(rest, try tagOf(p)); @@ -527,3 +691,94 @@ test ": on the empty field gives the keyboard to the tag's own text, and : there try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expectEqual(panes.Pane.Focus.body, pane.focus); } + +test ":theme lists the themes and the whole window wears the current one, none dimmed; Tab steps it on, Esc puts the window's own back, Enter keeps the one worn; :set lists the settings with their values, then a setting's values; :pipe the pipes typed last; :open the recent files" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + _ = try p.setTestFile("one\n"); + const id = p.view().active; + p.newScratchBelow(id); + const other = p.view().active; + p.view().active = id; + const s = &p.view().tag_state; + const own = p.theme(); + const own_chrome = p.chromeTheme().*; + typeKeys(p, ":theme "); + try std.testing.expectEqual(Rows.themes, s.prompt.command.rows); + try std.testing.expectEqual(@as(u16, @intCast(colors.themes.len)), s.nhits); + try std.testing.expect(has(try tagOf(p), try std.fmt.allocPrint(a, " {d} themes · the window wears the current one ", .{colors.themes.len}))); + // The window wears the first: its colours, its chrome; no pane dimmed. + try std.testing.expectEqualStrings(colors.themes[s.hits[0]].name, p.theme().name); + try std.testing.expectEqual(colors.ChromeTheme.fromTheme(p.theme()), p.chromeTheme().*); + try std.testing.expect(s.previewing.isSet(other)); + const surface = try p.render(a); + const r = p.view().rects[other]; + try std.testing.expect(!surface.at(r.x + 3, r.y).style.dim); + // Typed: the themes it matches; the window wears the first of them. + typeKeys(p, "o"); + try std.testing.expect(s.nhits > 2 and s.nhits < colors.themes.len); + try std.testing.expectEqualStrings(colors.themes[s.hits[0]].name, p.theme().name); + // Tab: the current one in the field; again, the next, worn. + p.update(.{ .key = .{ .cp = Key.tab } }); + p.update(.{ .key = .{ .cp = Key.tab } }); + const next = colors.themes[s.hits[1]].name; + try std.testing.expectEqualStrings(try std.fmt.allocPrint(a, "theme {s}", .{next}), s.field.text()); + try std.testing.expectEqualStrings(next, p.theme().name); + try std.testing.expectEqual(colors.ChromeTheme.fromTheme(p.theme()), p.chromeTheme().*); + // A load meanwhile (dump.afterLoad) works out the session's own chrome, + // never the one worn. Esc: the window's own again, nothing set. + colors.finishThemeInitialization(p); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqualStrings(own.name, p.theme().name); + try std.testing.expectEqual(own_chrome, p.chromeTheme().*); + try std.testing.expect(s.worn == null and !s.previewing.isSet(other)); + // Enter keeps the one the window wears. + typeKeys(p, ":theme o"); + p.update(.{ .key = .{ .cp = Key.tab } }); + p.update(.{ .key = .{ .cp = Key.tab } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(s.worn == null); + try std.testing.expectEqualStrings(next, p.theme().name); + try std.testing.expectEqualStrings(next, colors.themes[p.settings.theme].name); + + // :set: the settings, each its value now; a setting and a space, its + // values, the one it has said. + typeKeys(p, ":set "); + try std.testing.expectEqual(Rows.settings, s.prompt.command.rows); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ Wrap ") and has(try bandRow(p, a, 0), " on")); + typeKeys(p, "Wrap "); + try std.testing.expectEqual(Rows.values, s.prompt.command.rows); + try std.testing.expectEqual(@as(u16, 2), s.nhits); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ on now")); + try std.testing.expect(has(try tagOf(p), " : set Wrap = Exec Wrap 2 values ")); + typeKeys(p, "of"); + try std.testing.expect(has(try tagOf(p), " : set Wrap of = Exec Wrap of 1 value ")); + p.update(.{ .key = .{ .cp = Key.tab } }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(!p.settings.wrap); + // :toggle: the settings; with one, nothing more to list. + typeKeys(p, ":toggle Wrap "); + try std.testing.expectEqual(Rows.none, s.prompt.command.rows); + p.update(.{ .key = .{ .cp = Key.escape } }); + + // :pipe: the pipes typed last, newest first. + p.prompt_history.pipe.add("tr a-z A-Z"); + p.prompt_history.pipe.add("sort -u"); + typeKeys(p, ":pipe "); + try std.testing.expectEqual(@as(u16, 2), s.nhits); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ sort -u")); + typeKeys(p, "tr"); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ tr a-z A-Z")); + p.update(.{ .key = .{ .cp = Key.escape } }); + + // :open: the recent files, from the pane's directory. + p.recent.opened(p.recentGpa(), "/elsewhere/notes.txt"); + typeKeys(p, ":open no"); + try std.testing.expectEqual(Rows.files, s.prompt.command.rows); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ /elsewhere/notes.txt")); + try std.testing.expect(has(try tagOf(p), " = Look no 1 recent file ")); + p.update(.{ .key = .{ .cp = Key.escape } }); +} diff --git a/core/Core.zig b/core/Core.zig index 61d8c7e9..a16940e3 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -345,6 +345,8 @@ font_request_taken: bool = false, custom_theme: ?Theme = null, /// bodyChrome's: the theme it was worked out for, and it. body_chrome: ?struct { theme: Theme, chrome: ChromeTheme } = null, +/// The chrome of the theme a view wears (colors.wear): its index, and it. +worn_chrome: ?struct { index: u16, chrome: ChromeTheme } = null, theme_file_path: config.Settings.Text(limits.host_path_cap) = .{}, /// The ThemeFile last loaded whole: what /ctl and DumpConfig name, and /// what a failed ThemeFile goes back to (colors.revertThemeFile). @@ -531,7 +533,7 @@ pub const replica = .{ "opts", "dump_out", "dump_export", "tape", }, - .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, + .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome", "worn_chrome" }, // The host's handles, the state file every session of the user // shares (and Recent read from it), this session's closed panes, and // the files of a host with no file system of its own. (The 9P @@ -2137,12 +2139,15 @@ fn sizes(p: *const Pardes, pane: *const Pane) bool { } pub fn theme(p: *const Pardes) *const Theme { + // A theme the live view wears while it previews one (colors.wear). + if (p.views[p.live]) |*v| if (v.tag_state.worn) |i| return &themes[i]; if (p.custom_theme) |*custom| return custom; return &themes[p.settings.theme]; } /// The colours of the chrome: the theme's, set as it changes. pub fn chromeTheme(p: *const Pardes) *const ChromeTheme { + if (p.views[p.live]) |*v| if (v.tag_state.worn) |i| if (p.worn_chrome) |*w| if (w.index == i) return &w.chrome; return &p.chrome; } diff --git a/core/Prompt.zig b/core/Prompt.zig index 0dfe79ea..20c393bb 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -831,7 +831,7 @@ fn plainWord(w: *std.Io.Writer, op: Op, line: []const u8) !?[]const u8 { .pipe => try w.print("Edit |{s}", .{line}), .pipe_to => try w.print("Edit >{s}", .{line}), .insert, .append, .keep_pipe => return null, - .command => return Command.plainWord(w, line), + .command => return null, // Command.plainWord, which reads the rows } return if (op == .search or op == .search_back) "Look" else "Exec"; } @@ -1017,7 +1017,8 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { if (line.len > 0) { var buf: [Field.max + 16]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); - if (plainWord(&w, pr.op, line) catch null) |verb| { + const said = if (pr.op == .command) Command.plainWord(p, &w, line) else plainWord(&w, pr.op, line) catch null; + if (said) |verb| { try styled(arena, &text, &spans, try std.fmt.allocPrint(arena, " = {s}", .{verb}), .dim); try text.print(arena, " {s}", .{w.buffered()}); } diff --git a/core/TagState.zig b/core/TagState.zig index fea71f2a..a52d5d17 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -32,7 +32,7 @@ const Prompt = @import("Prompt.zig"); /// (Prompt.zig, SPEC §7). pub const Kind = enum(u8) { rest, help, describe, prompt }; -pub const replica = .{ .len = .{ .{ "scope", "scope_n" }, .{ "hits", "nhits" } } }; +pub const replica = .{ .len = .{ .{ "scope", "scope_n" }, .{ "hits", "nhits" } }, .index = .{.{ "worn", "isTheme" }} }; kind: Kind = .rest, /// The pane whose tag a typing state shows in, by serial: the one the @@ -75,9 +75,17 @@ used: [bindings.ngroups]u8 = @splat(0), /// landing pane, a pane an action draws ghosts in), by slot: not dimmed /// while the others are (SPEC §3.6). Set by the state that previews. previewing: std.StaticBitSet(pardes.MAX_PANES) = .empty, +/// The theme the view wears while a state previews themes (colors.wear), +/// an index of colors.themes; null, its own. +worn: ?u16 = null, -/// Help's rows at most: every leaf of every root, and the registry's words. -pub const max_hits = bindings.flat.len + 64; +/// Help's rows at most: every leaf of every root, and the registry's words; +/// as many as there are themes, for `:theme `'s (Command.zig). +pub const max_hits = @max(bindings.flat.len + 64, @import("colors.zig").themes.len); + +pub fn isTheme(_: *const TagState, i: usize) bool { + return i < @import("colors.zig").themes.len; +} /// A root struck, its time not had yet (as Pane.question_asked: the step /// that strikes it reads no clock). @@ -163,6 +171,7 @@ pub fn toRest(p: *Pardes) void { if (s.kind == .prompt) Prompt.leave(p); s.kind = .rest; s.previewing = .empty; + s.worn = null; s.root = null; s.due_ns = 0; s.unrolled = false; diff --git a/core/colors.zig b/core/colors.zig index 84595a30..f0b01c71 100644 --- a/core/colors.zig +++ b/core/colors.zig @@ -764,7 +764,8 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8) bool { } pub fn finishThemeInitialization(p: *Pardes) void { - p.chrome = ChromeTheme.fromTheme(p.theme()); + // The session's own theme's: never one a view wears (`wear`). + p.chrome = ChromeTheme.fromTheme(if (p.custom_theme) |*custom| custom else &themes[p.settings.theme]); } /// Themes near `name`, a line of them, for a Theme given a name there is @@ -832,6 +833,26 @@ pub fn themeIndex(name: []const u8) ?usize { return null; } +/// The live view wears theme `index` while a state previews themes (`:theme +/// `, a Themes picker): the whole window drawn in it, no pane dimmed; null, +/// its own again. Nothing is loaded or set: `theme()` and `chromeTheme()` +/// read what the view wears, and rest (TagState.toRest) takes it off. +pub fn wear(p: *Pardes, index: ?usize) void { + const s = &p.view().tag_state; + s.worn = if (index) |i| (if (i < themes.len) @intCast(i) else null) else null; + s.previewing = if (s.worn != null) .full else .empty; + wornChrome(p); +} + +/// The chrome of the theme the live view wears, worked out once a theme +/// (its contrast searches take pow calls): `wear`'s, and a frame's first +/// step's, for a view that wears another than the last one worked out. +pub fn wornChrome(p: *Pardes) void { + const i = p.view().tag_state.worn orelse return; + if (p.worn_chrome) |w| if (w.index == i) return; + p.worn_chrome = .{ .index = i, .chrome = ChromeTheme.fromTheme(&themes[i]) }; +} + pub fn setThemeIndex(p: *Pardes, index: usize) void { if (index >= themes.len or (p.custom_theme == null and p.theme_file_path.get().len == 0 and diff --git a/core/draw.zig b/core/draw.zig index 0b6cebcf..6f5d5a43 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -324,6 +324,7 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { for (&s.tag_layers) |*layer| layer.rows = 0; s.placements.clearRetainingCapacity(); @memset(&s.gpu_frames, null); + colors.wornChrome(p); const chrome = p.chromeTheme(); s.chrome = .{ .page = p.theme().bg, diff --git a/core/root.zig b/core/root.zig index 903dadce..bf72a32e 100644 --- a/core/root.zig +++ b/core/root.zig @@ -761,7 +761,7 @@ const kept_out = [_]struct { []const u8, []const u8, []const u8 }{ .{ "Core.zig", "needs_frame", "whether to draw: steps set it, the loop reads it; no step's outcome reads it" }, .{ "Core.zig", "effects effects_head effects_len in_q in_head in_len opts dump_out dump_export tape", "this process's queues, options and tape, drained or set by its host" }, .{ "Core.zig", "shell_rows", "a terminal's rows as read here, swept at every update; the terminal's own digest covers its active area, not its history (Term.zig `calls`)" }, - .{ "Core.zig", "diff_output locations_cache tty_filter_palette body_chrome", "caches of pure functions of their key (a text, a theme): a step finds what it would make; a query row's colours from them are out of its digest (locations.Row.digest), and what a step asks a worker to paint is never decided by them (Stream.paintRows)" }, + .{ "Core.zig", "diff_output locations_cache tty_filter_palette body_chrome worn_chrome", "caches of pure functions of their key (a text, a theme): a step finds what it would make; a query row's colours from them are out of its digest (locations.Row.digest), and what a step asks a worker to paint is never decided by them (Stream.paintRows)" }, .{ "Core.zig", "retired_panes", "panes torn down this step, freed at its end" }, .{ "Core.zig", "multi_first multi_stop multi_index multi_count exec_depth exec_column", "a step's own: set by a key's replay across the ranges (normal.replaySels) and a word's run (exec.zig), each put back before the step ends; between steps what `init` makes, so a load takes them as it makes them (a dump's said 2^30 ranges once: a yank sized its register by it)" }, .{ "Core.zig", "rects col_x col_w", "a view's geometry: the layout pass's (layout.compute) over its columns and screen, at every sync and resize, and at a load (dump.afterLoad); never read before it" }, diff --git a/test/perf.zig b/test/perf.zig index 4ba061e1..458910df 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -1854,6 +1854,30 @@ fn measureSearch(io: std.Io) !void { std.debug.print("prompt {s} \"{s}\": {d} instructions, {d} us (key and frame)\n", .{ opener, typed[0 .. n + 1], counter.read() -| was, (nowNs() -| t0) / 1000 }); } }; + + // `:` over the same file (core/Command.zig): each key typed into its + // field with its frame, its rows filtered; then Tab through `:theme + // `'s, the whole window wearing each. + const core = try boot(); + defer core.deinit(); + pardes.look.lookAt(core, 0, mb_path); + pump(core); + _ = try frame(core); + const line = ":theme "; + for (line, 0..) |c, n| { + const was = counter.read(); + const t0 = nowNs(); + core.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + _ = try frame(core); + std.debug.print("colon \"{s}\": {d} instructions, {d} us (key and frame)\n", .{ line[0 .. n + 1], counter.read() -| was, (nowNs() -| t0) / 1000 }); + } + for (0..4) |n| { + const was = counter.read(); + const t0 = nowNs(); + core.update(.{ .key = .{ .cp = pardes.Key.tab } }); + _ = try frame(core); + std.debug.print("colon :theme Tab {d} ({s}): {d} instructions, {d} us (key and frame)\n", .{ n + 1, core.theme().name, counter.read() -| was, (nowNs() -| t0) / 1000 }); + } } /// The longest of a scan's calls. -- cgit v1.3 From c9a5d230570863beb8183190d01c7e19c7696a14 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 25 Nov 2026 00:52:21 -0300 Subject: Prompt: `:` has a history of its own, Up and Down recalling the lines its Enter ran and C-r unrolling them before its rows (walk UI, pick-ui SPEC.md §7.2 and §8, item 3 of line E2): Up and Down bring `:`'s ring's lines into the field (Pardes.prompt_history.command, 16 lines, newest first; the search and select prompts' and the pipes' are their own), its rows made again for the line recalled (`set Wrap off` lists Wrap's values); Ctrl-r unrolls the ring as a step, the lines holding the field, now the band's FIRST step for `:` (Prompt.steps): after the rows and their doc it was placed past the doc, which takes the width the others leave, so it was never seen; `:` on the empty field gives the keyboard to the tag's own text and `:` there gives it back (item 1); Esc in a tag keeps today's meaning (Last), as four tests pin it; TEST FIRST: Command.zig ": has a history of its own: Up and Down bring back the lines Enter ran, newest first; C-r unrolls them beside the rows, filtered by the field; a search's lines are not among them" (on item 2: the band's first step was the rows, `Save in the tag` where `goto 2` was looked for); core-test 1023/1190 (167 skipped) Co-Authored-By: Claude Opus 5.5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Command.zig | 41 +++++++++++++++++++++++++++++++++++++++++ core/Prompt.zig | 7 ++++++- 2 files changed, 47 insertions(+), 1 deletion(-) (limited to 'core/Command.zig') diff --git a/core/Command.zig b/core/Command.zig index 5364b898..4a10e869 100644 --- a/core/Command.zig +++ b/core/Command.zig @@ -782,3 +782,44 @@ test ":theme lists the themes and the whole window wears the current one, none d try std.testing.expect(has(try tagOf(p), " = Look no 1 recent file ")); p.update(.{ .key = .{ .cp = Key.escape } }); } + +test ": has a history of its own: Up and Down bring back the lines Enter ran, newest first; C-r unrolls them beside the rows, filtered by the field; a search's lines are not among them" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const pane = try p.setTestFile("one\ntwo\n"); + const s = &p.view().tag_state; + typeKeys(p, ":set Wrap off\r:goto 2\r/one\r"); + try std.testing.expectEqualStrings("one", p.prompt_history.search.get(1).?); + typeKeys(p, ":"); + p.update(.{ .key = .{ .cp = Key.up } }); + try std.testing.expectEqualStrings("goto 2", s.field.text()); + try std.testing.expect(has(try tagOf(p), " : goto 2 = Look :2 ")); + p.update(.{ .key = .{ .cp = Key.up } }); + try std.testing.expectEqualStrings("set Wrap off", s.field.text()); + try std.testing.expectEqual(Rows.values, s.prompt.command.rows); + p.update(.{ .key = .{ .cp = Key.up } }); + try std.testing.expectEqualStrings("set Wrap off", s.field.text()); + p.update(.{ .key = .{ .cp = Key.down } }); + p.update(.{ .key = .{ .cp = Key.down } }); + try std.testing.expectEqualStrings("", s.field.text()); + try std.testing.expectEqual(Rows.names, s.prompt.command.rows); + // C-r: the history beside the rows, its first step, the doc still + // in the band's width after them. + p.update(.{ .key = .{ .cp = 'r', .ctrl = true } }); + var band = (try Band.of(p, a, pane, p.view().active)).?; + try std.testing.expectEqual(@as(u8, 3), band.nsteps); + try std.testing.expectEqualStrings("goto 2", band.steps[0].rows[0].text); + try std.testing.expectEqualStrings("set Wrap off", band.steps[0].rows[1].text); + try std.testing.expectEqual(@as(usize, 2), band.steps[0].rows.len); + try std.testing.expect(band.steps[2].doc and band.steps[2].x < band.columns); + // Typed: the history's lines holding it. + typeKeys(p, "set"); + band = (try Band.of(p, a, pane, p.view().active)).?; + try std.testing.expectEqual(@as(usize, 1), band.steps[0].rows.len); + try std.testing.expectEqualStrings("set Wrap off", band.steps[0].rows[0].text); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); +} diff --git a/core/Prompt.zig b/core/Prompt.zig index 20c393bb..3dc13e1f 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -1145,7 +1145,12 @@ fn historyRows(p: *Pardes) u16 { pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.Step) ![]Band.Step { const s = &p.view().tag_state; var made: usize = 0; - if (s.prompt.op == .command) made = try Command.steps(p, arena, out); + // `:`'s history before its rows and their doc, which takes the width + // left after the others. + if (s.prompt.op == .command) { + if (s.prompt.history) made = try historyStep(p, arena, &out[0]); + return out[0 .. made + try Command.steps(p, arena, out[made..])]; + } if (s.prompt.op == .pipe_to) if (p.panes[TagState.hostOf(p) orelse return out[0..0]]) |pane| if (answered(p, pane, s.field.text())) |l| { var rows: std.ArrayList(Band.Row) = .empty; for (l.outs, l.codes, 0..) |o, c, i| { -- cgit v1.3 From 09729fc927bca5f05f99de7844069cae445598a1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 25 Nov 2026 02:03:41 -0300 Subject: Command: a question waiting on the pane lists its answers as `:`'s first rows, a letter typed only filtering them and Tab or a click giving one; `:open ` lists the files the panes show; and the faults the gates met (walk UI, pick-ui SPEC.md §10.4, item 4 of line E2): with a question waiting on the pane (Messages.waitingAnswers) its answers are the names' first rows, each after its key in the key ink (`r Reload`, `k Keep mine`, `d Diff`), `answers the question` on their right, the last ruled off from the tag's words as theirs is from the commands; typing filters them as the rest (Names.match on the word), so a letter never answers (`:r` is a line, `:reflow` runs reflow); Enter gives one when the current row is an answer Tab or a click put in the field, or its whole word typed, the tag at rest first (Messages.waiting, Messages.answer); a click (B1, TagState.point) on an answer gives it at once, and on a line of C-r's history puts that line in the field (Prompt.history_row marks a history row's index: a click there was read as an index of the rows); a damaged image's cursor past the rows is held to the last (Command.current); THE STAND-IN: this tree has no questions: Messages.zig's block from `---- a stand-in for line C's questions` to `---- the stand-in's end ----` has `Answer`, `waiting`, `waitingAnswers` and `answer` in the shapes of line C's (vxmpyyst), saying none, and root.zig's allowed_vars line `Messages.zig stub_asked` its test's question: THE MERGE WITH C DELETES BOTH and keeps C's; Command.zig's test asks through the stand-in here and, by `comptime @hasDecl(Messages, "stub_asked")`, through a file changed on disk under unsaved text in C's tree (`ask`, as C's own test asks), nothing to edit; THE FAULT THE SOAK MET (item 2's): `:open ` listed the session's recent files, which are its alone (Core.zig `.authority`): random keys typed `:o ` and the session listed them where a client listed none, so the band under the tag took other rows there (pane tag_rows, rows, the view's rects: the witness's names) and a PDF pane's kind diverged after its view (rtt 30: diverged 31, the tape's answers to the addon short after); now the files the panes show, each once (Command.fileOf, firstOf: replicated, 64 at most); THE FAULT THE 40,000-SEED FUZZ MET (in no code of this line: the dump's layout moved, TagState.worn and Prompt.command, and a seed met it): a dump whose fs.Namespace `opens_len` said 98 of its 64 opens was refused (walk.holdTo), and the load's errdefer (dump.zig loadKnowing) tore the namespace down over `opens[0..opens_len]`: index out of bounds, a panic where a refusal was due; Namespace.deinit holds the length to its opens now; a user restoring a damaged dump could have hit it; TEST FIRST: Command.zig "a question waiting on the pane lists its answers first, each after its key, ruled off; a letter typed only filters them; Tab onto one and Enter gives it" (a click too), fs.zig "a namespace a refused dump left saying more opens than it holds is torn down to its last" (shell-test), item 3's test clicks a history line, item 2's lists the open file; core-test 1025/1192 (167 skipped), shell-test 1239/1242, pane-test 182/184, behaviour-test and its replay 274/277 (the failures A's, as on E's tip), process-test 5 of 146 (the base's: find, builtins, leader, tagnav, ttyhelp), term-test 17/17, kitty-test 45/45; the fuzz at 40,000 join and 1,779 image seeds over a session left in `:theme o` after a Tab (TagState.worn set): pass; THE SOAK, 5 clients, FAILS: diverged 32/0/2 at rtt 30/80/207 (seeds 0-2: a PDF pane's kind state after its view, then the tape's answers to it short; the witness names the band's host pane's and the PDF pane's tag_rows, rows and tag_lead, and the view's rects), and inputs `the mouse in a kind's pane` that may_wait does not list; with `:`'s band held at 0 rows (Command.takes, a temporary edit, put back) the same soak is clean: diverged 0 at 30/80/207, no stop off may_wait: the fault is the band's (A's layout term) in a session with a kind's pane and clients, which no input of the soak reached before `:` (typed by its random keys) opened one; not fixed here Co-Authored-By: Claude Opus 5.5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Command.zig | 265 ++++++++++++++++++++++++++++++++++++++++++------------ core/Messages.zig | 40 +++++++++ core/Prompt.zig | 6 +- core/fs.zig | 10 ++- core/root.zig | 1 + 5 files changed, 264 insertions(+), 58 deletions(-) (limited to 'core/Command.zig') diff --git a/core/Command.zig b/core/Command.zig index 4a10e869..6814a0e3 100644 --- a/core/Command.zig +++ b/core/Command.zig @@ -1,7 +1,8 @@ //! `:` (walk UI, pick-ui SPEC.md §8): a line typed in the focused pane's //! tag after a `:` key cell, a text prompt of its own (Prompt.zig's -//! `.command`). Its rows are the tag's own words in the tag's order (`Kill` -//! after them for a terminal), marked `in the tag` and ruled off, then the +//! `.command`). Its rows are the answers of a question waiting on the pane +//! (SPEC §10.4), then the tag's own words in the tag's order (`Kill` after +//! them for a terminal), marked `in the tag`, each kind ruled off, then the //! commands a helix hand types, each the pardes word it runs: `write` //! Save, `quit` Del, `theme X` Theme X, `set` and `toggle` a setting's //! word, `reload` Get, `goto N` Look `:N`, `open F` Look F, `sort`, @@ -12,11 +13,11 @@ //! its argument the rows are what it takes, the rest of the line the //! query: `:theme ` the themes, the whole window wearing the current one //! (colors.wear); `:set ` the settings and their values now, then `:set -//! Wrap ` its values; `:open ` the recent files; `:pipe ` the pipes typed -//! last. Enter runs the line as B2 runs a word: a command as its pardes -//! word (`:theme`'s the theme the window wears), anything else Exec'd. `:` -//! on the empty field gives the keyboard to the tag's own text, as `:` did -//! before there was a field; `:` there gives it back. +//! Wrap ` its values; `:open ` the files the panes show; `:pipe ` the +//! pipes typed last. Enter runs the line as B2 runs a word: a command as +//! its pardes word (`:theme`'s the theme the window wears), anything else +//! Exec'd. `:` on the empty field gives the keyboard to the tag's own text, +//! as `:` did before there was a field; `:` there gives it back. //! //! A view's own, as its prompt is (TagState.prompt.command): which rows //! the hits (TagState.hits) are of, and where in the field Tab writes. @@ -36,6 +37,7 @@ const TagState = @import("TagState.zig"); const Prompt = @import("Prompt.zig"); const Pipe = @import("Pipe.zig"); const colors = @import("colors.zig"); +const Messages = @import("Messages.zig"); const Pardes = pardes.Pardes; const Pane = panes.Pane; const File = panes.File; @@ -44,17 +46,19 @@ const Key = pardes.Key; /// What the rows (TagState.hits) are of: the names a line starts with /// (the tag's words, then the commands); a command's argument (themes, by /// their index in colors.themes; settings, in config.Settings.settings; -/// the values of `setting`; the recent files, newest first; the pipe -/// history, newest first); or none, an argument that is no list (`goto`'s -/// line). +/// the values of `setting`; the files the panes show, by the pane's slot; +/// the pipe history, newest first); or none, an argument that is no list +/// (`goto`'s line). pub const Rows = enum(u8) { names, themes, settings, values, files, pipes, none }; rows: Rows = .names, /// `:set X `'s setting, by its index in config.Settings.settings: the /// values are its. setting: u8 = 0, -/// The names' first rows are the tag's words: how many there were when -/// the field was filtered (the commands' come after them). +/// The names' first rows are the waiting question's answers, then the +/// tag's words: how many of each there were when the field was filtered +/// (the commands' come after them). +answers: u8 = 0, words: u8 = 0, /// Where the query starts in the field: Tab writes the current row from /// there. @@ -81,8 +85,9 @@ const typed = [_]Typed{ .{ .name = "toggle", .arg = "setting", .rows = .settings, .doc = "Flip a setting, or step it to its next value: its word bare." }, }; -/// The tag's words `:` lists at most. +/// The tag's words `:` lists at most, and a question's answers. const max_words = 32; +const max_answers = 8; /// A row's text at most, its name and what it says beside it. const row_max = 96; /// A doc's rows, the fewest the names' band has. @@ -141,6 +146,7 @@ pub fn filter(p: *Pardes, pane: *Pane) void { const l = read(line); c.completed = false; c.from = 0; + c.answers = 0; c.words = 0; s.rows = .{}; s.nhits = 0; @@ -165,13 +171,20 @@ pub fn filter(p: *Pardes, pane: *Pane) void { switch (c.rows) { .none => {}, .names => { + // A question waiting on the pane: its answers first (SPEC + // §10.4), a row each, which a letter typed only filters. + for (p.messages.waitingAnswers(pane)) |ans| { + if (c.answers == max_answers) break; + consider(s, q, ans.word, c.answers); + c.answers += 1; + } var words = tagWords(pane); while (words.next()) |w| { if (c.words == max_words) break; - consider(s, q, w, c.words); + consider(s, q, w, c.answers + c.words); c.words += 1; } - for (typed, 0..) |t, i| consider(s, q, t.name, c.words + i); + for (typed, 0..) |t, i| consider(s, q, t.name, c.answers + c.words + i); }, .themes => for (colors.themes, 0..) |t, i| consider(s, q, t.name, i), .settings => { @@ -183,9 +196,12 @@ pub fn filter(p: *Pardes, pane: *Pane) void { var i: usize = 0; while (values.next()) |v| : (i += 1) consider(s, q, v, i); }, + // The files the panes show, each once: the session's recent files + // are its alone (`.authority`), which a client replaying the key + // would not list. .files => { const dir = panes.paneDir(pane); - for (p.recent.entries.items, 0..) |e, i| consider(s, q, shownPath(e.path, dir), i); + for (0..p.panes.len) |i| if (fileOf(p, i)) |path| if (firstOf(p, i, path)) consider(s, q, shownPath(path, dir), i); }, .pipes => { const r = &p.prompt_history.pipe; @@ -207,20 +223,41 @@ fn valuesOf(k: u8) std.mem.SplitIterator(u8, .sequence) { return std.mem.splitSequence(u8, words, ", "); } -/// A recent file as its row shows it: under the pane's directory, from -/// there. +/// The file pane `i` shows, a text's on disk (not a listing or an +/// output). +fn fileOf(p: *const Pardes, i: usize) ?[]const u8 { + const other = p.panes[i] orelse return null; + const f = other.file orelse return null; + if (f.output != null or f.listing != null or f.path.len == 0) return null; + return f.path; +} + +/// Whether pane `i` is the first that shows `path`. +fn firstOf(p: *const Pardes, i: usize, path: []const u8) bool { + for (0..i) |k| if (fileOf(p, k)) |other| if (std.mem.eql(u8, other, path)) return false; + return true; +} + +/// A file as its row shows it: under the pane's directory, from there. fn shownPath(path: []const u8, dir: []const u8) []const u8 { const d = std.mem.trimEnd(u8, dir, "/"); if (d.len > 0 and path.len > d.len + 1 and std.mem.startsWith(u8, path, d) and path[d.len] == '/') return path[d.len + 1 ..]; return path; } +/// The current row's place in its source: none without rows, the last +/// for a cursor past them (a damaged image's). +fn current(s: *const TagState) ?usize { + if (s.nhits == 0) return null; + return s.hits[@min(s.rows.at, s.nhits - 1)]; +} + /// The window wears the current row while the rows are themes; else its /// own theme. fn preview(p: *Pardes) void { const s = &p.view().tag_state; const c = &s.prompt.command; - const worn: ?usize = if (c.rows == .themes and s.nhits > 0) s.hits[s.rows.at] else null; + const worn: ?usize = if (c.rows == .themes) current(s) else null; if (worn == null and s.worn == null) return; colors.wear(p, worn); } @@ -263,16 +300,17 @@ fn tagWords(pane: *Pane) Words { return .{ .words = std.mem.tokenizeAny(u8, tagline.curTail(pane), " \t\n"), .kill = pane.isTerminal() and pane.command == null }; } -/// What row `at` of the rows stands for, as Tab writes it: a tag's word, -/// a command's name, a theme's, a setting's word, a value, a recent file, -/// a pipe; null past them (a damaged image's). +/// What row `at` of the rows stands for, as Tab writes it: an answer's +/// word, a tag's word, a command's name, a theme's, a setting's word, a +/// value, a file a pane shows, a pipe; null past them (a damaged image's). fn rowOf(p: *Pardes, pane: *Pane, c: *const Command, at: usize) ?[]const u8 { switch (c.rows) { .none => return null, .names => { - if (at >= c.words) return if (at - c.words < typed.len) typed[at - c.words].name else null; + if (answerAt(p, pane, c, at)) |ans| return ans.word; + if (typedAt(c, at)) |t| return typed[t].name; var words = tagWords(pane); - var i: usize = 0; + var i: usize = c.answers; while (words.next()) |w| : (i += 1) if (i == at) return w; return null; }, @@ -284,11 +322,25 @@ fn rowOf(p: *Pardes, pane: *Pane, c: *const Command, at: usize) ?[]const u8 { while (values.next()) |v| : (i += 1) if (i == at) return v; return null; }, - .files => return if (at < p.recent.entries.items.len) shownPath(p.recent.entries.items[at].path, panes.paneDir(pane)) else null, + .files => return shownPath(fileOf(p, if (at < p.panes.len) at else return null) orelse return null, panes.paneDir(pane)), .pipes => return p.prompt_history.pipe.get(@intCast(@min(at + 1, Prompt.Ring.max + 1))), } } +/// The answer row `at` of the names is, if it is one. +fn answerAt(p: *const Pardes, pane: *const Pane, c: *const Command, at: usize) ?Messages.Answer { + if (c.rows != .names or at >= c.answers) return null; + const all = p.messages.waitingAnswers(pane); + return if (at < all.len) all[at] else null; +} + +/// The command row `at` of the names is, by its index in `typed`. +fn typedAt(c: *const Command, at: usize) ?usize { + if (c.rows != .names or at < @as(usize, c.answers) + c.words) return null; + const t = at - c.answers - c.words; + return if (t < typed.len) t else null; +} + /// Tab (`by` 1) and S-Tab (-1): the next row, or the last, written into /// the field from where the query starts, a command's name with a space /// after it; the rows stay the line's from before. @@ -307,13 +359,13 @@ fn step(p: *Pardes, pane: *Pane, by: i32) void { fn fill(p: *Pardes, pane: *Pane) void { const s = &p.view().tag_state; const c = &s.prompt.command; - const at = s.hits[s.rows.at]; + const at = current(s) orelse return; const name = rowOf(p, pane, c, at) orelse return; var line: [Field.max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); w.writeAll(s.field.text()[0..@min(c.from, s.field.len)]) catch return; w.writeAll(name) catch return; - if (c.rows == .names and at >= c.words) w.writeByte(' ') catch return; + if (typedAt(c, at) != null) w.writeByte(' ') catch return; s.field.set(w.buffered()); c.completed = true; s.prompt.recall = 0; @@ -321,12 +373,24 @@ fn fill(p: *Pardes, pane: *Pane) void { } /// A click on row `index` of the rows: it is current, written into the -/// field as Tab writes it. +/// field as Tab writes it; an answer is given (SPEC §10.4). On a line of +/// C-r's history, that line in the field, as Up brings it. pub fn point(p: *Pardes, index: u16) void { const s = &p.view().tag_state; const pane = p.panes[TagState.hostOf(p) orelse return] orelse return; + if (index & Prompt.history_row != 0) { + const back: u8 = @truncate(index); + s.field.set(p.prompt_history.command.get(back) orelse return); + s.prompt.recall = back; + return filter(p, pane); + } if (index >= s.nhits) return; s.rows.at = index; + if (answerAt(p, pane, &s.prompt.command, s.hits[index])) |ans| { + TagState.toRest(p); + if (p.messages.waiting(pane)) |i| _ = Messages.answer(p, i, ans.key); + return; + } fill(p, pane); } @@ -398,6 +462,15 @@ fn allDigits(s: []const u8) bool { /// and what it says run, in pane `id`, as B2 runs a word. fn run(p: *Pardes, id: usize, pane: *Pane) void { const s = &p.view().tag_state; + // The current row an answer, written into the field by Tab or a click + // or typed whole: given, nothing run (a letter typed never answers). + if (current(s)) |at| if (answerAt(p, pane, &s.prompt.command, at)) |ans| { + if (s.prompt.command.completed or std.mem.eql(u8, s.field.text(), ans.word)) { + TagState.toRest(p); + if (p.messages.waiting(pane)) |i| _ = Messages.answer(p, i, ans.key); + return; + } + }; var kept: [Field.max]u8 = undefined; const line = kept[0..s.field.len]; @memcpy(line, s.field.text()); @@ -445,7 +518,7 @@ pub fn count(p: *Pardes, arena: std.mem.Allocator, text: *std.ArrayList(u8)) !vo .themes => try text.print(arena, " {d} theme{s} · the window wears the current one", .{ s.nhits, if (s.nhits == 1) "" else "s" }), .settings => try text.print(arena, " {d} setting{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), .values => try text.print(arena, " {d} value{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), - .files => try text.print(arena, " {d} recent file{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), + .files => try text.print(arena, " {d} file{s} open", .{ s.nhits, if (s.nhits == 1) "" else "s" }), .pipes => try text.print(arena, " {d} pipe{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }), .none => { const l = read(s.field.text()); @@ -487,15 +560,19 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { const q = if (c.completed) "" else if (c.rows == .names) read(line).name else line[@min(c.from, line.len)..]; const now = if (c.rows == .settings) try pardes.ctl.settingsText(p) else ""; var widest: usize = 0; - for (s.hits[win.lo..win.hi]) |at| widest = @max(widest, File.displayWidth(tab, rowOf(p, pane, c, at) orelse "") + aliasWidth(c, at)); + for (s.hits[win.lo..win.hi]) |at| widest = @max(widest, File.displayWidth(tab, rowOf(p, pane, c, at) orelse "") + aliasWidth(p, pane, c, at)); const rows = try arena.alloc(Band.Row, win.hi - win.lo); for (rows, win.lo..) |*row, i| { const at = s.hits[i]; const name = rowOf(p, pane, c, at) orelse ""; var text: std.ArrayList(u8) = .empty; + // An answer after its key, in the key ink: `r Reload`. + const ans = answerAt(p, pane, c, at); + if (ans) |x| try text.print(arena, "{c} ", .{x.key}); + const lead: u16 = @intCast(text.items.len); try text.appendSlice(arena, name); - if (c.rows == .names and at >= c.words and typed[at - c.words].alias.len > 0) try text.print(arena, " {s}", .{typed[at - c.words].alias}); - const said = try saidOf(p, arena, pane, c, at, name, now); + if (typedAt(c, at)) |t| if (typed[t].alias.len > 0) try text.print(arena, " {s}", .{typed[t].alias}); + const said = try saidOf(p, arena, c, at, name, now); if (said.len > 0) { for (0..widest + 2 - File.displayWidth(tab, text.items)) |_| try text.append(arena, ' '); try text.appendSlice(arena, said); @@ -506,25 +583,27 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: []Band.Step) !usize { var low: [row_max]u8 = undefined; const t = name[0..@min(name.len, low.len)]; _ = Names.match(q, t, std.ascii.lowerString(&low, t), 0, &marks, &nmarks); + for (marks[0..nmarks]) |*m| m.* += lead; } - // The tag's last word ruled off from the commands under it. - const last_word = c.rows == .names and at + 1 == c.words and i + 1 < s.nhits; - row.* = .{ .text = text.items, .on = i == s.rows.at, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .under = last_word }; + // The answers' last and the tag's last word ruled off from what is + // under them. + const last = c.rows == .names and i + 1 < s.nhits and (at + 1 == c.answers or at + 1 == @as(usize, c.answers) + c.words); + row.* = .{ .text = text.items, .on = i == s.rows.at, .key = if (ans != null) 1 else 0, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .under = last }; } out[0] = .{ .rows = rows }; if (c.rows != .names) return 1; - out[1] = .{ .rows = try docRows(p, arena, pane, c, s.hits[s.rows.at]), .doc = true }; + out[1] = .{ .rows = try docRows(p, arena, pane, c, current(s).?), .doc = true }; return 2; } /// What row `at` says on its right: a tag's word `in the tag`, what a /// command takes, the theme in use, a setting's value now (its line of -/// the settings' text `now`), the value it has, a file open in a pane. -fn saidOf(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, c: *const Command, at: usize, name: []const u8, now: []const u8) ![]const u8 { +/// the settings' text `now`), the value it has. +fn saidOf(p: *Pardes, arena: std.mem.Allocator, c: *const Command, at: usize, name: []const u8, now: []const u8) ![]const u8 { switch (c.rows) { .names => { - if (at < c.words) return "in the tag"; - const t = typed[at - c.words]; + if (at < c.answers) return "answers the question"; + const t = typed[typedAt(c, at) orelse return "in the tag"]; return if (t.arg.len > 0) try std.fmt.allocPrint(arena, "<{s}>", .{t.arg}) else ""; }, .themes => return if (p.custom_theme == null and at == p.settings.theme) "in use" else "", @@ -538,11 +617,7 @@ fn saidOf(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, c: *const Command, const value = valueNow(p, c.setting, &buf); return if (std.mem.eql(u8, value, name)) "now" else ""; }, - .files => { - _ = pane; - return if (at < p.recent.entries.items.len and p.recent.entries.items[at].open) "open" else ""; - }, - .pipes, .none => return "", + .files, .pipes, .none => return "", } } @@ -561,9 +636,11 @@ fn valueNow(p: *Pardes, k: u8, buf: []u8) []const u8 { return std.fmt.bufPrint(buf, "{s}", .{value}) catch ""; } -fn aliasWidth(c: *const Command, at: usize) usize { - if (c.rows != .names or at < c.words) return 0; - const alias = typed[at - c.words].alias; +/// The cells a row takes besides its name: an answer's key, a command's +/// short name. +fn aliasWidth(p: *const Pardes, pane: *const Pane, c: *const Command, at: usize) usize { + if (answerAt(p, pane, c, at) != null) return 2; + const alias = typed[typedAt(c, at) orelse return 0].alias; return if (alias.len == 0) 0 else alias.len + 1; } @@ -572,8 +649,13 @@ fn aliasWidth(c: *const Command, at: usize) usize { fn docRows(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, c: *const Command, at: usize) ![]const Band.Row { const rows = try arena.alloc(Band.Row, 2); const none = std.math.maxInt(u16); - if (at >= c.words) { - const t = typed[at - c.words]; + if (answerAt(p, pane, c, at)) |ans| { + rows[0] = .{ .text = try std.fmt.allocPrint(arena, "{c} {s}", .{ ans.key, ans.word }), .key = 1, .index = none }; + rows[1] = .{ .text = "An answer to the question waiting on this pane: ⏎ gives it.", .index = none }; + return rows; + } + if (typedAt(c, at)) |k| { + const t = typed[k]; const head = try std.fmt.allocPrint(arena, ":{s}{s}{s}{s}", .{ t.name, if (t.alias.len > 0) " " else "", t.alias, if (t.arg.len > 0) try std.fmt.allocPrint(arena, " <{s}>", .{t.arg}) else "" }); rows[0] = .{ .text = head, .key = @intCast(1 + t.name.len), .index = none }; rows[1] = .{ .text = t.doc, .index = none }; @@ -692,7 +774,7 @@ test ": on the empty field gives the keyboard to the tag's own text, and : there try std.testing.expectEqual(panes.Pane.Focus.body, pane.focus); } -test ":theme lists the themes and the whole window wears the current one, none dimmed; Tab steps it on, Esc puts the window's own back, Enter keeps the one worn; :set lists the settings with their values, then a setting's values; :pipe the pipes typed last; :open the recent files" { +test ":theme lists the themes and the whole window wears the current one, none dimmed; Tab steps it on, Esc puts the window's own back, Enter keeps the one worn; :set lists the settings with their values, then a setting's values; :pipe the pipes typed last; :open the files the panes show" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); defer p.deinit(); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); @@ -774,12 +856,11 @@ test ":theme lists the themes and the whole window wears the current one, none d try std.testing.expect(has(try bandRow(p, a, 0), "▶ tr a-z A-Z")); p.update(.{ .key = .{ .cp = Key.escape } }); - // :open: the recent files, from the pane's directory. - p.recent.opened(p.recentGpa(), "/elsewhere/notes.txt"); - typeKeys(p, ":open no"); + // :open: the files the panes show, from the pane's directory. + typeKeys(p, ":open te"); try std.testing.expectEqual(Rows.files, s.prompt.command.rows); - try std.testing.expect(has(try bandRow(p, a, 0), "▶ /elsewhere/notes.txt")); - try std.testing.expect(has(try tagOf(p), " = Look no 1 recent file ")); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ /test.txt")); + try std.testing.expect(has(try tagOf(p), " = Look te 1 file open ")); p.update(.{ .key = .{ .cp = Key.escape } }); } @@ -820,6 +901,78 @@ test ": has a history of its own: Up and Down bring back the lines Enter ran, ne band = (try Band.of(p, a, pane, p.view().active)).?; try std.testing.expectEqual(@as(usize, 1), band.steps[0].rows.len); try std.testing.expectEqualStrings("set Wrap off", band.steps[0].rows[0].text); + // A click on it: the line in the field, its rows made for it. + TagState.point(p, band.steps[0].rows[0].index); + try std.testing.expectEqualStrings("set Wrap off", s.field.text()); + try std.testing.expectEqual(Rows.values, s.prompt.command.rows); p.update(.{ .key = .{ .cp = Key.escape } }); try std.testing.expectEqual(TagState.Kind.rest, s.kind); } + +/// A question waiting on `pane`: in this tree the stand-in's +/// (Messages.zig), in line C's a file changed on disk under its unsaved +/// text, as C's own test asks one. +fn ask(p: *Pardes, pane: *Pane, tmp: *std.testing.TmpDir, disk: []const u8) !void { + if (comptime @hasDecl(Messages, "stub_asked")) { + Messages.stub_asked = pane.serial; + return; + } + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.zig", .data = "one\ntwo\n" }); + var dir: [4096]u8 = undefined; + const path = try std.fmt.allocPrint(p.gpa, "{s}/c.zig", .{dir[0..try tmp.dir.realPath(std.testing.io, &dir)]}); + p.gpa.free(pane.file.?.path); + pane.file.?.path = path; + const f = &pane.file.?; + f.saved_hash = File.fileHash(f); + typeKeys(p, "Gofour\x1b"); + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "c.zig", .data = disk }); + p.update(.{ .file_changed = .{ .pane = .at(p.view().active), .bytes = disk } }); +} + +test "a question waiting on the pane lists its answers first, each after its key, ruled off; a letter typed only filters them; Tab onto one and Enter gives it" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + defer if (comptime @hasDecl(Messages, "stub_asked")) { + Messages.stub_asked = 0; + }; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const pane = try p.setTestFile("one\ntwo\n"); + try ask(p, pane, &tmp, "zero\none\ntwo\n"); + try std.testing.expect(p.messages.waiting(pane) != null); + const s = &p.view().tag_state; + typeKeys(p, ":"); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ r Reload ")); + try std.testing.expect(has(try bandRow(p, a, 0), "answers the question")); + try std.testing.expect(has(try bandRow(p, a, 2), " d Diff ")); + const band = (try Band.of(p, a, pane, p.view().active)).?; + try std.testing.expect(band.steps[0].rows[2].under and !band.steps[0].rows[1].under); + try std.testing.expect(has(band.steps[0].rows[3].text, "Save")); + // A letter typed filters them: it never answers. + typeKeys(p, "r"); + try std.testing.expectEqualStrings("r", s.field.text()); + try std.testing.expect(p.messages.waiting(pane) != null); + p.update(.{ .key = .{ .cp = Key.escape } }); + // A click on `Keep mine`: given. + typeKeys(p, ":"); + TagState.point(p, 1); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); + try std.testing.expect(p.messages.waiting(pane) == null); + // Asked again: Tab onto `Keep mine`, Enter: given. + try ask(p, pane, &tmp, "zero\nzero\none\ntwo\n"); + try std.testing.expect(p.messages.waiting(pane) != null); + typeKeys(p, ":kee"); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ k Keep mine")); + p.update(.{ .key = .{ .cp = Key.tab } }); + try std.testing.expectEqualStrings("Keep mine", s.field.text()); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(TagState.Kind.rest, s.kind); + try std.testing.expect(p.messages.waiting(pane) == null); + // None waiting: none listed. + typeKeys(p, ":"); + try std.testing.expect(has(try bandRow(p, a, 0), "▶ Save ")); + p.update(.{ .key = .{ .cp = Key.escape } }); +} diff --git a/core/Messages.zig b/core/Messages.zig index 6b406e91..53d2951b 100644 --- a/core/Messages.zig +++ b/core/Messages.zig @@ -544,6 +544,46 @@ pub fn bands(p: *const Pardes, v: *const Pardes.View, pane: *const Pane, tag_row return n; } +// ---- a stand-in for line C's questions (walk UI; C's change vxmpyyst) ---- +// +// `:` lists the answers of a question waiting on its pane first (pick-ui +// SPEC §10.4, Command.zig). This tree has no such questions, and these say +// none: THE MERGE with C's line deletes this block, to its end marker, and +// keeps C's `Answer`, `waiting`, `waitingAnswers` and `answer`, whose +// shapes these are. `stub_asked` lets Command.zig's test ask one here. + +/// A question's answer: the key that gives it and its word. +pub const Answer = struct { key: u8, word: []const u8 }; + +/// The question waiting on `pane`, an index of the live ones: none. +pub fn waiting(m: *const Messages, pane: *const Pane) ?usize { + _ = m; + return if (@import("builtin").is_test and stub_asked != 0 and stub_asked == pane.serial) 0 else null; +} + +/// The answers of the question waiting on `pane`: none. +pub fn waitingAnswers(m: *const Messages, pane: *const Pane) []const Answer { + return if (m.waiting(pane) != null) &stub_answers else &.{}; +} + +/// Answer live question `i` with `key`: false for a key that is none of +/// its answers. +pub fn answer(p: *Pardes, i: usize, key: u8) bool { + _ = p; + _ = i; + for (stub_answers) |a| if (a.key == key) { + stub_asked = 0; + return true; + }; + return false; +} + +/// A test's question: the serial of the pane it waits on. +pub var stub_asked: u32 = 0; +const stub_answers = [_]Answer{ .{ .key = 'r', .word = "Reload" }, .{ .key = 'k', .word = "Keep mine" }, .{ .key = 'd', .word = "Diff" } }; + +// ---- the stand-in's end ---- + test "errors are said in words, and a ZON failure names its line" { var buf: [128]u8 = undefined; try std.testing.expectEqualStrings("file not found", errorWords(error.FileNotFound, &buf)); diff --git a/core/Prompt.zig b/core/Prompt.zig index 3dc13e1f..83432cae 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -1170,6 +1170,10 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.St return out[0 .. made + try historyStep(p, arena, &out[made])]; } +/// A history step's row's index: its line's place in the ring with this +/// bit, which a click tells from the other steps' rows (Command.point). +pub const history_row: u16 = 0x8000; + /// The history step (Ctrl-r): its lines newest first, those holding the /// field, the one Up recalled on. fn historyStep(p: *Pardes, arena: std.mem.Allocator, out: *Band.Step) !usize { @@ -1181,7 +1185,7 @@ fn historyStep(p: *Pardes, arena: std.mem.Allocator, out: *Band.Step) !usize { while (back <= r.n and rows.items.len < Band.Cursor.shown) : (back += 1) { const l = r.get(back).?; if (std.mem.indexOf(u8, l, q) == null) continue; - try rows.append(arena, .{ .text = l, .on = back == s.prompt.recall, .index = back }); + try rows.append(arena, .{ .text = l, .on = back == s.prompt.recall, .index = history_row | back }); } if (rows.items.len == 0) try rows.append(arena, .{ .text = if (r.n == 0) "no history yet" else "no line holds it", .dim = true }); out.* = .{ .rows = rows.items }; diff --git a/core/fs.zig b/core/fs.zig index 6b43ba67..f428212b 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -1026,7 +1026,9 @@ pub const Namespace = struct { st.mounts.deinit(gpa); for (st.os_paths.items) |entry| gpa.free(entry.path); st.os_paths.deinit(gpa); - for (st.opens[0..st.opens_len]) |*o| o.deinit(gpa); + // A dump refused for its `opens_len` (walk.holdTo) left it saying + // more than it holds when the load's errdefer tears this down. + for (st.opens[0..@min(st.opens_len, st.opens.len)]) |*o| o.deinit(gpa); for (st.pages.items) |*page| page.bytes.deinit(gpa); st.pages.deinit(gpa); for (st.closed_lines.items) |*line| line.bytes.deinit(gpa); @@ -1594,3 +1596,9 @@ test "a write open(2) refuses says its errno in words" { var words: [128]u8 = undefined; try std.testing.expectEqualStrings("no such file or directory", pardes.Messages.errorWords(error.NoSuchFileOrDirectory, &words)); } + +test "a namespace a refused dump left saying more opens than it holds is torn down to its last" { + var st: Namespace = .{}; + st.opens_len = st.opens.len + 34; + st.deinit(std.testing.allocator); +} diff --git a/core/root.zig b/core/root.zig index bf72a32e..4713dc9d 100644 --- a/core/root.zig +++ b/core/root.zig @@ -479,6 +479,7 @@ const allowed_vars = [_]struct { []const u8, []const u8, []const u8 }{ .{ "Kind.zig", "breach", "Debug: the first call over budget, for the addon test tool" }, .{ "Kind.zig", "drawer heap seed_base", "set once by the host at start" }, .{ "Kind.zig", "ready drawn", "this process's draws: those done, each keyed by its core, pane and serial, and the last token given, which no rollback takes back" }, + .{ "Messages.zig", "stub_asked", "a test's question in the stand-in for line C's questions (Command.zig's test), deleted with the stand-in at the merge with line C" }, .{ "Names.zig", "dp_rows dp_trace", "the scorer's scratch, written before it is read in one call" }, .{ "Turn.zig", "yield_depth", "this thread's nesting of yields (threadlocal)" }, .{ "Term.zig", "writes reloads", "this process's emulators' writes, read across one replay on its thread (the replica engine's one rule), and its reloads' count and time" }, -- cgit v1.3