diff options
| -rw-r--r-- | core/Prompt.zig | 84 | ||||
| -rw-r--r-- | test/snapshots/fsearch.golden | 2 |
2 files changed, 73 insertions, 13 deletions
diff --git a/core/Prompt.zig b/core/Prompt.zig index 9dfe9ec2..ad2cfbed 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -1083,7 +1083,11 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { } } // The count and the hints at the row's right end (UX D3). + const left = b.col(); const at = text.items.len; + // Where a search's ` · N lines` starts after its count: dim, and gone + // with the hints (UX E7). + var tail: ?usize = null; if (line.len > 0) { // A pattern that is no regex: why, in the err ink, in place of the // hints (UX D14), what is shown meanwhile dim; in the count's place, @@ -1095,11 +1099,23 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } else if (behavior(pr.op) != null) { try pipeCount(p, arena, text, spans, pane, pr, line); - } else try count(arena, text, pane, pr); + } else tail = try count(arena, text, pane, pr); } if (pr.op == .command) try Command.count(p, arena, text); const hints = text.items.len; - try styled(arena, text, spans, info.get(pr.op).hint, .dim); + const hint = info.get(pr.op).hint; + if (tail) |t| { + // Both beside the count, or neither: as much room as the right + // block asks for its two gaps (Builder.right drops the hints alone). + const cells = File.displayWidth(p.settings.tab, std.mem.trimStart(u8, text.items[at..], " ")) + File.displayWidth(p.settings.tab, hint); + if (left + 2 + cells + 2 > b.width) { + text.shrinkRetainingCapacity(t); + try b.right(at, t); + return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; + } + try spans.append(arena, .{ .lo = @intCast(t), .hi = @intCast(hints), .style = .dim }); + } + try styled(arena, text, spans, hint, .dim); try b.right(at, hints); return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } @@ -1111,24 +1127,34 @@ fn styled(arena: std.mem.Allocator, text: *std.ArrayList(u8), spans: *std.ArrayL } /// The prompt's count after its field: what it found (`4 selections in 6 -/// lines`, `keeps 3 of 9`, `3/7 · 5 lines`, `3/⋯` while its places are -/// found, `0`). -fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *const Prompt) !void { +/// lines`, `keeps 3 of 9`, `3/7 · 5 lines`, `1/7 ↻ · 5 lines`, `3/⋯` while +/// its places are found, `0`); where a search's ` · N lines` starts. +fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *const Prompt) !?usize { switch (pr.op) { .search, .search_back => { const set = &pane.places; - if (!set.shows(pane)) return; + if (!set.shows(pane)) return null; const hits = set.hits.items; - if (set.complete() and hits.len == 0) return text.appendSlice(arena, " 0"); + if (set.complete() and hits.len == 0) { + try text.appendSlice(arena, " 0"); + return null; + } try text.appendSlice(arena, " "); const at = if (set.current) |c| (if (c < hits.len) c else null) else null; if (at) |c| try text.print(arena, "{d}", .{c + 1}) else try text.appendSlice(arena, "–"); - if (!set.complete()) return text.appendSlice(arena, "/⋯"); - try text.print(arena, "/{d} · {d} line{s}", .{ hits.len, pr.lines, plural(pr.lines) }); + if (!set.complete()) { + try text.appendSlice(arena, "/⋯"); + return null; + } + try text.print(arena, "/{d}", .{hits.len}); // Round the end from where it began. - const c = at orelse return; - const origin = set.origin(pane); - if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " ↻"); + if (at) |c| { + const origin = set.origin(pane); + if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " ↻"); + } + const tail = text.items.len; + try text.print(arena, " · {d} line{s}", .{ pr.lines, plural(pr.lines) }); + return tail; }, .select, .split => if (pr.found == 0) try text.appendSlice(arena, " 0") @@ -1137,6 +1163,7 @@ fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *c .keep, .remove => try text.print(arena, " keeps {d} of {d}", .{ pr.found, pane.nsel_snap }), else => {}, } + return null; } /// A pipe prompt's count: `6 lines → 5 · exit 0`, `6 runs · 5 ok · 1 exit 2 @@ -1668,3 +1695,36 @@ test "Alt-Enter in / or s keeps the result as a list (SPEC §7.2): where jumps w try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); try std.testing.expect(p.panes[p.view().active].?.query != null); } + +test "a search's count keeps ` · N lines` after it in the dim ink, and drops it with the hints where they do not fit beside it; a wrap's ↻ is the count's (UX E7, D3)" { + for ([_]u16{ 200, 52 }) |cols| { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = cols, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile(dirty); + pane.body.cur_row = 2; + pane.body.cur_col = 6; + typeKeys(p, "/dirty"); + const h = try head(p, p.scratch.allocator(), pane); + const lines = std.mem.indexOf(u8, h.text, " \u{b7} 5 lines"); + if (cols == 200) { + try std.testing.expect(has(h.text, "3/7 \u{b7} 5 lines \u{23ce} land")); + const dim = for (h.spans) |span| { + if (span.lo == lines.? and span.style == .dim) break span; + } else return error.TestUnexpectedResult; + try std.testing.expectEqualStrings(" \u{b7} 5 lines", h.text[dim.lo..dim.hi]); + } else { + // No room for the hints: the count alone, at the row's end. + try std.testing.expect(lines == null); + try std.testing.expect(std.mem.indexOf(u8, h.text, "\u{23ce} land") == null); + try std.testing.expect(std.mem.endsWith(u8, h.text, "3/7")); + } + } + // From the last line round the end: `↻` after the count, before its lines. + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 }); + defer p.deinit(); + const pane = try p.setTestFile(dirty); + pane.body.cur_row = 7; + pane.body.cur_col = 6; + typeKeys(p, "/dirty"); + try std.testing.expect(has(try tagOf(p), "1/7 \u{21bb} \u{b7} 5 lines \u{23ce} land")); +} diff --git a/test/snapshots/fsearch.golden b/test/snapshots/fsearch.golden index 59a1b0bb..e7814fb5 100644 --- a/test/snapshots/fsearch.golden +++ b/test/snapshots/fsearch.golden @@ -52,7 +52,7 @@ |13: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, |14: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, |15: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-179 d,#0d1410, -|16: 0-1 d,#496656, 2-30 #b2c3b8,#1d2824, 31-40 #d3b0d9,#1d2824, 41-89 #b2c3b8,#1d2824, 90-91 #000000,#add38a, 92-98 #becfc4,#2c2c0f, 99-99 #bada9d,#2c2c0f,b 100-100 #bada9d,#2c2c0f, 101-106 #becfc4,#2c2c0f, 107-113 #79836f,#2c2c0f, 114-137 #becfc4,#2c2c0f, 138-179 #79836f,#2c2c0f, +|16: 0-1 d,#496656, 2-30 #b2c3b8,#1d2824, 31-40 #d3b0d9,#1d2824, 41-89 #b2c3b8,#1d2824, 90-91 #000000,#add38a, 92-98 #becfc4,#2c2c0f, 99-99 #bada9d,#2c2c0f,b 100-100 #bada9d,#2c2c0f, 101-106 #becfc4,#2c2c0f, 107-113 #79836f,#2c2c0f, 114-126 #becfc4,#2c2c0f, 127-179 #79836f,#2c2c0f, |17: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#608269, 91-94 #e5eee5,#0d1410, 95-96 #59675c,#0d1410, 97-179 #e5eee5,#0d1410, |18: 0-0 d,#608269, 1-1 d,#0d1410, 2-89 #e5eee5,#0d1410, 90-90 #e5eee5,#e5eee5, 91-91 #e5eee5,#0d1410, 92-94 #e5eee5,#0d1410,b 95-95 #616f64,#0d1410,b 96-96 #bada9d,#0d1410,b 97-100 #f5f7ee,#3c614a, 101-101 #f5f7ee,#0d1410, 102-179 #e5eee5,#0d1410, |19: 0-0 d,#608269, 1-89 d,#0d1410, 90-90 d,#608269, 91-94 d,#0d1410, 95-96 #59675c,#0d1410, 97-179 #e5eee5,#0d1410, |
