summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Command.zig978
-rw-r--r--core/Messages.zig1
-rw-r--r--core/Pipe.zig2
-rw-r--r--core/Prompt.zig30
-rw-r--r--core/TagState.zig14
-rw-r--r--core/colors.zig4
-rw-r--r--core/normal.zig10
-rw-r--r--core/root.zig2
-rw-r--r--test/behaviour/keys.zig6
-rw-r--r--test/behaviour/panes.zig2
-rw-r--r--test/behaviour/tags.zig48
-rw-r--r--test/panes.zig4
-rw-r--r--test/perf.zig24
-rw-r--r--test/process/tty_editing.zig20
-rw-r--r--test/process/tty_layout.zig2
-rw-r--r--test/process/tty_terminals.zig4
-rw-r--r--test/snapshots/tagnav.golden2
-rw-r--r--test/snapshots/ttytaken.golden2
18 files changed, 1095 insertions, 60 deletions
diff --git a/core/Command.zig b/core/Command.zig
new file mode 100644
index 00000000..55c336f1
--- /dev/null
+++ b/core/Command.zig
@@ -0,0 +1,978 @@
+//! `:` (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 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` Find 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. 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.previewTheme, the themes picker's); `:set ` the settings and their values now, then `:set
+//! Wrap ` its values; `:open ` hands the line to Find (D's source of every
+//! path, Pick.zig), the rest of it the query; `: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.
+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 colors = @import("colors.zig");
+const Messages = @import("Messages.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); a command's argument (themes, by
+/// their index in colors.themes; settings, in config.Settings.settings;
+/// the values of `setting`; the pipe history, newest first); or none, an
+/// argument that is no list (`goto`'s line). `find`'s are Find's own: the
+/// line goes to it (`:open `), so no rows are ever of it here.
+pub const Rows = enum(u8) { names, themes, settings, values, find, 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 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.
+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 = "", 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", .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", .rows = .find, .doc = "Open a file: Find, the rest of the line its query." },
+ .{ .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", .rows = .settings, .doc = "Flip a setting, or step it to its next value: its word bare." },
+};
+
+/// 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.
+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 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;
+ const line = s.field.text();
+ const l = read(line);
+ c.completed = false;
+ c.from = 0;
+ c.answers = 0;
+ c.words = 0;
+ s.rows = .{};
+ s.nhits = 0;
+ s.ntotal = 0;
+ c.rows = .names;
+ var q = l.name;
+ if (l.command) |i| if (l.arg) |arg| {
+ // `open `: Find from this pane, as B2 on `Find arg` runs it (a
+ // picker in the tag where jumps walk), the rest of the line its
+ // query: every path, listed and scored off the loop (SPEC §8, §5.1).
+ if (typed[i].rows == .find) {
+ var buf: [Field.max + 8]u8 = undefined;
+ const word = std.fmt.bufPrint(&buf, "Find {s}", .{arg}) catch return;
+ const id = TagState.hostOf(p) orelse return;
+ TagState.toRest(p);
+ return exec.runBuiltin(p, config.exec_cmd, id, "", word);
+ }
+ 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, .find => {},
+ .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.answers + c.words);
+ c.words += 1;
+ }
+ 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 => {
+ 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);
+ },
+ .pipes => {
+ const r = &p.prompt_history.pipe;
+ for (0..r.n) |i| consider(s, q, r.get(@intCast(i + 1)).?, 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, ", ");
+}
+
+/// 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, no pane
+/// dimmed (colors.previewTheme, as a themes picker's cursor); else its own
+/// theme. At each change of the rows, and after every step
+/// (Prompt.settle), as the picker's is (Pick.produceAll); rest takes it
+/// off (TagState.toRest).
+pub fn preview(p: *Pardes) void {
+ const s = &p.view().tag_state;
+ const worn: ?usize = if (s.prompt.command.rows == .themes) current(s) else null;
+ if (worn == null and !s.previewing.eql(.full)) return;
+ colors.previewTheme(p, worn);
+ s.previewing = if (worn != null) .full else .empty;
+}
+
+/// 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 };
+}
+
+/// 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, .find => return null,
+ .names => {
+ 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 = c.answers;
+ 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;
+ },
+ .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.
+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 = 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 (typedAt(c, at) != null) 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
+/// 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);
+}
+
+/// 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(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 };
+ 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"))
+ theme(p, arg, buf)
+ else if (std.mem.eql(u8, name, "set") or std.mem.eql(u8, name, "toggle"))
+ 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"))
+ .{ .word = std.fmt.bufPrint(buf, "Find {s}", .{arg}) catch return .{ .said = "open: the query is too long" } }
+ 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;
+}
+
+/// `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 worn = if (s.prompt.command.rows == .themes) current(s) else null;
+ const name = if (worn != null and worn.? < colors.themes.len) colors.themes[worn.?].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 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 = 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" } };
+}
+
+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;
+ // 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());
+ // 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);
+ 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);
+ p.prompt_history.pipe.add(r.word);
+ 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(p: *Pardes, w: *std.Io.Writer, line: []const u8) ?[]const u8 {
+ var buf: [Field.max + 16]u8 = undefined;
+ const r = plan(p, 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" }),
+ .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" }),
+ .pipes => try text.print(arena, " {d} pipe{s}", .{ s.nhits, if (s.nhits == 1) "" else "s" }),
+ .none, .find => {
+ 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 if (c.rows == .names) @max(doc_rows, shown) else shown;
+}
+
+/// 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 `<argument>`, 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;
+ 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 = 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 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, 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 (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);
+ }
+ 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);
+ for (marks[0..nmarks]) |*m| m.* += lead;
+ }
+ // 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, 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.
+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.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 "",
+ .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 "";
+ },
+ .find, .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 "";
+}
+
+/// 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;
+}
+
+/// 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 (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 };
+ return rows;
+ }
+ 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;
+}
+
+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, "<theme>"));
+ // 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");
+ const worn = colors.themes[p.theme_preview.?].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(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));
+ // `: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 <line> "));
+ 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);
+}
+
+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 hands the line to Find" {
+ 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(p.theme_preview == 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(p.theme_preview == 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 line goes to Find, the rest of it the query, the keys
+ // after it typed into Find's field.
+ typeKeys(p, ":o");
+ try std.testing.expect(has(try tagOf(p), " : o = Exec Find "));
+ typeKeys(p, " te");
+ try std.testing.expectEqual(TagState.Kind.pick, s.kind);
+ try std.testing.expectEqual(pardes.Pick.Source.find, pardes.Pick.live(p).?.source);
+ try std.testing.expectEqualStrings("te", s.field.text());
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqual(TagState.Kind.rest, s.kind);
+}
+
+test "a view in :theme on its last row keeps it through a dump and a load: its rows index the themes, not the tree's leaves" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 });
+ defer p.deinit();
+ _ = try p.setTestFile("one\n");
+ typeKeys(p, ":theme ");
+ p.update(.{ .key = .{ .cp = Key.tab, .shift = true } });
+ const s = &p.view().tag_state;
+ const last = s.hits[s.rows.at];
+ try std.testing.expectEqual(colors.themes.len - 1, last);
+ try std.testing.expect(last >= @import("bindings.zig").flat.len + p.kinds.nwords);
+ const bytes = try pardes.dump.byteDump(p);
+ defer std.testing.allocator.free(bytes);
+ const q = try pardes.dump.load(std.testing.allocator, p.opts, bytes, null);
+ defer q.deinit();
+ const t = &q.views[p.live].?.tag_state;
+ try std.testing.expectEqual(TagState.Kind.prompt, t.kind);
+ try std.testing.expectEqual(Rows.themes, t.prompt.command.rows);
+ try std.testing.expectEqual(last, t.hits[t.rows.at]);
+}
+
+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 <line> "));
+ 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);
+ // 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`: a file changed on disk under its unsaved
+/// text, as line C's own test asks one.
+fn ask(p: *Pardes, pane: *Pane, tmp: *std.testing.TmpDir, disk: []const u8) !void {
+ 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();
+ 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 e2224337..2b63e647 100644
--- a/core/Messages.zig
+++ b/core/Messages.zig
@@ -989,7 +989,6 @@ pub fn bands(p: *const Pardes, pane: *const Pane, tag_rows: u16, r: Rect) Pane.N
return n;
}
-
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/Pipe.zig b/core/Pipe.zig
index 1dc69d49..1ef2e4ea 100644
--- a/core/Pipe.zig
+++ b/core/Pipe.zig
@@ -480,7 +480,7 @@ test "| in a tag pipes the tag's own text, and undo there brings it back" {
defer p.deinit();
while (p.nextEffect()) |_| {}
const pane = try p.setTestFile("body\n");
- p.update(.{ .key = .{ .cp = ':' } }); // the tag, on Save
+ for ("::") |c| p.update(.{ .key = .{ .cp = c } }); // the tag (`:` on the empty `:` field), on Save
p.update(.{ .key = .{ .cp = 'e' } }); // Save selected
p.update(.{ .key = .{ .cp = '|' } });
try std.testing.expect(pane.focus == .tag and pane.prompt == .pipe and pane.prompt_for == .tag);
diff --git a/core/Prompt.zig b/core/Prompt.zig
index 6b177c19..64e030e0 100644
--- a/core/Prompt.zig
+++ b/core/Prompt.zig
@@ -34,6 +34,7 @@ const TagState = @import("TagState.zig");
const Kind = @import("Kind.zig");
const Places = @import("Places.zig");
const Trail = @import("Trail.zig");
+const Command = @import("Command.zig");
const colors = @import("colors.zig");
const Surface = @import("surface.zig").Surface;
const Cell = @import("surface.zig").Cell;
@@ -73,6 +74,8 @@ chain: u32 = 0,
/// refused once over runs that failed.
forced: bool = false,
refused: bool = false,
+/// `:`'s (Command.zig): what its rows are of, and where Tab writes.
+command: Command = .{},
/// The session's prompt histories: the lines Enter took, newest last.
pub const History = struct {
@@ -132,7 +135,7 @@ const info = std.EnumArray(Op, Info).init(.{
.insert = .{ .cell = "!", .hint = "⏎ insert · C-r history · esc" },
.append = .{ .cell = "A-!", .hint = "⏎ append · C-r history · esc" },
.keep_pipe = .{ .cell = "$", .hint = "⏎ keep · C-r history · esc" },
- .command = .{ .cell = ":", .hint = "⏎ run · C-r history · esc" },
+ .command = .{ .cell = ":", .hint = "Tab complete · ⏎ run · C-r history · esc" },
});
/// The history `op` reads and Enter adds to.
@@ -254,6 +257,7 @@ pub fn key(p: *Pardes, k: Key) void {
const plain = !k.ctrl and !k.alt;
const ctrl = k.ctrl and !k.alt;
if (hit(k, config.escape)) return cancel(p, pane);
+ if (s.prompt.op == .command and Command.key(p, id, pane, k)) return;
if (plain and k.cp == Key.enter) return commit(p, id, pane);
if (k.alt and !k.ctrl and k.cp == Key.enter) return asList(p, id, pane);
// Backspace on an empty field: the prompt goes, as Esc.
@@ -289,6 +293,7 @@ pub fn key(p: *Pardes, k: Key) void {
fn live(p: *Pardes, pane: *Pane) void {
const s = &p.view().tag_state;
const pat = s.field.text();
+ if (s.prompt.op == .command) return Command.filter(p, pane);
if (behavior(s.prompt.op)) |how| {
// A pipe: run live per selection when it is a known filter (or
// Ctrl-p said so), off the loop; Enter is asked again.
@@ -719,10 +724,12 @@ fn refresh(pane: *Pane, wrapped: ?bool) void {
/// After each step (Core.zig, beside the places'): every search word says
/// where its places stand; the live view's search prompt finds `?`'s place
-/// and counts the lines its places are on once they are all found.
+/// and counts the lines its places are on once they are all found; its
+/// `:theme ` previews its current row (Command.preview).
pub fn settle(p: *Pardes) void {
for (p.panes) |slot| if (slot) |pane| refresh(pane, null);
const s = &p.view().tag_state;
+ if (s.kind == .prompt and s.prompt.op == .command) Command.preview(p);
if (s.kind != .prompt or !searches(s.prompt.op)) return;
const pane = p.panes[TagState.hostOf(p) orelse return].?;
if (s.prompt.seeking) seekBack(p, pane);
@@ -879,7 +886,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 => try w.writeAll(line),
+ .command => return null, // Command.plainWord, which reads the rows
}
return if (op == .search or op == .search_back) "Look" else "Exec";
}
@@ -1065,7 +1072,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()});
}
@@ -1077,6 +1085,7 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head {
try pipeCount(p, arena, &text, &spans, pane, pr, line);
} else try count(arena, &text, pane, pr);
}
+ if (pr.op == .command) try Command.count(p, arena, &text);
try text.appendSlice(arena, " ");
try styled(arena, &text, &spans, info.get(pr.op).hint, .dim);
return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret };
@@ -1162,6 +1171,7 @@ fn plural(n: usize) []const u8 {
/// lines the field is in, eleven at most.
pub fn takes(p: *Pardes) u16 {
const s = &p.view().tag_state;
+ if (s.prompt.op == .command) return @max(Command.takes(p), if (s.prompt.history) historyRows(p) else 0);
if (s.prompt.op == .pipe_to) if (p.panes[TagState.hostOf(p) orelse return 0]) |pane| if (answered(p, pane, s.field.text())) |l|
return @max(runRows(l), if (s.prompt.history) historyRows(p) else 0);
if (!s.prompt.history) return 0;
@@ -1190,6 +1200,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;
+ // `:`'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| {
@@ -1209,6 +1225,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 {
@@ -1220,7 +1240,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/TagState.zig b/core/TagState.zig
index ef5c1d53..3bd79176 100644
--- a/core/TagState.zig
+++ b/core/TagState.zig
@@ -139,8 +139,9 @@ pub fn describeKey(p: *Pardes, key: pardes.Key) void {
if (!c.group) s.found = true;
}
-/// 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);
/// A root struck, its time not had yet (as Pane.question_asked: the step
/// that strikes it reads no clock).
@@ -232,6 +233,9 @@ pub fn toRest(p: *Pardes) void {
const s = &v.tag_state;
if (s.kind == .prompt) Prompt.leave(p);
s.kind = .rest;
+ // The whole window previewed `:theme `'s row (Command.preview): its
+ // own theme again (a themes picker's went as it rolled up).
+ if (s.previewing.eql(.full)) @import("colors.zig").previewTheme(p, null);
s.previewing = .empty;
s.root = null;
s.due_ns = 0;
@@ -273,7 +277,10 @@ pub fn loaded(p: *Pardes) void {
for (&p.views) |*slot| if (slot.*) |*v| {
const s = &v.tag_state;
var held = s.field.caret <= s.field.len and s.nhits <= max_hits and s.rows.at < @max(1, s.nhits) and !(s.found and s.scope_n == 0) and !(s.missed and s.scope_root == null and s.scope_n == 0);
- for (s.hits[0..@min(s.nhits, max_hits)]) |at| held = held and at < bindings.flat.len + p.kinds.nwords;
+ // `:`'s hits index its own rows (the themes, the settings: Command.zig,
+ // which holds each where it reads it); the rest the tree's leaves.
+ const command = s.kind == .prompt and s.prompt.op == .command;
+ for (s.hits[0..@min(s.nhits, max_hits)]) |at| held = held and (command or at < bindings.flat.len + p.kinds.nwords);
if (!held) s.* = .{ .used = s.used };
};
}
@@ -330,6 +337,7 @@ pub fn point(p: *Pardes, index: u16) void {
s.rows.at = index;
return;
}
+ if (s.kind == .prompt and s.prompt.op == .command) return @import("Command.zig").point(p, index);
const cur = current(p, v) orelse return;
var children: [bindings.max_children]bindings.Child = undefined;
const level = bindings.level(p, cur.root, cur.path, &children);
diff --git a/core/colors.zig b/core/colors.zig
index 2a97ac69..ef2d5aae 100644
--- a/core/colors.zig
+++ b/core/colors.zig
@@ -764,7 +764,9 @@ 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 the window previews
+ // (`previewTheme`).
+ 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
diff --git a/core/normal.zig b/core/normal.zig
index e1af647d..2579fab6 100644
--- a/core/normal.zig
+++ b/core/normal.zig
@@ -23,6 +23,7 @@ const Pardes = pardes.Pardes;
const Macro = pardes.Macro;
const Editor = @import("Editor.zig");
const Prompt = @import("Prompt.zig");
+const Command = @import("Command.zig");
fn isPrefix(key: Key, cp: u21) bool {
return key.cp == cp and !key.ctrl and !key.alt;
@@ -1379,12 +1380,15 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
v.prefix = .leader;
v.leader_n = 0;
},
- // `:` is the one key a tag and a body do not share: it moves the
- // keyboard between them.
+ // `:` is the one key a tag and a body do not share: from a body it
+ // opens the command line in its tag (Command.zig), whose `:` on
+ // the empty field gives the keyboard to the tag's text; from there
+ // it comes back.
.command_line => switch (t.what) {
.tag => pane.focus = .body,
.column, .workspace => tagline.exitHeader(p),
- .body, .input => tagline.enterTag(p, pane),
+ .body => Command.open(p, pane),
+ .input => tagline.enterTag(p, pane),
},
inline .pipe_selection, .pipe_selection_to, .insert_output, .append_output, .keep_pipe => |role| {
const how: modal.Normal.PipeBehavior = switch (role) {
diff --git a/core/root.zig b/core/root.zig
index c086e987..d1a25d21 100644
--- a/core/root.zig
+++ b/core/root.zig
@@ -94,6 +94,7 @@ pub const TagState = @import("TagState.zig");
pub const Band = @import("Band.zig");
pub const Pick = @import("Pick.zig");
pub const Prompt = @import("Prompt.zig");
+pub const Command = @import("Command.zig");
pub const Addon = @import("Addon.zig");
pub const dump = @import("dump.zig");
pub const lsp = @import("lsp/lsp.zig");
@@ -423,6 +424,7 @@ test {
_ = @import("Pick.zig");
_ = @import("Field.zig");
_ = @import("Prompt.zig");
+ _ = @import("Command.zig");
_ = @import("Effect.zig");
_ = @import("tape.zig");
_ = @import("replica.zig");
diff --git a/test/behaviour/keys.zig b/test/behaviour/keys.zig
index e02b1fa5..6a62ccd7 100644
--- a/test/behaviour/keys.zig
+++ b/test/behaviour/keys.zig
@@ -40,7 +40,7 @@ test "keys: a prompt's entry and cancellation leave the tag, the body and its cu
// Save on a scratch asks for a path, starting from its directory.
const scratch = try s.newPane();
const scratch_tag = try s.read(s.at("/pane/{d}/tag", .{scratch}));
- try s.keys(":\tzq");
+ try s.keys("::\tzq");
_ = try s.where("/zq");
try s.expect(s.at("/pane/{d}/tag", .{scratch}), scratch_tag);
try s.keys("\x1b");
@@ -272,7 +272,7 @@ test "keys: Save's prompt on a scratch starts from the scratch's directory, one
// New from the file's pane: a scratch in its directory.
try s.write(s.at("/pane/{d}/exec", .{n}), "New\n");
try std.testing.expect(try s.focused() != n);
- try s.keys(":\tzq");
+ try s.keys("::\tzq");
_ = try s.where(c[1]);
try s.keys("\x1b");
}
@@ -380,7 +380,7 @@ test "keys: a PDF in normal mode keeps : (its tag and back), SPC (a leader, canc
try std.testing.expect(try s.cursor() == null);
_ = try s.pdfPage(n);
const tag = (try s.where("[1/"))[1];
- try s.keys(":");
+ try s.keys("::");
// On its tag, which runs to three rows (its path, the core's art words).
const on_tag = (try s.cursor()).?[1];
try std.testing.expect(on_tag >= tag and on_tag <= tag + 2);
diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig
index 96bae187..4c76df25 100644
--- a/test/behaviour/panes.zig
+++ b/test/behaviour/panes.zig
@@ -252,7 +252,7 @@ test "panes: Collapse keeps the text and folds the pane to its tag, and again re
try s.expect(s.at("/pane/{d}/body", .{n}), "keep this text\n");
try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
// Its tag still takes the keyboard, the cursor on the tag's row.
- try s.keys(":");
+ try s.keys("::");
try std.testing.expectEqual((try s.where("/test.txt"))[1], (try s.cursor()).?[1]);
try s.write(ctl, "Collapse\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") == null);
diff --git a/test/behaviour/tags.zig b/test/behaviour/tags.zig
index e4e4c04b..c8de115b 100644
--- a/test/behaviour/tags.zig
+++ b/test/behaviour/tags.zig
@@ -27,7 +27,7 @@ test "tags: s and S in a tag answer for the tag's own text; / still searches the
const n = try s.focused();
try s.replace(s.at("/pane/{d}/tag", .{n}), " Save Del Snarf");
// The tag's line, its S's selected, then changed: both are the tag's.
- try s.keys(":xsS\ncZ\x1b");
+ try s.keys("::xsS\ncZ\x1b");
try std.testing.expectEqualStrings("/test.txt Zave Del Znarf", try tag(&s, n));
try s.expect(s.at("/pane/{d}/body", .{n}), "Some body\n");
// `/` from the tag is the body's search, as acme's Look from a tag is.
@@ -42,7 +42,7 @@ test "tags: ms and md edit a tag's own text, and the text the edit made is read
// A held pane (Keep): its tag as it was before the walk, no `Keep` word (SPEC §6.5).
try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Keep\n");
const n = try s.focused();
- try s.keys(":miwms(");
+ try s.keys("::miwms(");
try std.testing.expectEqualStrings("/test.txt (Save) Term Collapse Follow Del", try tag(&s, n));
try s.keys("md(");
try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del", try tag(&s, n));
@@ -56,7 +56,7 @@ test "tags: the keyboard reaches a tag's path, which it can select and yank but
const n = try s.focused();
const before = try tag(&s, n);
// `0` is the line's start, as in acme, where the name is ordinary text.
- try s.keys(":0");
+ try s.keys("::0");
try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt"));
// An edit that reaches into it is refused, the cursor with it.
try s.keys("Eyd");
@@ -69,7 +69,7 @@ test "tags: the keyboard reaches a tag's path, which it can select and yank but
try s.keys("\x1b\x1b");
try s.expect(s.at("/pane/{d}/name", .{n}), "/test.txt\n");
// After the path, the text is the tag's own to edit.
- try s.keys(":A!\x1b");
+ try s.keys("::A!\x1b");
try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del!", try tag(&s, n));
// What it yanked was the path.
try s.keys(":P");
@@ -89,7 +89,7 @@ test "tags: a tag remembers its cursor, starts on Save, and a click moves it" {
const other = s.at("{d}", .{second});
try s.write("/focus", focus);
// Nothing to remember yet: `:` lands on Save.
- try s.keys(":");
+ try s.keys("::");
try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt"));
// Home is the line's start, the path's: the keyboard reaches it.
try s.keys(home ++ right ++ right ++ right);
@@ -98,13 +98,13 @@ test "tags: a tag remembers its cursor, starts on Save, and a click moves it" {
try s.keys(":");
try std.testing.expect((try s.cursor()).?[1] != (try s.where("/test.txt"))[1]);
try s.write("/focus", other);
- try s.keys(":" ++ home ++ right ++ right ++ right ++ right ++ right ++ ":");
+ try s.keys("::" ++ home ++ right ++ right ++ right ++ right ++ right ++ ":");
try s.write("/focus", focus);
- try s.keys(":");
+ try s.keys("::");
try std.testing.expectEqual(@as(usize, 3), try tagCol(&s, "/test.txt"));
try s.keys(":");
try s.write("/focus", other);
- try s.keys(":");
+ try s.keys("::");
try std.testing.expectEqual(@as(usize, 5), try tagCol(&s, "/second.txt"));
try s.keys(":");
// A click in the tag puts its cursor there, the pane's keyboard with it.
@@ -113,7 +113,7 @@ test "tags: a tag remembers its cursor, starts on Save, and a click moves it" {
try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt"));
// A prompt opened and left from the body leaves it there (a clicked
// tag is in normal mode: `:` goes back to the body).
- try s.keys(":/\x1b:");
+ try s.keys(":/\x1b::");
try std.testing.expectEqual(@as(usize, "/test.txt Save T".len), try tagCol(&s, "/test.txt"));
try s.keys(":");
try s.expect(s.at("/pane/{d}/body", .{first}), "body\n");
@@ -127,13 +127,13 @@ test "tags: executing a word from the tag runs it and gives the keyboard back to
try s.keys("ix\x1b");
// `:` lands on Save; the execute key runs it, the look key looks
// it up. Either way the tag lets go of the keyboard first.
- try s.keys(":");
+ try s.keys("::");
try s.keys(chord);
try std.testing.expectEqual(@as(usize, if (chord[0] == '\t') 1 else 0), s.saved);
try s.keys("iQ\x1b");
try s.expect(s.at("/pane/{d}/body", .{n}), "xQbody\n");
// ...and the tag still knows where it was.
- try s.keys(":");
+ try s.keys("::");
try std.testing.expectEqual(@as(usize, "/test.txt ".len), try tagCol(&s, "/test.txt"));
}
}
@@ -144,13 +144,13 @@ test "tags: a renamed path moves the tag's cursor with the text after it and a s
// A held pane (Keep): its tag as it was before the walk, no `Keep` word (SPEC §6.5).
try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Keep\n");
const n = try s.focused();
- try s.keys("::"); // on Save, and back
+ try s.keys(":::"); // `::` on Save, and `:` back
try s.write(s.at("/pane/{d}/name", .{n}), "/界.zig");
- try s.keys(":");
+ try s.keys("::");
try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界"));
try s.keys(":");
try s.replace(s.at("/pane/{d}/tag", .{n}), "é");
- try s.keys(":");
+ try s.keys("::");
// `/界.zig é`: the cursor is pulled back onto the last character.
try std.testing.expectEqual(@as(usize, "/界.zig ".len - 1), try tagCol(&s, "/界"));
try s.expect(s.at("/pane/{d}/body", .{n}), "body\n");
@@ -163,13 +163,13 @@ test "tags: window keys move between panes from a tag and each tag keeps its cur
const second = try s.newPane();
try s.write(s.at("/pane/{d}/name", .{second}), "/second.txt");
try s.write("/focus", s.at("{d}", .{first}));
- try s.keys(":" ++ home ++ "ll" ++ window ++ "j\n");
+ try s.keys("::" ++ home ++ "ll" ++ window ++ "j\n");
try std.testing.expectEqual(second, try s.focused());
- try s.keys(":" ++ home ++ "llll" ++ window ++ "k\n");
+ try s.keys("::" ++ home ++ "llll" ++ window ++ "k\n");
try std.testing.expectEqual(first, try s.focused());
- try s.keys(":");
+ try s.keys("::");
try std.testing.expectEqual(@as(usize, 2), try tagCol(&s, "/test.txt"));
- try s.keys(window ++ "j\n:");
+ try s.keys(window ++ "j\n::");
try std.testing.expectEqual(@as(usize, 4), try tagCol(&s, "/second.txt"));
}
@@ -182,7 +182,7 @@ test "tags: a tag's normal mode is the body's: motions stay in the tag and inser
try s.write("/ctl", "Newcol\n");
const right_pane = try s.newPane();
try s.write("/focus", s.at("{d}", .{first}));
- try s.keys(":" ++ home ++ "l");
+ try s.keys("::" ++ home ++ "l");
try std.testing.expectEqual(@as(usize, 1), try tagCol(&s, "/test.txt"));
try s.keys("hkj");
try std.testing.expectEqual(@as(usize, 0), try tagCol(&s, "/test.txt"));
@@ -191,7 +191,7 @@ test "tags: a tag's normal mode is the body's: motions stay in the tag and inser
try std.testing.expectEqual(right_pane, try s.focused());
try s.keys(window ++ "h\n");
try std.testing.expectEqual(first, try s.focused());
- try s.keys(":i\u{F018}hjklHJKL:");
+ try s.keys("::i\u{F018}hjklHJKL:");
try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow DelhjklHJKL:", try tag(&s, first));
try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " mode:normal "));
try s.expect(s.at("/pane/{d}/body", .{first}), "body\n");
@@ -205,7 +205,7 @@ test "tags: a tag with a newline in it is as tall as its lines, and each line is
try s.write(s.at("/pane/{d}/exec", .{try s.focused()}), "Keep\n");
const n = try s.focused();
// Insert mode in a tag is a body's: Enter is a new line.
- try s.keys(":A\nSecond");
+ try s.keys("::A\nSecond");
try std.testing.expectEqualStrings("/test.txt Save Term Collapse Follow Del\nSecond", try tag(&s, n));
const x, const y = try s.where("/test.txt");
try std.testing.expectEqual(.{ x, y + 1 }, try s.where("Second"));
@@ -234,7 +234,7 @@ test "tags: a tag with more lines than rows scrolls to keep its caret in view" {
try s.write(s.at("/pane/{d}/tag", .{n}), many.items);
const x, const y = try s.where("/test.txt");
// To the last line: the tag scrolls so it is the bottom row, caret on it.
- try s.keys(":ge");
+ try s.keys("::ge");
const lx, const ly = try s.where("L20");
try std.testing.expectEqual(.{ lx, ly }, (try s.cursor()).?);
// What the pointer sees is what is shown: its first row is line 13.
@@ -264,7 +264,7 @@ test "tags: a long pane tag wraps onto rows, and a click on a wrapped row maps t
try s.write(s.at("/pane/{d}/name", .{n}), "/an/especially/deep/project/path/with/a/long/component/filename.zig");
try s.write(s.at("/pane/{d}/tag", .{n}), " JumpTrail");
const before = try tag(&s, n);
- try s.keys(":A");
+ try s.keys("::A");
try std.testing.expect((try s.cursor()).?[0] < 50);
// JumpTrail is on a row of its own, below the path's.
try std.testing.expect((try s.where("JumpTrail"))[1] > (try s.where("/an/"))[1]);
@@ -461,7 +461,7 @@ test "tags: truncating a tag clears its own text, u in the tag undoes it, and >
s.clunk(h);
try std.testing.expectEqualStrings("/test.txt", std.mem.trimEnd(u8, try tag(&s, n), " "));
// The tag keeps its undo: undoing in it brings back what was cleared.
- try s.keys(":u:");
+ try s.keys("::u:");
try std.testing.expect(std.mem.endsWith(u8, try tag(&s, n), " Mine"));
// Written back without a leading space, the text still stands apart
// from the path, and a click on its first letter lands on it.
diff --git a/test/panes.zig b/test/panes.zig
index 127043a2..3ce35d14 100644
--- a/test/panes.zig
+++ b/test/panes.zig
@@ -911,7 +911,7 @@ test "paste into pane commands and prompts never inserts into the body" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true });
defer p.deinit();
const pane = try p.setTestFile("one two one\n");
- p.update(.{ .key = .{ .cp = ':' } });
+ for ("::") |c| p.update(.{ .key = .{ .cp = c } }); // the tag (`:` on the empty `:` field)
p.update(.{ .key = .{ .cp = 'A' } });
// A tag takes a paste as a body does, newline and all.
p.update(.{ .paste = " Custom\nCommand" });
@@ -940,7 +940,7 @@ test "a tag that exactly fills its row stays one row, its caret at the edge as a
path[0] = '/';
pane.file.?.path = path;
try std.testing.expectEqual(width, (try pardes.tagline.tagText(p, p.scratch.allocator(), pane)).len);
- p.update(.{ .key = .{ .cp = ':' } });
+ for ("::") |c| p.update(.{ .key = .{ .cp = c } }); // the tag (`:` on the empty `:` field)
p.update(.{ .key = .{ .cp = 'A' } });
var arena = std.heap.ArenaAllocator.init(p.gpa);
defer arena.deinit();
diff --git a/test/perf.zig b/test/perf.zig
index 8e622680..d8025fdc 100644
--- a/test/perf.zig
+++ b/test/perf.zig
@@ -1956,6 +1956,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 });
+ }
}
/// `--pick`: a picker's hot paths (core/Pick.zig, walk UI line D), each
diff --git a/test/process/tty_editing.zig b/test/process/tty_editing.zig
index cb903bd6..9285ce7d 100644
--- a/test/process/tty_editing.zig
+++ b/test/process/tty_editing.zig
@@ -109,7 +109,7 @@ fn fileedit(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.snap("undone");
// Save through the file pane's own tag: `:` focuses that same tag and `w`
// selects Save before the ordinary execute key runs it.
- try t.key(":");
+ try t.key(": :");
try t.stable(400, 5000);
try t.key("w tab");
try t.stable(600, 8000);
@@ -1393,7 +1393,7 @@ fn cmdword(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
// opens ITS tag; `A` appends at the end of it, Esc and `h` put the
// normal-mode cursor on the last character typed, and the execute key runs
// the word under it exactly as the body's does.
- try t.key(":");
+ try t.key(": :");
try t.key("A");
try t.key("space");
try t.send("Exec");
@@ -1409,7 +1409,7 @@ fn cmdword(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.snap("execword");
// a command word run from that tag runs as a command pane in the directory
// the pane lists, as a word run from any text pane's tag does
- try t.key(":");
+ try t.key(": :");
try t.key("A");
try t.key("space");
try t.send("@`echo ZZ''T`");
@@ -2407,7 +2407,7 @@ fn tagcmd(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.stable(400, 5000);
try t.snap("edited");
// `:` -> the tag, in normal mode, cursor on Save
- try t.key(":");
+ try t.key(": :");
try t.stable(400, 5000);
try t.snap("colon");
// w -> the "Save" span (selected, like the body's word motion)
@@ -2421,7 +2421,7 @@ fn tagcmd(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.snap("saved");
try t.snapFile("written", "/tmp/pardes-snap/tagcmd/cwd/c.txt");
// `:` then `:`: back to the body with the tag untouched, nothing run
- try t.key(":");
+ try t.key(": :");
try t.stable(400, 5000);
try t.key(":");
try t.stable(400, 5000);
@@ -2468,7 +2468,7 @@ fn tagnav(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.stable(400, 5000);
try t.snap("start");
// `:` -> the file's OWN tagline, normal mode, on Save
- try t.key(":");
+ try t.key(": :");
try t.stable(400, 5000);
try t.snap("colon");
// w selects in the tag the way it selects in a body
@@ -2485,13 +2485,13 @@ fn tagnav(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.stable(400, 5000);
try t.snap("right");
// `:` there -> that pane's own tag, on its Save the first time
- try t.key(":");
+ try t.key(": :");
try t.stable(400, 5000);
try t.snap("right-tag");
// back left, and `:` finds the file's tag cursor where l left it
try t.key("c-w h enter");
try t.stable(400, 5000);
- try t.key(":");
+ try t.key(": :");
try t.stable(400, 5000);
try t.snap("left-tag");
// `:` in the tag's normal mode: back to the body
@@ -2572,7 +2572,7 @@ fn tagpath(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.wait(8000, "c.txt");
try t.stable(700, 20000);
try t.snap("start");
- try t.key(":");
+ try t.key(": :");
try t.stable(400, 5000);
try t.snap("colon");
try t.key("0");
@@ -2611,7 +2611,7 @@ fn tagpath(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.key("esc");
try t.stable(400, 5000);
try t.snap("name-cancelled");
- try t.key(":");
+ try t.key(": :");
try t.key("0");
try t.key("i");
try t.send("/renamed");
diff --git a/test/process/tty_layout.zig b/test/process/tty_layout.zig
index e0396a53..00197a5c 100644
--- a/test/process/tty_layout.zig
+++ b/test/process/tty_layout.zig
@@ -439,7 +439,7 @@ fn tagalign(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.settle(100);
try t.key("esc");
try t.settle(100);
- try t.key(":");
+ try t.key(": :");
try t.key("A");
try t.stable(400, 5000);
try t.snap("revealed");
diff --git a/test/process/tty_terminals.zig b/test/process/tty_terminals.zig
index 838b2aea..53050a18 100644
--- a/test/process/tty_terminals.zig
+++ b/test/process/tty_terminals.zig
@@ -427,7 +427,7 @@ fn ttytaken(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
// at the prompt: a command word in the terminal's own tag, executed, is
// typed into its shell. `:` opens the tag, `A` appends, and the execute key
// runs the word under the cursor.
- try t.key(":");
+ try t.key(": :");
try t.key("A");
try t.key("space");
try t.send("@`echo TAKEN''ONE`");
@@ -451,7 +451,7 @@ fn ttytaken(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try t.stable(400, 5000);
try t.snap("paging");
// the same gesture from that terminal: a command pane, not `less` keystrokes
- try t.key(":");
+ try t.key(": :");
try t.key("A");
try t.key("space");
try t.send("@`echo TAKEN''TWO`");
diff --git a/test/snapshots/tagnav.golden b/test/snapshots/tagnav.golden
index 1039c40a..9bf061bc 100644
--- a/test/snapshots/tagnav.golden
+++ b/test/snapshots/tagnav.golden
@@ -42,14 +42,12 @@
== snap tty-on grid=100x31 cursor=54,6
|2: /tmp/pardes-snap/tagnav/cwd/w.txt Save Term Coll$ /tmp/pardes-snap/tagnav/cwd Term+bash Save Mode
|4: 1 alpha $ ls
-|6: 3 gamma $
|30: ✓ /tmp/pardes-snap/tagnav/cwd/w.txt Window 5 messages · SPC h m
== snap tty-tag grid=100x31 cursor=95,2
== snap tty-restored grid=100x31 cursor=54,6
== snap column-tag grid=100x31 cursor=2,1
|2: /tmp/pardes-snap/tagnav/cwd/w.txt Save Term Coll /tmp/pardes-snap/tagnav/cwd Term+bash Save Mode
|4: 1 alpha ls
-|6: 3 gamma
|30: ✓ /tmp/pardes-snap/tagnav/cwd/w.txt Up 8 messages · SPC h m
== snap column-word grid=100x31 cursor=5,1
== snap workspace-tag grid=100x31 cursor=2,0
diff --git a/test/snapshots/ttytaken.golden b/test/snapshots/ttytaken.golden
index 337072f1..a2011891 100644
--- a/test/snapshots/ttytaken.golden
+++ b/test/snapshots/ttytaken.golden
@@ -32,7 +32,7 @@
| /tmp/pardes-snap/ttytaken/cwd
== snap at-prompt grid=210x31 cursor=4,5
|2: /tmp/pardes-snap/ttytaken/cwd Term+bash Save Mode Filter Collapse Del @`echo TAKEN''ONE`
-|5: echo TAKEN''ONE
+|5: echo TAKEN''ONE
|6: TAKENONE
|30: ✓ @p1 Exec 1 message · SPC h m
== snap paging grid=210x31 cursor=9,29