diff options
Diffstat (limited to 'core/Band.zig')
| -rw-r--r-- | core/Band.zig | 87 |
1 files changed, 83 insertions, 4 deletions
diff --git a/core/Band.zig b/core/Band.zig index 0e67bcf4..079a1bcc 100644 --- a/core/Band.zig +++ b/core/Band.zig @@ -26,6 +26,7 @@ const modal = @import("modal.zig"); const panes = @import("panes.zig"); const builtins = @import("builtins.zig"); const TagState = @import("TagState.zig"); +const Names = @import("Names.zig"); const Pardes = pardes.Pardes; const Pane = pardes.Pane; const File = panes.File; @@ -83,9 +84,13 @@ columns: u16 = 0, pub fn takes(p: *Pardes, id: usize) u16 { const v = p.view(); const s = &v.tag_state; - if (id != v.active) return 0; - if (s.kind != .rest) return 0; - if (!s.unrolled or TagState.hostOf(p) != id) return 0; + if (TagState.hostOf(p) != id) return 0; + switch (s.kind) { + .rest => {}, + .help => return @max(doc_rows, @max(1, @min(Cursor.shown, s.nhits))), + .describe => return 0, + } + if (!s.unrolled) return 0; const cur = TagState.current(p, v) orelse return 0; var children: [bindings.max_children]bindings.Child = undefined; const n: u16 = @intCast(bindings.level(p, cur.root, cur.path, &children).len); @@ -103,8 +108,17 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band { if (rows == 0) return null; const v = p.view(); const s = &v.tag_state; - const cur = TagState.current(p, v).?; var band: Band = .{ .height = rows, .columns = layout.tagCapacity(p, p.view().rects[id].w -| pardes.TAG_TEXT_INSET) }; + switch (s.kind) { + .rest => {}, + .help => { + try band.helpSteps(p, arena, s); + try band.place(p, arena, pane, id); + return band; + }, + .describe => return null, + } + const cur = TagState.current(p, v).?; // The level, its cursor on the child it names, else the leaf its group // ran last, else the first; and that child's doc beside it. const node = bindings.nodeAt(cur.root, cur.path); @@ -121,6 +135,71 @@ pub fn of(p: *Pardes, arena: std.mem.Allocator, pane: *Pane, id: usize) !?Band { return band; } +/// Help's rows (`chord · word · group`, the query's bytes in the match +/// tint) around the cursor, and the cursor's leaf's doc beside them. +fn helpSteps(band: *Band, p: *Pardes, arena: std.mem.Allocator, s: *const TagState) !void { + if (s.nhits == 0) { + const none = try arena.alloc(Row, 1); + none[0] = .{ .text = if (s.field.len == 0) "no keys here" else "no key matches", .dim = true }; + band.add(.{ .rows = none }); + return; + } + const w = s.rows.window(s.nhits); + const rows = try arena.alloc(Row, w.hi - w.lo); + const q = s.field.text(); + for (rows, w.lo..) |*row, i| { + const buf = try arena.create([TagState.row_max]u8); + const leaf = TagState.helpLeaf(p, s.hits[i], buf); + var marks: [Names.max_query * 4]u16 = undefined; + var nmarks: usize = 0; + if (std.mem.trim(u8, q, " ").len > 0) { + const low = try std.ascii.allocLowerString(arena, leaf.row); + _ = Names.match(q, leaf.row, low, 0, &marks, &nmarks); + } + row.* = .{ .text = leaf.row, .on = i == s.rows.at, .marks = try arena.dupe(u16, marks[0..nmarks]), .index = @intCast(i), .dim = bindings.dimmed(p.panes[p.view().active].?, leaf.child) }; + } + band.add(.{ .rows = rows }); + var buf: [TagState.row_max]u8 = undefined; + const at = TagState.helpLeaf(p, s.hits[s.rows.at], &buf); + band.add(try docStep(p, arena, at.root, at.path[0 .. at.path.len - 1], at.child)); +} + +/// A list's cursor and the rows of it a band shows: eleven at most, around +/// the cursor (SPEC §3.6, the prototype's `H.window`). Each view's (a +/// tag state's field): Help's rows, a picker's, a prompt's history. +pub const Cursor = struct { + at: u16 = 0, + + pub const shown = 11; + + /// Rows [lo, hi) of `n` the band shows. + pub fn window(c: Cursor, n: u16) struct { lo: u16, hi: u16 } { + const lo = @min(c.at -| shown / 2, n -| shown); + return .{ .lo = lo, .hi = @min(n, lo + shown) }; + } + + /// A key that steps a list of `n`: Down, C-j, C-n a row on, Up, C-k, + /// C-p a row back, PageDown and PageUp ten; true when taken. + pub fn key(c: *Cursor, k: pardes.Key, n: u16) bool { + const Key = pardes.Key; + const plain = !k.ctrl and !k.alt; + const ctrl = k.ctrl and !k.alt; + const step: i32 = if (plain and k.cp == Key.down or ctrl and (k.cp == 'j' or k.cp == 'n')) + 1 + else if (plain and k.cp == Key.up or ctrl and (k.cp == 'k' or k.cp == 'p')) + -1 + else if (plain and k.cp == Key.page_down) + 10 + else if (plain and k.cp == Key.page_up) + -10 + else + return false; + if (n == 0) return true; + c.at = @intCast(std.math.clamp(@as(i32, c.at) + step, 0, @as(i32, n) - 1)); + return true; + } +}; + fn add(band: *Band, step: Step) void { if (band.nsteps == max_steps) return; band.steps[band.nsteps] = step; |
