diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-24 13:11:11 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-24 13:27:54 -0300 |
| commit | 9299281492d63e549ab274034efb59ab4e9d96f6 (patch) | |
| tree | 326e55f07d422adf7fd4e9037107c22bfe70ae6e /src/Finder.zig | |
| parent | 1af098381515fbe3a4fe678cd023c7fd74418ae1 (diff) | |
| download | pardes-9299281492d63e549ab274034efb59ab4e9d96f6.tar.gz pardes-9299281492d63e549ab274034efb59ab4e9d96f6.zip | |
Search converge 6/6: SearchPlace column, the finder column at the right edge holding the query panes; n/N in a frozen query pane; the design doc
Diffstat (limited to 'src/Finder.zig')
| -rw-r--r-- | src/Finder.zig | 240 |
1 files changed, 240 insertions, 0 deletions
diff --git a/src/Finder.zig b/src/Finder.zig new file mode 100644 index 00000000..a2b8f89f --- /dev/null +++ b/src/Finder.zig @@ -0,0 +1,240 @@ +//! The finder column: where query panes go with `SearchPlace column`, the +//! alternative to attaching them under the pane that asked (lifted from +//! the column prototype, cut to its placement). A column with a role at +//! the right edge: the first search opens it and the other columns give it +//! its share once, each in proportion to its weight; after that no pane +//! outside it moves for a search. Its width is a share of the window, not a +//! column weight (layout.compute), so it opens and closes without rounding +//! anyone's width off. Its panes are a recency stack: the newest query pane +//! on top and open, the older ones folded to their tags. A pane opened +//! from a row goes to the column of the pane the query searches, never in +//! here. Its last pane gone, the column goes and every other is as wide as +//! before it came. The role is the column's serial. +const Finder = @This(); + +const std = @import("std"); +const pardes = @import("pardes.zig"); +const layout = @import("layout.zig"); +const config = @import("config.zig"); +const panes = @import("panes.zig"); +const Pardes = pardes.Pardes; + +/// The finder column's serial (layout.columnSerial); 0 while there is none. +serial: u32 = 0, +/// Its width, in thousandths of the window's: kept while it is closed, so +/// it comes back as wide as it went. +share: u16 = 300, + +/// More query panes than this and the oldest closes: each folded one keeps +/// a tag row. ponytail: a fixed count; size it from the column's rows. +pub const max_results = 8; + +const tag_text = "Finder Delcol"; + +/// The finder column's index, while it is there. +pub fn column(f: *const Finder, p: *const Pardes) ?usize { + if (f.serial == 0) return null; + return layout.columnBySerial(p, f.serial); +} + +/// Its width in cells: its share, leaving every other column MINW; the +/// whole window when it is the only column. +pub fn width(f: *const Finder, p: *const Pardes) u16 { + if (p.ncol <= 1) return p.screen_w; + const others: u32 = @as(u32, config.MINW) * @as(u32, @intCast(p.ncol - 1)); + const most: u32 = @as(u32, p.screen_w) -| others; + const wanted: u32 = @as(u32, p.screen_w) * f.share / 1000; + return @intCast(@min(@max(wanted, config.MINW), @max(most, @min(config.MINW, p.screen_w)))); +} + +/// Query pane `id` goes on top of the finder column, opened at the right +/// edge if there is none; the older ones fold. False when the window has +/// no room for the column (the caller attaches it instead). +pub fn place(f: *Finder, p: *Pardes, id: usize) bool { + const pane = p.panes[id] orelse return false; + const c = f.column(p) orelse f.open(p) orelse return false; + layout.insert(p, c, 0, id); + pane.vweight = 1; + pane.collapsed = false; + f.fold(p, c, id); + // The oldest goes once there are too many: the bottom one, as the top + // is always the newest. + while (p.col_n[c] > max_results) { + const oldest = p.col_panes[c][p.col_n[c] - 1]; + p.removePane(oldest, null) catch break; + } + return true; +} + +/// A finder column at the right edge, empty, its tag its own: the other +/// columns give it its share, each in proportion to its weight. Null when +/// the window has no room for one more column, or narrower columns would +/// leave a pane under its tag and 2 rows. +fn open(f: *Finder, p: *Pardes) ?usize { + if (p.ncol >= pardes.MAX_COLS or p.ncol == 0) return null; + if (p.screen_w < @as(u32, config.MINW) * @as(u32, @intCast(p.ncol + 1))) return null; + const c = p.ncol; + p.col_n[c] = 0; + // Not used for its width (layout.compute), only kept positive for the + // code that halves and joins weights. + p.col_weight[c] = layout.column_weight_unit; + p.col_tags[c] = .{ .what = .column, .gpa = p.gpa }; + p.col_tags[c].own = p.gpa.dupe(u8, tag_text) catch null; + p.col_serial[c] = 0; + p.ncol += 1; + f.serial = layout.columnSerial(p, c); + // A narrower column wraps long tags onto more rows: shared out again, + // and where that cannot hold every minimum there is no finder, as + // Newcol is refused. + layout.compute(p); + for (0..p.ncol) |k| if (k != c and !layout.columnAtMinimums(p, k) and !layout.shareColumn(p, k)) { + layout.dropColumnUnannounced(p, c); + f.serial = 0; + layout.compute(p); + return null; + }; + layout.noteColumn(p, "newcol", f.serial); + return c; +} + +/// Fold every pane of finder column `c` but `top` (and what is attached +/// to it, its +Preview) to its tag. +fn fold(f: *const Finder, p: *Pardes, c: usize, top: usize) void { + _ = f; + layout.compute(p); + for (p.col_panes[c][0..p.col_n[c]]) |k| { + const pane = p.panes[k] orelse continue; + if (k == top or layout.groupRoot(p, k) == top) { + pane.collapsed = false; + pane.vweight = 1; + } else if (!pane.collapsed) { + // As Collapse folds it: its weight keeps its height for later. + pane.vweight = @floatFromInt(@max(1, p.rects[k].h)); + pane.collapsed = true; + } + } + layout.compute(p); +} + +/// Query pane `id`, asked again: back on top of the stack, open, the rest +/// folded. Nothing outside the finder column moves. +pub fn raise(f: *Finder, p: *Pardes, id: usize) void { + const c = f.column(p) orelse return; + const at = layout.findPane(p, id) orelse return; + if (at.col != c) return; + if (at.idx != 0) { + // the pane and what is attached to it (its +Preview) move together + const end = layout.groupEnd(p, c, at.idx); + var moved: [pardes.MAX_PANES]usize = undefined; + const n = end - at.idx; + @memcpy(moved[0..n], p.col_panes[c][at.idx..end]); + for (moved[0..n]) |k| layout.removePane(p, k); + for (moved[0..n], 0..) |k, i| layout.insert(p, c, i, k); + } + f.fold(p, c, id); +} + +/// The column a pane asked for from the finder goes in (a row looked at, a +/// command run there): the column of the pane its query searches, else +/// the nearest other. Null when nothing points at the finder. +pub fn away(f: *Finder, p: *Pardes, from: usize, kind: pardes.exec.NewPane) ?usize { + const fc = f.column(p) orelse return null; + const in_finder = if (layout.findPane(p, from)) |at| at.col == fc else false; + const to_last = (kind == .command or kind == .errors) and fc == p.ncol - 1; + if (!in_finder and p.exec_column != fc and !to_last) return null; + if (in_finder) { + const root = layout.groupRoot(p, from); + if (p.panes[root]) |qp| if (qp.query) |q| if (p.paneBySerial(q.owner)) |oid| if (layout.findPane(p, oid)) |at| + if (at.col != fc) return at.col; + } + if (fc > 0) return fc - 1; + if (fc + 1 < p.ncol) return fc + 1; + // The finder is the only column: a document asked from it gets one of + // its own, left of it. + return layout.insertColumn(p, fc, true); +} + +/// The finder column has lost its last pane: it goes, and every other +/// column is as wide as before it came. +pub fn emptied(f: *Finder, p: *Pardes, c: usize) bool { + if (f.column(p) != c or p.col_n[c] != 0) return false; + layout.dropColumn(p, c); + f.serial = 0; + return true; +} + +/// A drag of the border between the finder column and column `other` +/// beside it: the finder's share follows the edge, and `other` takes the +/// difference, every other column keeping its width. +pub fn dragWidth(f: *Finder, p: *Pardes, other: usize, finder_w: u64) void { + const fc = f.column(p) orelse return; + if (p.screen_w == 0) return; + const combined: u64 = p.col_w[fc] + p.col_w[other]; + if (combined < 2 * @as(u64, config.MINW)) return; + const fw = std.math.clamp(finder_w, config.MINW, combined - config.MINW); + f.share = @intCast(@max(1, @min(999, fw * 1000 / p.screen_w))); + var rest_weight: u128 = 0; + var rest_cells: u64 = 0; + for (0..p.ncol) |k| if (k != fc and k != other) { + rest_weight += p.col_weight[k]; + rest_cells += p.col_w[k]; + }; + if (rest_cells > 0) { + const other_cells = combined - fw; + const w = @as(u128, other_cells) * rest_weight / rest_cells; + p.col_weight[other] = @intCast(std.math.clamp(w, 1, layout.max_column_weight)); + } + layout.compute(p); +} + +fn typeKeys(p: *Pardes, text: []const u8) void { + for (text) |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); +} + +test "SearchPlace column: query panes stack in a finder column at the right edge, nothing else moves after it opens, and it goes with its last pane" { + const Key = pardes.Key; + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 40 }); + defer p.deinit(); + p.settings.search_place = .column; + const pane = try p.setTestFile("alpha one\nbeta\nalphabet\n"); + p.newScratchBelow(0); + const other = p.active; + p.active = 0; + layout.compute(p); + const before = p.rects; + const width_before = p.col_w[0]; + typeKeys(p, "/"); + const fc = p.finder.column(p).?; + try std.testing.expectEqual(@as(usize, 1), fc); + const first = p.col_panes[fc][0]; + try std.testing.expect(p.panes[first].?.query != null); + // the first search narrows the column once; its panes keep their rows + try std.testing.expect(p.col_w[0] < width_before); + try std.testing.expectEqual(before[0].h, p.rects[0].h); + try std.testing.expectEqual(before[other].h, p.rects[other].h); + typeKeys(p, "alpha"); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(@as(usize, 0), p.active); + try std.testing.expect(pane.places.started); + layout.compute(p); + const opened = p.rects; + // a second search, from the other pane: on top, the first folded + p.active = other; + typeKeys(p, "/"); + const second = p.col_panes[fc][0]; + try std.testing.expect(second != first); + try std.testing.expect(p.panes[first].?.collapsed); + layout.compute(p); + try std.testing.expectEqualDeep(opened[0], p.rects[0]); + try std.testing.expectEqualDeep(opened[other], p.rects[other]); + // Esc: the second closes, the first stays + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(p.panes[second] == null); + try std.testing.expectEqual(fc, p.finder.column(p).?); + // the first's Del: the column goes, every other as wide as before it + pardes.exec.runBuiltin(p, .Del, first, "", null); + try std.testing.expect(p.finder.column(p) == null); + layout.compute(p); + try std.testing.expectEqual(width_before, p.col_w[0]); + try std.testing.expectEqualDeep(before[0], p.rects[0]); +} |
