diff options
Diffstat (limited to 'core/body_layer.zig')
| -rw-r--r-- | core/body_layer.zig | 86 |
1 files changed, 66 insertions, 20 deletions
diff --git a/core/body_layer.zig b/core/body_layer.zig index a182f218..6ce2b6be 100644 --- a/core/body_layer.zig +++ b/core/body_layer.zig @@ -706,10 +706,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; @@ -721,12 +720,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 @@ -1155,7 +1176,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(); @@ -1167,34 +1188,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. @@ -1209,5 +1255,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)); } |
