summaryrefslogtreecommitdiff
path: root/core/Pick.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Pick.zig')
-rw-r--r--core/Pick.zig98
1 files changed, 97 insertions, 1 deletions
diff --git a/core/Pick.zig b/core/Pick.zig
index cb5f605d..efadc881 100644
--- a/core/Pick.zig
+++ b/core/Pick.zig
@@ -680,6 +680,45 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step {
// ---- the preview ----
+/// The line (from 0) pane `pane`'s body lands the live picker's cursor row
+/// on, while it is that picker's landing pane previewing that row's file
+/// (UX D7: drawn on the hit ground, body_layer.zig); null else.
+pub fn landingLine(p: *Pardes, arena: std.mem.Allocator, pane: *const Pane) ?usize {
+ const pk = live(p) orelse return null;
+ if (!pk.previews or pk.landing != pane.serial or pk.source == .themes) return null;
+ const s = &p.view().tag_state;
+ if (pk.shown != s.rows.at or count(p, pk) == 0) return null;
+ const row = rowAt(p, pk, arena, s.rows.at) catch return null;
+ const place = switch (row.target) {
+ .place => |t| t,
+ else => return null,
+ };
+ const f = pane.file orelse return null;
+ if (!std.mem.eql(u8, f.path, place.path)) return null;
+ return @intCast(@max(0, pane.body.cur_row));
+}
+
+/// The notice row's sentence while a picker previews (UX D7), written to
+/// `w`: where Enter lands (`here`, or the owner's reader by its address,
+/// a word to Look at), that the body is the preview, and Shift-Enter's new
+/// pane; the address's bytes in what was written, or null with no picker
+/// previewing.
+pub fn sentence(p: *const Pardes, w: *std.Io.Writer) ?[2]usize {
+ const pk = live(p) orelse return null;
+ if (!pk.previews or pk.source == .themes) return null;
+ if (pk.landing == pk.owner) {
+ w.writeAll("⏎ lands here") catch return null;
+ } else {
+ w.writeAll("⏎ lands in ") catch return null;
+ }
+ const lo = w.end;
+ if (pk.landing != pk.owner) w.print("@p{d}", .{pk.owner}) catch return null;
+ const hi = w.end;
+ if (pk.landing != pk.owner) w.writeAll("'s reader") catch return null;
+ w.writeAll(" · its text is the preview · S-⏎ a new pane") catch return null;
+ return .{ lo, hi };
+}
+
/// The landing pane's body showing the current row's place (SPEC §5.3):
/// a file at its line, the query's matches lit for a Grep. Its own text is
/// set aside the first time, put back when the picker rolls up. A file an
@@ -745,7 +784,7 @@ fn preview(p: *Pardes, pk: *Pick) void {
pane.body.cur_col = 0;
return;
}
- look.focusPaneLine(p, id, at, .center);
+ look.focusPaneLine(p, id, at, .third);
// A Grep's matches in the file lit, as its own pane would have them.
if (pk.source == .grep) pane.places.search(p, pane, .list, pk.id, std.mem.trim(u8, s.field.text(), " "), true, null, false);
}
@@ -1984,3 +2023,60 @@ test "the themes picker marks the theme in use: `in use` at its row's right end
band = (try Band.of(p, a, pane, id)).?;
try std.testing.expectEqualStrings("in use", Find.row(band, pardes.colors.themes[was].name).?.meta);
}
+
+test "a picker's preview is legible as one (UX D7): its landing line on the hit ground with `▸` in the gutter, a third of the body above it, and the notice row saying where Enter lands" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 });
+ defer p.deinit();
+ p.settings.jumps = .walk;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(std.testing.allocator);
+ for (1..81) |i| try text.print(std.testing.allocator, "line {d} of the file\n", .{i});
+ const path = try testFile(p, &tmp, "long.zig", text.items);
+ defer p.gpa.free(path);
+ _ = try p.setTestFile("held\n");
+ const asker = p.view().active;
+ Trail.keep(p.panes[asker].?);
+ const rows = try std.fmt.allocPrint(std.testing.allocator, "{s}:40:6 line 40 of the file\n", .{path});
+ defer std.testing.allocator.free(rows);
+ open(p, asker, .list, "Refs", "", rows);
+ const pk = live(p).?;
+ const reader = p.paneBySerial(pk.landing).?;
+ p.update(.tick);
+ const pane = p.panes[reader].?;
+ try std.testing.expectEqualStrings(path, pane.file.?.path);
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ const s = try p.render(frame.allocator());
+ const r = p.view().rects[reader];
+ const body_y = p.bodyTop(pane, r);
+ // Line 40 a third of the way down: ⌈rows/3⌉ rows of context above it.
+ var at: ?u16 = null;
+ for (0..pane.rows) |row| if (pane.wrapAt(p.settings.tab, @intCast(row)).line == 39) {
+ at = @intCast(row);
+ break;
+ };
+ try std.testing.expectEqual(@as(?u16, @intCast((pane.rows + 2) / 3)), at);
+ const y = body_y + at.?;
+ const roles = &p.chromeTheme().roles;
+ const tx = r.x + pardes.config.GUTTER;
+ const gutter = File.gutterWidth(pane);
+ // Its text on the hit ground across the row; the row above as it was.
+ for (gutter..r.w - pardes.config.GUTTER) |x| try std.testing.expectEqual(pardes.Color{ .rgb = roles.hit }, s.at(tx + @as(u16, @intCast(x)), y).style.bg);
+ try std.testing.expect(!std.meta.eql(s.at(tx + gutter + 2, y - 1).style.bg, pardes.Color{ .rgb = roles.hit }));
+ // `▸` in the key ink in the gutter's cell after the number.
+ const mark = s.at(tx + gutter - 1, y);
+ try std.testing.expectEqualStrings("▸", mark.grapheme());
+ try std.testing.expectEqual(pardes.Color{ .rgb = roles.key }, mark.style.fg);
+ // The notice row: where Enter lands, the owner's address a word in it.
+ var buf: [pardes.Messages.notice_row_cap]u8 = undefined;
+ const said = pardes.Messages.noticeRow(p, 120, &buf);
+ const want = try std.fmt.allocPrint(frame.allocator(), "⏎ lands in @p{d}'s reader · its text is the preview · S-⏎ a new pane", .{p.panes[asker].?.serial});
+ try std.testing.expect(std.mem.startsWith(u8, said.text, want));
+ try std.testing.expect(said.quiet_left);
+ try std.testing.expectEqualStrings(try std.fmt.allocPrint(frame.allocator(), "@p{d}", .{p.panes[asker].?.serial}), said.text[said.word[0]..said.word[1]]);
+ // At rest: no ground, no mark, the row back to its own.
+ p.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ try std.testing.expect(!pardes.Messages.noticeRow(p, 120, &buf).quiet_left);
+}