diff options
Diffstat (limited to 'core/Command.zig')
| -rw-r--r-- | core/Command.zig | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/core/Command.zig b/core/Command.zig index 6814a0e3..55c336f1 100644 --- a/core/Command.zig +++ b/core/Command.zig @@ -5,15 +5,16 @@ //! 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`, +//! 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.wear); `:set ` the settings and their values now, then `:set -//! Wrap ` its values; `:open ` the files the panes show; `:pipe ` the +//! (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, @@ -46,10 +47,10 @@ 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 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 }; +/// 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 @@ -79,7 +80,7 @@ const typed = [_]Typed{ .{ .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 = .files, .doc = "Open a file: Look at its name." }, + .{ .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." }, @@ -154,6 +155,16 @@ pub fn filter(p: *Pardes, pane: *Pane) void { 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; @@ -169,7 +180,7 @@ pub fn filter(p: *Pardes, pane: *Pane) void { }; }; switch (c.rows) { - .none => {}, + .none, .find => {}, .names => { // A question waiting on the pane: its answers first (SPEC // §10.4), a row each, which a letter typed only filters. @@ -196,13 +207,6 @@ 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 (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; for (0..r.n) |i| consider(s, q, r.get(@intCast(i + 1)).?, i); @@ -223,28 +227,6 @@ fn valuesOf(k: u8) std.mem.SplitIterator(u8, .sequence) { return std.mem.splitSequence(u8, words, ", "); } -/// 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 { @@ -252,14 +234,17 @@ fn current(s: *const TagState) ?usize { 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 { +/// 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 c = &s.prompt.command; - const worn: ?usize = if (c.rows == .themes) current(s) else null; - if (worn == null and s.worn == null) return; - colors.wear(p, worn); + 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 @@ -305,7 +290,7 @@ fn tagWords(pane: *Pane) Words { /// 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, + .none, .find => return null, .names => { if (answerAt(p, pane, c, at)) |ans| return ans.word; if (typedAt(c, at)) |t| return typed[t].name; @@ -322,7 +307,6 @@ 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 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))), } } @@ -422,7 +406,7 @@ fn plan(p: *Pardes, line_in: []const u8, buf: []u8) Run { 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 }) + .{ .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")) @@ -437,7 +421,8 @@ fn plan(p: *Pardes, line_in: []const u8, buf: []u8) Run { /// `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; + 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" } }; } @@ -518,9 +503,8 @@ 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} 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 => { + .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})}); @@ -617,7 +601,7 @@ fn saidOf(p: *Pardes, arena: std.mem.Allocator, c: *const Command, at: usize, na const value = valueNow(p, c.setting, &buf); return if (std.mem.eql(u8, value, name)) "now" else ""; }, - .files, .pipes, .none => return "", + .find, .pipes, .none => return "", } } @@ -723,7 +707,7 @@ 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"); - const worn = colors.themes[s.worn.?].name; + 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); @@ -774,7 +758,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 files the panes show" { +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); @@ -816,13 +800,13 @@ test ":theme lists the themes and the whole window wears the current one, none d 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)); + 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(s.worn == null); + 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); @@ -856,12 +840,36 @@ 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 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), "▶ /test.txt")); - try std.testing.expect(has(try tagOf(p), " = Look te 1 file open ")); + // :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" { @@ -909,14 +917,9 @@ test ": has a history of its own: Up and Down bring back the lines Enter ran, ne 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. +/// 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 { - 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)]}); @@ -932,9 +935,6 @@ fn ask(p: *Pardes, pane: *Pane, tmp: *std.testing.TmpDir, disk: []const u8) !voi 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); |
