diff options
Diffstat (limited to 'core/Prompt.zig')
| -rw-r--r-- | core/Prompt.zig | 30 |
1 files changed, 25 insertions, 5 deletions
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 }; |
