diff options
| -rw-r--r-- | core/jj.zig | 143 | ||||
| -rw-r--r-- | core/surface.zig | 3 | ||||
| -rw-r--r-- | dev/gui/gui.zig | 35 |
3 files changed, 150 insertions, 31 deletions
diff --git a/core/jj.zig b/core/jj.zig index 59e0e21d..96009d74 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -369,9 +369,9 @@ pub const Hunk = struct { /// A row a view shows that is not a line of the text (PARDES.md "Changes on /// the source view": a ghost row): a line a pin took away whole, drawn where /// it stood, red across; or a pin's description (`id title author age`), -/// after its last line, in its lane's colour. No number, the cursor steps -/// over it, search and selection do not see it; a Look on it goes to its -/// line in the revision it came from (`lookGhost`). A line a pin CHANGED +/// after its last line, its lane an arrow to it. No number, the cursor +/// steps over it, search and selection do not see it; a Look on it goes to +/// its line in the revision it came from (`lookGhost`). A line a pin CHANGED /// is a ghost row too: its old text right above the line, the words that /// went in red, the line with the words that came in green. pub const Ghost = struct { @@ -1425,10 +1425,12 @@ fn ghostLanes(ch: *const Changes, ghosts: []const Ghost, gi: usize) [levels]PinI } /// A ghost row's cells: a line taken away whole red across, its text in -/// the line numbers' grey (dimmer than the text's ink); a description in -/// its lane's colour, its author and age grey; `said`, the row's text as -/// the body shows it (a description `fitted`). -fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, said: []const u8, marks: []const [2]u32, colours: []const [3]u8, x: u16, y: u16, w: u16) void { +/// the line numbers' grey (dimmer than the text's ink); a description's id +/// and title in its lane's colour `lane`, or with one change pinned (no one +/// colour a lane) its id in the theme's keyword ink; its author and age +/// grey; `said`, the row's text as the body shows it (a description +/// `fitted`). +fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, said: []const u8, marks: []const [2]u32, lane: ?[3]u8, x: u16, y: u16, w: u16) void { const th = p.theme(); switch (g.kind) { .deleted => s.tint(x, y, w, pardes.colors.Grounds.of(th).del.row, th.lineno), @@ -1445,9 +1447,13 @@ fn inkGhost(p: *Pardes, s: *pardes.Surface, g: Ghost, said: []const u8, marks: [ // `id title author age`: the last two words grey. var grey = said.len; for (0..2) |_| grey = std.mem.lastIndexOfScalar(u8, said[0..grey], ' ') orelse 0; - const lane_cells = File.displayWidth(p.settings.tab, said[0..grey]); + const id_cells = if (lane != null) File.displayWidth(p.settings.tab, said) else std.mem.indexOfScalar(u8, said, ' ') orelse said.len; + const title_cells = File.displayWidth(p.settings.tab, said[0..grey]); const cells = @min(w, File.displayWidth(p.settings.tab, said)); - for (0..cells) |c| s.at(x + @as(u16, @intCast(c)), y).style.fg = .{ .rgb = if (c < lane_cells) colours[g.pin % colours.len] else th.lineno }; + for (0..cells) |c| { + if (c >= id_cells and c < title_cells) continue; + s.at(x + @as(u16, @intCast(c)), y).style.fg = .{ .rgb = if (c >= title_cells) th.lineno else lane orelse th.kw }; + } }, } } @@ -1539,6 +1545,26 @@ fn nest(lines: []Line, npins: usize) void { } } +/// A diff's two inks (one change pinned, `paintLanes`): its lane's colour +/// says the kind of the row it stands beside (the user's rulings of +/// 2026-11-23), `added` beside a row it wrote (a line added whole, a changed +/// line's new text), `taken` beside a row it took away (a line taken away, a +/// changed line's old text). +pub const Inks = struct { added: [3]u8, taken: [3]u8 }; + +/// The diff's own two colours: the theme's green and its red (the grounds' +/// and the changed words' hues), each pushed toward the ink until it reads +/// 3:1 on the page (or nearly as the ink does, where the ink reads less). +pub fn laneInks(th: *const pardes.Theme) Inks { + var inks: [2][3]u8 = .{ if (th.palette) |c| c[2] else .{ 0x2e, 0xa0, 0x43 }, themeRed(th) }; + const page = th.bg orelse th.tag_bg; + for (&inks) |*ink| { + var step: usize = 0; + while (step < 8 and pardes.colors.contrast(ink.*, page) < 3) : (step += 1) ink.* = pardes.colors.mixOklab(ink.*, th.fg orelse th.tag_fg, 0.4); + } + return .{ .added = inks[0], .taken = inks[1] }; +} + /// The lanes' colours, a pin's the `pin % len`th, oldest first: the /// theme's accents but the two that mean written and taken away (its green /// and red), in hue order from the top, each a hue `hue_apart` or more from @@ -1666,6 +1692,9 @@ fn paintLanes(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u var buf: [8][3]u8 = undefined; const colours = laneColours(p.theme(), &buf); if (colours.len == 0) return; + // One change pinned is a diff: its lane in the diff's own two colours, + // by the row's kind; several, a colour a change, to tell them apart. + const one: ?Inks = if (ch.pins.len == 1) laneInks(p.theme()) else null; const gutter = File.gutterWidth(pane); const cells = File.laneCells(pane); if (gutter >= tw or cells == 0) return; @@ -1677,13 +1706,18 @@ fn paintLanes(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u var vr: u16 = pane.context_rows; while (vr < body_h) : (vr += 1) { const y = body_y + vr; + var taken = false; + // On a description row, its change's lane: an arrow to it. + var points: PinIndex = .none; const lanes: [levels]PinIndex = if (vr < pane.wrap_n and pane.wrap_col[vr] < 0) ghost: { const gi: usize = @intCast(-1 - pane.wrap_col[vr]); if (gi >= pane.ghosts.len) continue; const g = pane.ghosts[gi]; const said = pane.ghost_text[g.text..][0..g.len]; var fit: [row_max]u8 = undefined; - inkGhost(p, s, g, if (g.kind == .description) fitted(p.settings.tab, said, File.bodyWidth(pane), &fit) else said, ch.marks, colours, tx + gutter, y, tw - gutter); + inkGhost(p, s, g, if (g.kind == .description) fitted(p.settings.tab, said, File.bodyWidth(pane), &fit) else said, ch.marks, if (one == null) colours[g.pin % colours.len] else null, tx + gutter, y, tw - gutter); + if (g.kind == .description) points = @enumFromInt(g.pin); + taken = g.kind != .description or takenAbove(pane.ghosts, gi); break :ghost ghostLanes(ch, pane.ghosts, gi); } else line: { const row: i32 = if (pane.wrap_n == 0) scroll + @as(i32, vr) else if (vr < pane.wrap_n) pane.wrap_line[vr] else break; @@ -1695,20 +1729,37 @@ fn paintLanes(p: *Pardes, s: *pardes.Surface, pane: *Pane, r: pardes.Rect, tx: u }; for (lanes, 0..) |lane, level| { if (lane == .none) break; - const rgb = colours[@intFromEnum(lane) % colours.len]; + const arrow = lane == points; + const rgb = if (one) |d| (if (taken) d.taken else d.added) else colours[@intFromEnum(lane) % colours.len]; if (windowed) { // A body a pixel shell draws as a layer is painted on a // temporary surface first: its lanes are not drawn yet. - if (s == &p.surface) s.lanes.append(p.gpa, .{ .x = first, .y = y, .level = @intCast(level), .rgb = rgb }) catch {}; + if (s == &p.surface) s.lanes.append(p.gpa, .{ .x = first, .y = y, .level = @intCast(level), .rgb = rgb, .arrow = if (arrow) @intCast(cells) else 0 }) catch {}; } else if (level < cells) { var style = s.at(first + @as(u16, @intCast(level)), y).style; style.fg = .{ .rgb = rgb }; - s.set(first + @as(u16, @intCast(level)), y, "\u{258e}", style); + // An arrow: the lane, a shaft, a head at the text (`▎─→`); + // from the last level, a bent one. + const at: u16 = @intCast(level); + s.set(first + at, y, if (!arrow) "\u{258e}" else if (at + 1 == cells) "\u{21b3}" else "\u{258e}", style); + if (arrow) for (at + 1..cells) |c| s.set(first + @as(u16, @intCast(c)), y, if (c + 1 == cells) "\u{2192}" else "\u{2500}", style); } } } } +/// Whether the row above description `ghosts[gi]` is one its lanes stand +/// beside as taken away: a ghost row of a line at its place, not a line. +fn takenAbove(ghosts: []const Ghost, gi: usize) bool { + var j = gi; + while (j > 0) { + j -= 1; + if (ghosts[j].at != ghosts[gi].at) return false; + if (ghosts[j].kind != .description) return true; + } + return false; +} + // ---- tests ---- const testing = std.testing; @@ -2143,6 +2194,19 @@ test "Changes draws a lane a pinned change after the line number, nested ones a } } +test "a diff's two lane inks in every theme: its green and its red, never one colour, each read on the page" { + for (&pardes.colors.themes) |*th| { + const inks = laneInks(th); + errdefer std.debug.print("{s}: added {any} taken {any}\n", .{ th.name, inks.added, inks.taken }); + try testing.expect(!std.meta.eql(inks.added, inks.taken)); + const page = th.bg orelse th.tag_bg; + // 3:1, or as near the theme's own ink's as it reads (hex_poison's). + const floor = @min(3, pardes.colors.contrast(th.fg orelse th.tag_fg, page)) - 0.1; + try testing.expect(pardes.colors.contrast(inks.added, page) >= floor); + try testing.expect(pardes.colors.contrast(inks.taken, page) >= floor); + } +} + /// A function `fn` wrote, two of its lines `cut` took away whole, and one /// `mid` rewrote as two (one changed, one added whole). const Ghosts = struct { @@ -2183,7 +2247,7 @@ fn saidOf(arena: std.mem.Allocator, ch: *const Changes) ![3][]const u8 { return said; } -test "Changes draws what the text has not where it stood, as ghost rows: a line a change took away whole red across, each change's description after its last line in its lane's colour; no number, their lanes, the rows below moved down in this view alone" { +test "Changes draws what the text has not where it stood, as ghost rows: a line a change took away whole red across, each change's description after its last line in its lane's colour, its lane an arrow to it; no number, their lanes; one change pinned, its lane green beside what it wrote and red beside what it took away; the rows below moved down in this view alone" { const gpa = testing.allocator; var repo = try Ghosts.repo() orelse return error.SkipZigTest; defer repo.deinit(gpa); @@ -2220,19 +2284,26 @@ test "Changes draws what the text has not where it stood, as ghost rows: a line const lanes = [_][]const u8{ "0", "0", "02", "02", "02", "02", "01", "01", "01", "0", "0", "0" }; const Ground = enum { none, add, del }; const ground = [_]Ground{ .add, .add, .none, .none, .add, .none, .del, .del, .none, .add, .add, .none }; - for (lanes, ground, 0..) |pins, gr, row| { + // On a description row the level its change's arrow leaves from. + const arrows = [_]?usize{ null, null, null, null, null, 1, null, null, 1, null, null, 0 }; + for (lanes, ground, arrows, 0..) |pins, gr, arrow, row| { const y = body.y + @as(u16, @intCast(row)); if (windowed) { var n: usize = 0; for (s.lanes.items) |lane| if (lane.y == y) { + try testing.expect(lane.level < pins.len); try testing.expectEqual(colours[pins[lane.level] - '0'], lane.rgb); + try testing.expectEqual(@as(u8, if (arrow == lane.level) @intCast(File.laneCells(pane)) else 0), lane.arrow); n += 1; }; try testing.expectEqual(pins.len, n); - } else for (pins, 0..) |pin, level| { + } else for (0..levels) |level| { + // `▎` a lane; an arrow: its lane, a shaft, a head (`▎─→`). const cell = s.at(first + @as(u16, @intCast(level)), y); - try testing.expectEqualStrings("\u{258e}", cell.grapheme()); - try testing.expectEqual(pardes.Color{ .rgb = colours[pin - '0'] }, cell.style.fg); + const glyph = if (level < pins.len) "\u{258e}" else if (arrow == null) " " else if (level + 1 == levels) "\u{2192}" else "\u{2500}"; + try testing.expectEqualStrings(glyph, cell.grapheme()); + const pin = if (level < pins.len) pins[level] - '0' else pins[arrow orelse 0] - '0'; + if (!std.mem.eql(u8, glyph, " ")) try testing.expectEqual(pardes.Color{ .rgb = colours[pin] }, cell.style.fg); } const cell = s.at(body.x + File.gutterWidth(pane) + 1, y); const bg: ?[3]u8 = switch (gr) { @@ -2247,8 +2318,10 @@ test "Changes draws what the text has not where it stood, as ghost rows: a line } // A description's id and title in its lane's colour; a line taken // away in the numbers' grey. - for ([_]u16{ 5, 8, 11 }, [_]usize{ 2, 1, 0 }) |row, pin| + for ([_]u16{ 5, 8, 11 }, [_]usize{ 2, 1, 0 }) |row, pin| { try testing.expectEqual(pardes.Color{ .rgb = colours[pin] }, s.at(body.x + File.gutterWidth(pane), body.y + row).style.fg); + try testing.expectEqual(pardes.Color{ .rgb = colours[pin] }, s.at(body.x + File.gutterWidth(pane) + 5, body.y + row).style.fg); + } try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(body.x + File.gutterWidth(pane) + 4, body.y + 6).style.fg); // The changed line: on its old text the word that went (`b`) red, // on it the word that came (`b2`) green; the rest of each neither. @@ -2262,6 +2335,31 @@ test "Changes draws what the text has not where it stood, as ghost rows: a line for (plain[0..6]) |row| try testing.expectEqualStrings(lines.next().?, Ghosts.text(pane, row)); p.toView(.at(0)); try testing.expectEqualStrings(said[2], Ghosts.text(pane, (try Ghosts.rows(p, pane, arena.allocator(), &buf))[5])); + // One change pinned, mid: a diff. Its rows `b();` (its old text), + // `b2();`, `x();`, its description: the lane red beside the first, + // green beside the lines, green into its arrow; its id the + // keyword's ink, its title the text's. + var one: [64]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&one, "Changes {s}", .{ch.pins[2].id}) }); + p.sync(); + const s1 = try p.render(arena.allocator()); + const top = s1.region(.body, id).?.rect.y; + const at = body.x + File.gutterWidth(pane) - File.laneCells(pane); + const inks = laneInks(p.theme()); + try testing.expect(!std.meta.eql(inks.added, inks.taken)); + for ([_]u16{ 2, 3, 4, 5 }, [_]bool{ true, false, false, false }) |row, red| { + const ink = if (red) inks.taken else inks.added; + if (windowed) { + var n: usize = 0; + for (s1.lanes.items) |lane| if (lane.y == top + row) { + try testing.expectEqual(ink, lane.rgb); + n += 1; + }; + try testing.expectEqual(@as(usize, 1), n); + } else try testing.expectEqual(pardes.Color{ .rgb = ink }, s1.at(at, top + row).style.fg); + } + try testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, s1.at(text_x, top + 5).style.fg); + try testing.expect(!std.meta.eql(pardes.Color{ .rgb = p.theme().kw }, s1.at(text_x + 5, top + 5).style.fg)); } } @@ -2385,14 +2483,13 @@ test "a description's ghost row keeps its author and age: in a pane narrower tha try testing.expect(File.displayWidth(p.settings.tab, row) <= room); try testing.expectStringStartsWith(row, try std.fmt.allocPrint(arena.allocator(), "{s} a title long", .{ch.pins[0].id[0..4]})); try testing.expectStringEndsWith(row, "\u{2026} t now"); - // Drawn: the id and the title in its lane's colour, the author and - // the age grey, at the row's end. + // Drawn: the id in the keyword's ink, the author and the age grey, + // at the row's end. p.sync(); const s = try p.render(arena.allocator()); const body = s.region(.body, id).?.rect; const x = body.x + File.gutterWidth(pane); - var cbuf: [8][3]u8 = undefined; - try testing.expectEqual(pardes.Color{ .rgb = laneColours(p.theme(), &cbuf)[0] }, s.at(x, body.y + 3).style.fg); + try testing.expectEqual(pardes.Color{ .rgb = p.theme().kw }, s.at(x, body.y + 3).style.fg); const age_x = x + @as(u16, @intCast(File.displayWidth(p.settings.tab, row))) - 1; try testing.expectEqualStrings("w", s.at(age_x, body.y + 3).grapheme()); try testing.expectEqual(pardes.Color{ .rgb = p.theme().lineno }, s.at(age_x, body.y + 3).style.fg); diff --git a/core/surface.zig b/core/surface.zig index 3452bc77..5b381958 100644 --- a/core/surface.zig +++ b/core/surface.zig @@ -155,6 +155,9 @@ pub const Lane = struct { y: u16, level: u8, rgb: [3]u8, + /// On its change's description row, the lane's end: an arrow to the + /// text, which stands this many cells from `x` (0: a strip). + arrow: u8 = 0, /// The levels a row shows at most: the ruled gutter fits three. pub const levels = 3; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 88ba023b..76af0ec8 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -5028,17 +5028,36 @@ fn laneDecor(g: *Gui, gpa: std.mem.Allocator, surface: *const pardes.Surface, la const LaneRide = struct { x0: u16, x1: u16, y0: u16, y1: u16, dy: f32, top: f32, bottom: f32 }; /// `lane`'s strip, `w` pixels wide, on grid row `row`, riding a scroll's -/// rows when given one. +/// rows when given one; on its description row an arrow to the text: the +/// strip down to the row's middle, a shaft along it, a head half a strip +/// short of the text. fn laneStrip(g: *Gui, gpa: std.mem.Allocator, lane: pardes.Lane, row: i32, ride: ?*const LaneRide, w: f32, layout: CellLayout, win_w: f32, win_h: f32) !void { const x0 = @round(@as(f32, @floatFromInt(lane.x)) * layout.w + w / 2 + @as(f32, @floatFromInt(lane.level)) * (w + w / 2)); - var y0 = @as(f32, @floatFromInt(row)) * layout.h; - var y1 = y0 + layout.h; - if (ride) |r| { - y0 = @max(y0 - r.dy, r.top); - y1 = @min(y1 - r.dy, r.bottom); - if (y1 <= y0) return; + const top = @as(f32, @floatFromInt(row)) * layout.h; + const Piece = struct { + fn add(gu: *Gui, alloc: std.mem.Allocator, rd: ?*const LaneRide, rgb: [3]u8, x_0: f32, y_0: f32, x_1: f32, y_1: f32, ww: f32, wh: f32) !void { + var a = y_0; + var b = y_1; + if (rd) |r| { + a = @max(a - r.dy, r.top); + b = @min(b - r.dy, r.bottom); + } + if (b > a and x_1 > x_0) try addDecor(gu, alloc, x_0, a, x_1, b, rgb, 1, ww, wh); + } + }; + if (lane.arrow == 0) return Piece.add(g, gpa, ride, lane.rgb, x0, top, x0 + w, top + layout.h, win_w, win_h); + const mid = @round(top + (layout.h - w) / 2); + const tip = @round(@as(f32, @floatFromInt(lane.x + lane.arrow)) * layout.w - w / 2); + const head = @round(w * 1.5); + try Piece.add(g, gpa, ride, lane.rgb, x0, top, x0 + w, mid + w, win_w, win_h); + try Piece.add(g, gpa, ride, lane.rgb, x0 + w, mid, tip - head, mid + w, win_w, win_h); + // The head: a pixel row each, narrowing to the tip. + var dy: f32 = -head; + while (dy < head) : (dy += 1) { + const y = mid + w / 2 + dy; + const len = @round(head * (1 - @abs(dy + 0.5) / head)); + try Piece.add(g, gpa, ride, lane.rgb, tip - head, @round(y), tip - head + len, @round(y) + 1, win_w, win_h); } - try addDecor(g, gpa, x0, y0, x0 + w, y1, lane.rgb, 1, win_w, win_h); } /// Each pane's rule between its tag and body, and its scroll rail: a page |
