summaryrefslogtreecommitdiff
path: root/core/Band.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 19:31:05 -0300
committerGabriel Schneider <[email protected]>2026-11-24 19:31:05 -0300
commitf44d1b7d040acf674814be3cde336527833450e9 (patch)
tree9c22c45541f16e4281a0a504084e3cf6e389d1c8 /core/Band.zig
parent326a2bac9558da0ff78987e697546a363b2b8e59 (diff)
downloadpardes-f44d1b7d040acf674814be3cde336527833450e9.tar.gz
pardes-f44d1b7d040acf674814be3cde336527833450e9.zip
TagState, Field, Band: the typing state, and Help over the binding tree (walk UI, pick-ui SPEC.md §3.2's typing row and §4.5, item 4 of line A): a tag's typing state is the view's (TagState.kind help, its host pane by serial, the hostOf every tag's text asks), its box `^` whatever the body's mode, its caret a bar at the field's (the body's caret and its layer's put by while the head is typed in), and it takes every key before anything but a question (keys.zig tagStateKey: Esc back to rest, Enter runs the row typed as its keys, Backspace on an empty field back to the level `?` was struck from, unrolled); SPC ? is Help over every leaf of every root (SPC's, g m z Z [ ]'s, window mode's, the addons' words) and `?` down a level or under a helix prefix Help for that level's (SPC l ?, g ?); its head ` Help ␣ l <field> n/N`; its rows `chord · word · group` scored by the query pane's scorer (Names.match, now pub) on each key into the view's fixed hits (max_hits: the tree's 160 leaves and the registry's 64 words; best first, the tree's order while the field is empty), the band shows eleven around the cursor, the query's bytes in the match tint, the cursor's leaf's doc beside them; `Help <name>` stays the word's +Help; FOR THE PICKERS AND PROMPTS, as they are: Field (core/Field.zig: a one-line field in a fixed 256-byte buffer with its caret, each view's in TagState.field; Field.key takes a line editor's keys: characters, Backspace/C-h, Delete/C-d, Left/C-b, Right/C-f, Home/C-a, End/C-e, C-u, C-k, C-w/M-Backspace, M-b, M-f; false for a key it has no use for, Enter, Esc, the arrows, which its owner takes; set, insert, clear) and Band.Cursor (a list's cursor, TagState.rows: Cursor.key steps it on Down/C-j/C-n, Up/C-k/C-p, PageDown/PageUp; Cursor.window(n) is the eleven rows [lo, hi) a band shows around it); a state's head is a TagState.Head {text, spans, caret}, its band Band.of's switch on TagState.kind; TagState.follow takes no argument now (a root a state puts back is current already: Help's Backspace keeps its level unrolled); TESTS: Field.zig's (a line editor's keys, and the keys it leaves), TagState.zig's two (SPC ? over every root, filtered, the box and the caret, Enter runs Wrap; SPC l ? and g ? scoped, Backspace back to the level unrolled, Esc to rest); core-test 1003 pass, 166 skip, 0 fail
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/Band.zig')
-rw-r--r--core/Band.zig87
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;