diff options
Diffstat (limited to 'core/body_layer.zig')
| -rw-r--r-- | core/body_layer.zig | 131 |
1 files changed, 111 insertions, 20 deletions
diff --git a/core/body_layer.zig b/core/body_layer.zig index 8bc38e23..0107b3e0 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -349,7 +349,11 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane panes.File.recolorSyntax(p, s, pane, f, r, tx, tw, body_h); panes.Tables.paint(p, s, pane, r, tx, tw, body_h); drawWrapMarkers(p, s, pane, r, tx, tw, body_h, pane_bg); + // A picker's landing line under the places it lights (UX D7). + const landing = pardes.Pick.landingLine(p, arena, pane); + if (landing) |line| paintLanding(p, s, pane, r, tx, tw, body_h, line); pane.places.paint(p, s, pane, r, body_h); + if (landing) |line| paintLandingSpan(p, s, pane, r, f, line); pardes.Prompt.paintGutter(p, s, pane, r, body_h); panes.Query.paint(p, s, pane, r, body_h); } else if (pane.isTerminal()) { @@ -652,6 +656,47 @@ pub fn wordAt(p: *Pardes, arena: std.mem.Allocator, id: usize, row: usize, col: return text.items[b.lo..b.hi]; } +/// A picker's landing line (UX D7): its text on the hit ground across +/// the body's width, and `▸` in the key ink in the gutter's cell after its +/// number (the one a prompt's current place has, Prompt.paintGutter), on +/// each row it wraps onto. +fn paintLanding(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, tx: u16, tw: u16, body_h: u16, line: usize) void { + const roles = &p.chromeTheme().roles; + const gutter = File.gutterWidth(pane); + const number = gutter -| File.laneCells(pane) -| pane.layers.heat -| 1; + const body_y = p.bodyTop(pane, r); + var vr: u16 = pane.context_rows; + while (vr < body_h) : (vr += 1) { + const w = pane.wrapAt(p.settings.tab, vr); + if (w.line != @as(i32, @intCast(line))) continue; + for (gutter..tw) |x| { + const cell = s.at(tx + @as(u16, @intCast(x)), body_y + vr); + cell.default = false; + cell.style.bg = .{ .rgb = roles.hit }; + } + if (gutter > 0 and w.at == 0) { + var style = s.at(tx + number, body_y + vr).style; + style.fg = .{ .rgb = roles.key }; + s.set(tx + number, body_y + vr, "▸", style); + } + } +} + +/// The landing line's matched span (a Grep's match at the row's column) on +/// the current hit's ground, over the places lit on it (UX D7). +fn paintLandingSpan(p: *Pardes, s: *Surface, pane: *Pane, r: Rect, f: *panes.File.State, line: usize) void { + if (!pane.places.shows(pane)) return; + const hits = pane.places.hits.items; + const ls = File.lineStart(p.gpa, f, line); + const caret = ls + @as(usize, @intCast(@max(0, pane.body.cur_col))); + var i = panes.Places.firstFrom(hits, ls); + while (i < hits.len and hits[i].start <= caret) : (i += 1) { + if (hits[i].end <= caret) continue; + File.paintWordSelection(p, s, pane, r, @intCast(line), @intCast(hits[i].start - ls), @intCast(hits[i].end - ls), p.chromeTheme().roles.hit_current); + return; + } +} + /// Painted targets (SPEC §4.4), while a motion root's level shows in this /// body's tag: each of its keys at the line it would go to, in the gutter /// (`g e t b .` for `g`; for `]` the next and for `[` the previous @@ -708,10 +753,9 @@ fn paintTargets(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, return; } if (cur.root != .goto and cur.root != .next and cur.root != .prev) return; - const th = p.theme(); - const style: CellStyle = .{ .fg = .{ .rgb = p.chromeTheme().box }, .bg = if (th.bg) |c| .{ .rgb = c } else .default, .bold = true }; - // The rows painted so far: a second key on a row goes after the first. - var painted: [bindings.max_children]u16 = undefined; + // Each target's row and key, in the level's order. + var rows: [bindings.max_children]u16 = undefined; + var keys: [bindings.max_children]u8 = undefined; var n: usize = 0; for (level) |c| { const leaf = (c.node orelse continue).leaf orelse continue; @@ -723,12 +767,34 @@ fn paintTargets(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, const w = pane.wrapAt(tab, row); if (w.line == line and w.at == 0) break; } else continue; - const x: u16 = @intCast(std.mem.count(u16, painted[0..n], &.{row})); - if (x >= tw) continue; - s.set(tx + x, body_y + row, bindings.keyName(c.key), style); - painted[n] = row; + rows[n] = row; + keys[n] = c.key; n += 1; } + // In place of the line's number (UX D10): its cells blank and the + // row's keys, as many as they hold, right-aligned to its last digit, + // bold in the key ink on the gutter's ground. With no numbers, over + // the line's first cells on the selection's ground. Never a shift. + const ink = p.chromeTheme().roles.key; + const number = File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1; + const cells = @min(if (number > 0) number else tw, tw); + for (rows[0..n], 0..) |row, i| { + if (std.mem.indexOfScalar(u16, rows[0..i], row) != null) continue; + const y = body_y + row; + const shown = @min(std.mem.count(u16, rows[0..n], &.{row}), cells); + if (number > 0) for (0..cells) |x| { + s.at(tx + @as(u16, @intCast(x)), y).text[0] = ' '; + s.at(tx + @as(u16, @intCast(x)), y).len = 1; + }; + var x: u16 = if (number > 0) @intCast(cells - shown) else 0; + for (rows[i..n], keys[i..n]) |other, key| { + if (other != row) continue; + if (x >= (if (number > 0) cells else shown)) break; + const bg = if (number > 0) s.at(tx + x, y).style.bg else pardes.Color{ .rgb = p.theme().sel_bg }; + s.set(tx + x, y, bindings.keyName(key), .{ .fg = .{ .rgb = ink }, .bg = bg, .bold = true }); + x += 1; + } + } } /// A hover's answer as ghost lines under the line it was asked at (SPEC @@ -1170,7 +1236,7 @@ test "a click under a message's detail lands on the row drawn there, a click on try std.testing.expectEqualStrings("one\ntwo\nthree\nfour\nfive\nsix\n", pane.file.?.content); } -test "a motion root's level paints its targets in the gutter: `]` the next paragraph's p, `g` g and e; `m` browsed on `i` tints the braces' inside; nothing at rest" { +test "a motion root's level paints its targets in place of their lines' numbers, right-aligned to the last digit, bold in the key ink: `]` the next paragraph's p and an unsaved edit's g, which `]g` goes to; `g` g and e; `m` browsed on `i` tints the braces' inside; nothing at rest (UX D10)" { const Key = pardes.Key; const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 20 }); defer p.deinit(); @@ -1182,34 +1248,59 @@ test "a motion root's level paints its targets in the gutter: `]` the next parag p.clock.now_ns = 5 * std.time.ns_per_s; p.clock.stepped_ns = p.clock.now_ns; pane.focus = .body; - pane.body.cur_row = 1; - pane.body.cur_col = 4; + // An edit on line 6, not saved; the caret back on line 2. + for ("5jAx") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + p.update(.{ .key = .{ .cp = Key.escape } }); + for ("4k") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); p.sync(); var s = try p.render(a); const body = s.region(.body, id).?.rect; const gutter = body.x; - const at_rest = [_][]const u8{ s.at(gutter, body.y).grapheme(), s.at(gutter, body.y + 4).grapheme(), s.at(gutter, body.y + 6).grapheme() }; - for (at_rest) |g| try std.testing.expect(!std.mem.eql(u8, g, "p") and !std.mem.eql(u8, g, "g") and !std.mem.eql(u8, g, "e")); + // The number's cells: the gutter less its blank. + const number = panes.File.gutterWidth(pane) - 1; + const Field = struct { + /// Row `y`'s number field, its blanks trimmed. + fn of(sf: *pardes.Surface, x: u16, y: u16, n: u16, buf: *[16]u8) []const u8 { + var w = std.Io.Writer.fixed(buf); + for (0..n) |i| w.writeAll(sf.at(x + @as(u16, @intCast(i)), y).grapheme()) catch {}; + return std.mem.trim(u8, w.buffered(), " "); + } + }; + var buf: [16]u8 = undefined; + try std.testing.expectEqualStrings("5", Field.of(s, gutter, body.y + 4, number, &buf)); const plain_bg = s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 1).style.bg; - // `]`: nothing until its level shows; then `p` on the next paragraph. + // `]`: nothing until its level shows; then `p` on the next paragraph's + // number, `g` on the edit's, the digits gone. p.update(.{ .key = .{ .cp = ']' } }); p.advance(p.clock.now_ns); _ = frame.reset(.retain_capacity); s = try p.render(a); - try std.testing.expect(!std.mem.eql(u8, s.at(gutter, body.y + 4).grapheme(), "p")); + try std.testing.expectEqualStrings("5", Field.of(s, gutter, body.y + 4, number, &buf)); p.clock.now_ns = p.view().tag_state.due_ns; _ = frame.reset(.retain_capacity); s = try p.render(a); - try std.testing.expectEqualStrings("p", s.at(gutter, body.y + 4).grapheme()); - p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqualStrings("p", Field.of(s, gutter, body.y + 4, number, &buf)); + const key = s.at(gutter + number - 1, body.y + 4); + try std.testing.expectEqualStrings("p", key.grapheme()); + try std.testing.expectEqual(pardes.Color{ .rgb = p.chromeTheme().roles.key }, key.style.fg); + try std.testing.expect(key.style.bold); + try std.testing.expect(std.mem.indexOfScalar(u8, Field.of(s, gutter, body.y + 5, number, &buf), 'g') != null); + try std.testing.expect(std.mem.indexOfAny(u8, Field.of(s, gutter, body.y + 5, number, &buf), "0123456789") == null); + // The text never shifts. + try std.testing.expectEqualStrings("t", s.at(gutter + panes.File.gutterWidth(pane) + 4, body.y + 5).grapheme()); + // `]g` goes where its target is. + p.update(.{ .key = .{ .cp = 'g', .text = "g" } }); + try std.testing.expectEqual(@as(i32, 5), pane.body.cur_row); + for ("4k") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); // `g`: its first and last lines. p.update(.{ .key = .{ .cp = 'g' } }); p.advance(p.clock.now_ns); p.clock.now_ns = p.view().tag_state.due_ns; _ = frame.reset(.retain_capacity); s = try p.render(a); - try std.testing.expectEqualStrings("g", s.at(gutter, body.y).grapheme()); - try std.testing.expectEqualStrings("e", s.at(gutter, body.y + 6).grapheme()); + try std.testing.expect(std.mem.indexOfScalar(u8, Field.of(s, gutter, body.y, number, &buf), 'g') != null); + try std.testing.expect(std.mem.indexOfScalar(u8, Field.of(s, gutter, body.y + 6, number, &buf), 'e') != null); p.update(.{ .key = .{ .cp = Key.escape } }); // `m` unrolled, its cursor on `i`: the inside of the braces around the // caret tinted. @@ -1224,5 +1315,5 @@ test "a motion root's level paints its targets in the gutter: `]` the next parag p.update(.{ .key = .{ .cp = Key.escape } }); _ = frame.reset(.retain_capacity); s = try p.render(a); - try std.testing.expectEqualStrings(at_rest[1], s.at(gutter, body.y + 4).grapheme()); + try std.testing.expectEqualStrings("5", Field.of(s, gutter, body.y + 4, number, &buf)); } |
