diff options
Diffstat (limited to 'core/Prompt.zig')
| -rw-r--r-- | core/Prompt.zig | 75 |
1 files changed, 48 insertions, 27 deletions
diff --git a/core/Prompt.zig b/core/Prompt.zig index 4db7e424..9dfe9ec2 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -809,10 +809,11 @@ pub fn execWord(p: *Pardes, id: usize) void { /// The span of pane `pane`'s tag its search word takes while its places no /// longer light it (the first Esc cleared them): drawn dimmed (draw.zig). pub fn unlit(p: *const Pardes, pane: *Pane) ?[2]usize { + if (Trail.outOfRest(p, pane)) return null; const t = &pane.trail; - const whole = Trail.folded(p, pane); + const whole = pardes.tagline.folded(p, pane); for (t.steps[0..t.cur], 0..) |*step, i| if (step.* == .search) { - if (lights(pane, &step.search) or t.cur - i > whole) return null; + if (lights(pane, &step.search) or t.isFolded(i, whole)) return null; return t.spanOf(i, whole); }; return null; @@ -1059,15 +1060,15 @@ fn strike(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, line: i32, lo_: } } -/// The head of the prompt's tag: ` s self▌ = Exec Edit x/self/ 4 +/// The head of the prompt's tag: `s self▌ = Exec Edit x/self/ 4 /// selections in 6 lines ⏎ select · M-⏎ as a list · C-r history · esc`. pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { const s = &p.view().tag_state; const pr = &s.prompt; - var text: std.ArrayList(u8) = .empty; - var spans: std.ArrayList(TagState.Span) = .empty; - try text.append(arena, ' '); - try styled(arena, &text, &spans, info.get(pr.op).cell, .key); + var b = try TagState.Builder.of(p, arena, pane); + const text = &b.text; + const spans = &b.spans; + try styled(arena, text, spans, info.get(pr.op).cell, .key); try text.append(arena, ' '); const caret: u32 = @intCast(text.items.len + s.field.caret); const line = s.field.text(); @@ -1077,23 +1078,29 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head { var w: std.Io.Writer = .fixed(&buf); const said = if (pr.op == .command) Command.plainWord(p, &w, line) else plainWord(&w, pr.op, line) catch null; if (said) |verb| { - try styled(arena, &text, &spans, try std.fmt.allocPrint(arena, " = {s}", .{verb}), .dim); + try styled(arena, text, spans, try std.fmt.allocPrint(arena, " = {s}", .{verb}), .dim); try text.print(arena, " {s}", .{w.buffered()}); } + } + // The count and the hints at the row's right end (UX D3). + const at = text.items.len; + 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. + // hints (UX D14), what is shown meanwhile dim; in the count's place, + // which never drops. if (pr.bad) { - try text.appendSlice(arena, " "); - try styled(arena, &text, &spans, regexMessage(line), .err); - try styled(arena, &text, &spans, try std.fmt.allocPrint(arena, " · the {d} from before shown, dimmed", .{@as(usize, pane.body.nsel) + 1}), .dim); + try styled(arena, text, spans, regexMessage(line), .err); + try styled(arena, text, spans, try std.fmt.allocPrint(arena, " · the {d} from before shown, dimmed", .{@as(usize, pane.body.nsel) + 1}), .dim); + try b.right(at, text.items.len); 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); + try pipeCount(p, arena, text, spans, pane, pr, line); + } else try count(arena, text, pane, pr); } - if (pr.op == .command) try Command.count(p, arena, &text); - try text.appendSlice(arena, " "); - try styled(arena, &text, &spans, info.get(pr.op).hint, .dim); + 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); + try b.right(at, hints); return .{ .text = text.items, .spans = spans.items, .cells = 1, .caret = caret }; } @@ -1121,7 +1128,7 @@ fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *c // 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, " · wrapped"); + if (if (pr.op == .search) hits[c].start < origin else hits[c].start > origin) try text.appendSlice(arena, " ↻"); }, .select, .split => if (pr.found == 0) try text.appendSlice(arena, " 0") @@ -1261,8 +1268,22 @@ fn tagOf(p: *Pardes) ![]const u8 { return tagline.tagText(p, p.scratch.allocator(), p.panes[p.view().active].?); } +/// Whether `needle` is in `hay`, a run of two spaces or more in either read +/// as two: a right block's padding is its row's (UX D3). fn has(hay: []const u8, needle: []const u8) bool { - return std.mem.indexOf(u8, hay, needle) != null; + var in: [8192]u8 = undefined; + var of: [1024]u8 = undefined; + return std.mem.indexOf(u8, squeezed(hay, &in), squeezed(needle, &of)) != null; +} + +fn squeezed(s: []const u8, buf: []u8) []const u8 { + var n: usize = 0; + for (s, 0..) |c, i| { + if (c == ' ' and i >= 2 and s[i - 1] == ' ' and s[i - 2] == ' ') continue; + buf[n] = c; + n += 1; + } + return buf[0..n]; } test "s asks in the tag's head: its key cell, the field, the plain word and the count, the selections live in the body; no match is 0, a pattern that is no regex keeps the last result dimmed and says why, Esc puts them back" { @@ -1275,9 +1296,9 @@ test "s asks in the tag's head: its key cell, the field, the plain word and the try std.testing.expectEqual(TagState.Kind.prompt, p.view().tag_state.kind); // No chip on the notice band: the prompt is the tag's. try std.testing.expectEqual(Pane.Prompt.none, pane.prompt); - try std.testing.expect(std.mem.endsWith(u8, try tagOf(p), " s ⏎ select · M-⏎ as a list · C-r history · esc")); + try std.testing.expect(has(try tagOf(p), "s ⏎ select · M-⏎ as a list · C-r history · esc")); typeKeys(p, "self"); - try std.testing.expect(has(try tagOf(p), " s self = Exec Edit x/self/ 2 selections in 3 lines ⏎ select")); + try std.testing.expect(has(try tagOf(p), "s self = Exec Edit x/self/ 2 selections in 3 lines ⏎ select")); try std.testing.expectEqual(@as(u16, 1), pane.body.nsel); try std.testing.expect(selecting(p, pane).?.dim == false); // No match: 0, and the selection it began with. @@ -1317,10 +1338,10 @@ test "Enter keeps what s found, the pattern in / and in the history Up brings ba typeKeys(p, "K"); p.update(.{ .key = .{ .cp = Key.up } }); try std.testing.expectEqualStrings("self..", p.view().tag_state.field.text()); - try std.testing.expect(has(try tagOf(p), " K self.. = Exec Edit g/self../ keeps 2 of 2")); + try std.testing.expect(has(try tagOf(p), "K self.. = Exec Edit g/self../ keeps 2 of 2")); p.update(.{ .key = .{ .cp = Key.down } }); typeKeys(p, "b"); - try std.testing.expect(has(try tagOf(p), " K b = Exec Edit g/b/ keeps 1 of 2 ⏎ keep")); + try std.testing.expect(has(try tagOf(p), "K b = Exec Edit g/b/ keeps 1 of 2 ⏎ keep")); // Ctrl-r: the history under the tag, a row a line. p.update(.{ .key = .{ .cp = 'r', .ctrl = true } }); try std.testing.expectEqual(@as(u16, 1), Band.takes(p, p.view().active)); @@ -1332,7 +1353,7 @@ test "Enter keeps what s found, the pattern in / and in the history Up brings ba typeKeys(p, "|"); try std.testing.expectEqual(Pane.Prompt.none, pane.prompt); p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); + try std.testing.expect(has(try tagOf(p), "| tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); p.update(.{ .key = .{ .cp = Key.enter } }); try std.testing.expectEqualStrings("tr a-z A-Z", p.registers.value('|', 0).?); var asked: ?u32 = null; @@ -1369,7 +1390,7 @@ test "/ searches the body as it is typed: the current place selected, `▸` in t pane.body.cur_row = 2; pane.body.cur_col = 6; typeKeys(p, "/dirty"); - try std.testing.expect(has(try tagOf(p), " / dirty = Look /dirty/ 3/7 · 5 lines ⏎ land · M-⏎ as a list")); + try std.testing.expect(has(try tagOf(p), "/ dirty = Look /dirty/ 3/7 · 5 lines ⏎ land · M-⏎ as a list")); try std.testing.expectEqual(@as(?usize, 2), pane.places.current); try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 0)); try std.testing.expectEqualStrings(" ", try gutterAt(p, frame.allocator(), 1)); @@ -1426,7 +1447,7 @@ test "? searches back from the caret; no match is 0; a pattern that is no regex pane.body.cur_col = 6; const was = pane.body.primaryRange(pane.file.?.content, 0); typeKeys(p, "?dirty"); - try std.testing.expect(has(try tagOf(p), " ? dirty = Look ?dirty? 2/7 · 5 lines ⏎ land")); + try std.testing.expect(has(try tagOf(p), "? dirty = Look ?dirty? 2/7 · 5 lines ⏎ land")); typeKeys(p, "x"); try std.testing.expect(has(try tagOf(p), "= Look ?dirtyx? 0 ⏎ land")); try std.testing.expectEqualDeep(was, pane.body.primaryRange(pane.file.?.content, 0)); @@ -1514,7 +1535,7 @@ test "a pipe prompt runs a known filter live, a run a selection: the outputs gho p.update(.{ .key = .{ .cp = Key.enter } }); typeKeys(p, "|"); p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } }); - try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); + try std.testing.expect(has(try tagOf(p), "| tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace")); const first = lastPipe(p) orelse return error.MissingLiveRun; const asked = p.pipe.pipeRequest(first).?; try std.testing.expectEqual(@as(usize, 2), asked.inputs.len); |
