summaryrefslogtreecommitdiff
path: root/core/Field.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/Field.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/Field.zig')
-rw-r--r--core/Field.zig142
1 files changed, 142 insertions, 0 deletions
diff --git a/core/Field.zig b/core/Field.zig
new file mode 100644
index 00000000..7b8ea399
--- /dev/null
+++ b/core/Field.zig
@@ -0,0 +1,142 @@
+//! A field typed in a tag's head (walk UI, pick-ui SPEC.md §3.2, the typing
+//! row): one line of text and its caret, in a fixed buffer, each view's
+//! own (TagState.field). Help's query, a picker's, a prompt's: the keys
+//! that edit it are a line editor's (readline's, helix's prompt's), and
+//! the state that owns it takes the rest (Enter, Esc, the arrows a list
+//! steps with).
+const Field = @This();
+
+const std = @import("std");
+const pardes = @import("root.zig");
+const modal = @import("modal.zig");
+const Key = pardes.Key;
+
+/// The longest field, in bytes: a query, a command line, a pattern.
+pub const max = 256;
+
+buf: [max]u8 = undefined,
+len: u16 = 0,
+/// A byte offset in the text, at a character's start.
+caret: u16 = 0,
+
+pub const replica = .{ .len = .{.{ "buf", "len" }} };
+
+pub fn text(f: *const Field) []const u8 {
+ return f.buf[0..f.len];
+}
+
+pub fn clear(f: *Field) void {
+ f.len = 0;
+ f.caret = 0;
+}
+
+/// `s` in place of the text, the caret at its end (a history's line).
+pub fn set(f: *Field, s: []const u8) void {
+ var n = @min(s.len, max);
+ while (n > 0 and n < s.len and s[n] & 0xC0 == 0x80) n -= 1;
+ @memcpy(f.buf[0..n], s[0..n]);
+ f.len = @intCast(n);
+ f.caret = f.len;
+}
+
+/// An editing key taken: true, the text or the caret moved (or a key that
+/// would have, at an edge). False for a key the field has no use for,
+/// which its owner takes.
+pub fn key(f: *Field, k: Key) bool {
+ const t = f.text();
+ const ctrl = k.ctrl and !k.alt;
+ const alt = k.alt and !k.ctrl;
+ const plain = !k.ctrl and !k.alt;
+ if (plain and k.cp == Key.backspace or ctrl and k.cp == 'h') {
+ if (f.caret > 0) f.cut(prev(t, f.caret), f.caret);
+ } else if (plain and k.cp == Key.delete or ctrl and k.cp == 'd') {
+ if (f.caret < f.len) f.cut(f.caret, @intCast(modal.nextGrapheme(t, f.caret)));
+ } else if (plain and k.cp == Key.left or ctrl and k.cp == 'b') {
+ f.caret = prev(t, f.caret);
+ } else if (plain and k.cp == Key.right or ctrl and k.cp == 'f') {
+ if (f.caret < f.len) f.caret = @intCast(modal.nextGrapheme(t, f.caret));
+ } else if (plain and k.cp == Key.home or ctrl and k.cp == 'a') {
+ f.caret = 0;
+ } else if (plain and k.cp == Key.end or ctrl and k.cp == 'e') {
+ f.caret = f.len;
+ } else if (ctrl and k.cp == 'u') {
+ f.cut(0, f.caret);
+ } else if (ctrl and k.cp == 'k') {
+ f.cut(f.caret, f.len);
+ } else if (ctrl and k.cp == 'w' or alt and k.cp == Key.backspace) {
+ f.cut(wordBack(t, f.caret), f.caret);
+ } else if (alt and k.cp == 'b') {
+ f.caret = wordBack(t, f.caret);
+ } else if (alt and k.cp == 'f') {
+ f.caret = wordOn(t, f.caret);
+ } else if (plain and k.cp >= 0x20 and k.cp != Key.backspace and k.cp < 0xF0000) {
+ var utf: [4]u8 = undefined;
+ const n = std.unicode.utf8Encode(k.cp, &utf) catch return true;
+ f.insert(if (k.text.len > 0) k.text else utf[0..n]);
+ } else return false;
+ return true;
+}
+
+/// `s` typed at the caret; what does not fit is dropped whole.
+pub fn insert(f: *Field, s: []const u8) void {
+ if (f.len + s.len > max) return;
+ std.mem.copyBackwards(u8, f.buf[f.caret + s.len .. f.len + s.len], f.buf[f.caret..f.len]);
+ @memcpy(f.buf[f.caret..][0..s.len], s);
+ f.len += @intCast(s.len);
+ f.caret += @intCast(s.len);
+}
+
+fn cut(f: *Field, lo: u16, hi: u16) void {
+ std.mem.copyForwards(u8, f.buf[lo .. f.len - (hi - lo)], f.buf[hi..f.len]);
+ f.len -= hi - lo;
+ f.caret = lo;
+}
+
+fn prev(t: []const u8, at: u16) u16 {
+ var i = at;
+ while (i > 0) {
+ i -= 1;
+ if (t[i] & 0xC0 != 0x80) break;
+ }
+ return i;
+}
+
+fn wordBack(t: []const u8, at: u16) u16 {
+ var i = at;
+ while (i > 0 and t[i - 1] == ' ') i -= 1;
+ while (i > 0 and t[i - 1] != ' ') i -= 1;
+ return i;
+}
+
+fn wordOn(t: []const u8, at: u16) u16 {
+ var i = at;
+ while (i < t.len and t[i] == ' ') i += 1;
+ while (i < t.len and t[i] != ' ') i += 1;
+ return i;
+}
+
+test "a field types, moves and deletes as a line editor does" {
+ var f: Field = .{};
+ for ("find me") |c| try std.testing.expect(f.key(.{ .cp = c }));
+ try std.testing.expectEqualStrings("find me", f.text());
+ try std.testing.expect(f.key(.{ .cp = 'w', .ctrl = true }));
+ try std.testing.expectEqualStrings("find ", f.text());
+ try std.testing.expect(f.key(.{ .cp = Key.home }));
+ try std.testing.expect(f.key(.{ .cp = '>' }));
+ try std.testing.expectEqualStrings(">find ", f.text());
+ try std.testing.expectEqual(@as(u16, 1), f.caret);
+ try std.testing.expect(f.key(.{ .cp = Key.backspace }));
+ try std.testing.expectEqualStrings("find ", f.text());
+ try std.testing.expect(f.key(.{ .cp = 0x65e5 })); // 日
+ try std.testing.expect(f.key(.{ .cp = Key.left }));
+ try std.testing.expectEqual(@as(u16, 0), f.caret);
+ try std.testing.expect(f.key(.{ .cp = Key.delete }));
+ try std.testing.expectEqualStrings("find ", f.text());
+ try std.testing.expect(f.key(.{ .cp = 'k', .ctrl = true }));
+ try std.testing.expectEqualStrings("", f.text());
+ // Enter, Esc and the arrows a list steps with are its owner's.
+ try std.testing.expect(!f.key(.{ .cp = Key.enter }));
+ try std.testing.expect(!f.key(.{ .cp = Key.escape }));
+ try std.testing.expect(!f.key(.{ .cp = Key.down }));
+ try std.testing.expect(!f.key(.{ .cp = 'n', .ctrl = true }));
+}