diff options
Diffstat (limited to 'src/term')
| -rw-r--r-- | src/term/TerminalImages.zig | 39 | ||||
| -rw-r--r-- | src/term/terminal.zig | 763 |
2 files changed, 279 insertions, 523 deletions
diff --git a/src/term/TerminalImages.zig b/src/term/TerminalImages.zig index ab7aca25..3847be4c 100644 --- a/src/term/TerminalImages.zig +++ b/src/term/TerminalImages.zig @@ -161,37 +161,35 @@ fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) ghostty_vt.sys.DecodeErr /// columns the body shows, from `tx`. pub fn draw( ti: *TerminalImages, - p: *pardes.Pardes, - s: *pardes.Surface, + gpa: std.mem.Allocator, vt: *ghostty_vt.Terminal, - pane_id: u8, - serial: u32, - tx: u16, - body_y: u16, - cols: u16, + where: plain.term.ImagesAt, rows_of: []const ?u16, + out: *std.ArrayList(plain.Placement), + frame: []plain.Cell, + frame_cols: u16, ) void { var conv = ti.converted.valueIterator(); while (conv.next()) |c| c.used = false; - defer ti.prune(p.gpa); + defer ti.prune(gpa); const screen = vt.screens.active; const storage = &screen.kitty_images; // Animations step on the core's clock, as this frame shows them; the // shown frame is `renderData()` (a new `generation` each step, which // the caches key on). - const now_ms = p.clock.stepped_ns / std.time.ns_per_ms; + const now_ms = where.now_ms; ti.animation_due_ms = if (storage.animationTick(vt.io(), now_ms)) |delay| now_ms +| delay else null; const pages = &screen.pages; const top = pages.getTopLeft(.viewport); const bottom = pages.getBottomRight(.viewport) orelse return; - const cw: u32 = @max(1, p.view().cell_pixels.w); - const ch: u32 = @max(1, p.view().cell_pixels.h); - const ctx: Ctx = .{ .ti = ti, .p = p, .pane = pane_id, .serial = serial, .tx = tx, .body_y = body_y, .cols = cols, .rows_of = rows_of, .cw = cw, .ch = ch }; + const cw: u32 = @max(1, where.cell_w); + const ch: u32 = @max(1, where.cell_h); + const ctx: Ctx = .{ .ti = ti, .gpa = gpa, .out = out, .pane = where.pane, .serial = where.serial, .tx = where.tx, .body_y = where.body_y, .cols = where.cols, .rows_of = rows_of, .cw = cw, .ch = ch }; // Handed on lowest z first (the storage's order is a hash map's), the // order a shell draws them in; equal z keeps the order they came in. - const first = s.placements.items.len; - defer std.sort.insertion(plain.Placement, s.placements.items[first..], {}, struct { + const first = out.items.len; + defer std.sort.insertion(plain.Placement, out.items[first..], {}, struct { fn lessThan(_: void, a: plain.Placement, b: plain.Placement) bool { return a.z < b.z; } @@ -206,8 +204,8 @@ pub fn draw( if (v >= rows_of.len) continue; const row = rows_of[v] orelse continue; for (@as(usize, at.viewport.x)..@as(usize, at.viewport.x) + run.width) |x| { - if (x >= cols) break; - const cell = s.at(tx + @as(u16, @intCast(x)), body_y + row); + if (x >= where.cols) break; + const cell = &frame[@as(usize, where.body_y + row) * frame_cols + where.tx + x]; cell.text[0] = ' '; cell.len = 1; } @@ -268,7 +266,8 @@ pub fn draw( const Ctx = struct { ti: *TerminalImages, - p: *pardes.Pardes, + gpa: std.mem.Allocator, + out: *std.ArrayList(plain.Placement), pane: u8, serial: u32, tx: u16, @@ -286,7 +285,7 @@ const Ctx = struct { const x0 = @max(dst.x, 0); const x1 = @min(dst.x + dst.w, @as(i64, ctx.cols) * cw); if (x1 <= x0) return; - const rgba = ctx.ti.pixelsOf(ctx.p.gpa, img) orelse return; + const rgba = ctx.ti.pixelsOf(ctx.gpa, img) orelse return; const first_row = @divFloor(@max(dst.y, 0), ch); const last_row = @min(@divFloor(dst.y + dst.h - 1, ch), @as(i64, @intCast(ctx.rows_of.len)) - 1); var v = first_row; @@ -304,7 +303,7 @@ const Ctx = struct { }; const c0: u16 = @intCast(@divFloor(x0, cw)); const c1: u16 = @intCast(@divFloor(x1 + cw - 1, cw)); - _ = ctx.p.appendPlacement(.{ + ctx.out.append(ctx.gpa, .{ .pane = ctx.pane, .raster = .of(rgba, img.width, img.height), .src = piece, @@ -318,7 +317,7 @@ const Ctx = struct { .key = plain.Placement.keyOf(ctx.serial, img.id, @truncate(img.generation)), .z = z, .art = .{ .glyph = ctx.ti.petscii }, - }); + }) catch {}; } } }; diff --git a/src/term/terminal.zig b/src/term/terminal.zig index d11d1074..320ab6ad 100644 --- a/src/term/terminal.zig +++ b/src/term/terminal.zig @@ -13,12 +13,19 @@ const tagline = @import("../tagline.zig"); const Key = plain.Key; const Pane = panes.Pane; const Term = @import("../Term.zig"); -const BodyWalk = @import("../body_layer.zig").BodyWalk; +const ansi = @import("../ansi.zig"); const screen_state = @import("screen_state.zig"); const EditText = panes.EditText; const File = panes.File; const ghostty_vt = @import("ghostty-vt"); +const Ink = plain.term.Ink; +const FilterPalette = plain.term.FilterPalette; +const Filter = plain.term.Filter; +const Painted = plain.term.Painted; +const PaintedRow = plain.term.PaintedRow; +const ImagesAt = plain.term.ImagesAt; +const PromptCut = plain.term.PromptCut; const EditBuffer = panes.Text.EditBuffer; const Snapshot = panes.Text.Snapshot; @@ -367,56 +374,22 @@ pub const State = struct { const GColor = ghostty_vt.color; -const FilterPaletteKey = struct { - bg: GColor.RGB, - fg: GColor.RGB, - base: [16]GColor.RGB, -}; - -pub const FilterPalette = LivePalette; - -const LivePalette = struct { - key: ?FilterPaletteKey = null, - colors: GColor.Palette = GColor.default, - - fn get(self: *LivePalette, theme: *const pardes.Theme) *const GColor.Palette { - const bg = asGhostRgb(theme.bg orelse theme.tag_bg); - const fg = asGhostRgb(theme.fg orelse theme.tag_fg); - var base: [16]GColor.RGB = undefined; - if (theme.palette) |palette| { - for (&base, palette) |*dst, src| dst.* = asGhostRgb(src); - } else { - const synthesized = [16][3]u8{ - theme.bg orelse theme.tag_bg, - theme.kw, - theme.str, - theme.num, - theme.box, - theme.sel_bg, - theme.comment, - theme.fg orelse theme.tag_fg, - theme.lineno, - theme.kw, - theme.str, - theme.num, - theme.scroll_thumb, - theme.sel_fg, - theme.tag_fg, - theme.fg orelse theme.tag_fg, - }; - for (&base, synthesized) |*dst, src| dst.* = asGhostRgb(src); - } - - const wanted: FilterPaletteKey = .{ .bg = bg, .fg = fg, .base = base }; - if (self.key) |old| if (std.meta.eql(old, wanted)) return &self.colors; +// Projected colors below this contrast use the theme's more legible anchor. +const min_contrast: f64 = 1.5; - var seed = GColor.default; - for (base, 0..) |rgb, i| seed[i] = rgb; - self.colors = GColor.generate256Color(seed, .empty, bg, fg, false); - self.key = wanted; - return &self.colors; - } -}; +/// The theme's 256 colours as the filter maps onto them (ghostty's +/// `generate256Color` over the theme's sixteen), made again only when the +/// theme's are not the ones `cache` was made of. +fn filterPalette(cache: *FilterPalette, ink: *const Ink) *const [256][3]u8 { + const wanted: FilterPalette.Key = .{ .bg = ink.bg orelse ink.tag_bg, .fg = ink.fg orelse ink.tag_fg, .base = ink.base }; + if (cache.key) |old| if (std.meta.eql(old, wanted)) return &cache.colors; + var seed = GColor.default; + for (ink.base, 0..) |rgb, i| seed[i] = asGhostRgb(rgb); + const colors = GColor.generate256Color(seed, .empty, asGhostRgb(wanted.bg), asGhostRgb(wanted.fg), false); + for (&cache.colors, colors) |*dst, src| dst.* = asTriple(src); + cache.key = wanted; + return &cache.colors; +} fn asGhostRgb(rgb: [3]u8) GColor.RGB { return .{ .r = rgb[0], .g = rgb[1], .b = rgb[2] }; @@ -449,6 +422,7 @@ pub fn grid(state: ?*const State) Term.Grid { .cursor_y = @intCast(screen.cursor.y), .cursor_visible = s.vt.modes.get(.cursor_visible), .view = if (screen.pages.viewport == .active) null else sb.offset, + .cols = screen.pages.cols, }; } @@ -1054,16 +1028,6 @@ pub fn promptMoves(state: *State, row: i32, col: i32) ?Term.Moves { /// What LEAVING raw tty mode does to one prompt row, decided from its cells /// alone: OSC 133 prompt cells are hidden in normal mode, what was typed at /// them stays. -const PromptCut = union(enum) { - /// show the row exactly as ghostty dumped it - keep, - /// show nothing at all - blank, - /// drop this many leading COLUMNS — the prompt — and keep the rest, which - /// is what was typed at it - cut: usize, -}; - fn promptCut(pin: ghostty_vt.Pin) PromptCut { const cells = pin.cells(.all); var cols: usize = 0; @@ -1128,6 +1092,31 @@ fn dumpedBytes(pin: ghostty_vt.Pin, cell: *const ghostty_vt.Cell) usize { return n; } +/// Its rows `first` and the `count` after it (from the top of its +/// history) as text, a prompt's own cells cut off the rows it starts: +/// `arena`'s. An edit copy's rows as they were (src/ansi.zig). +pub fn editRows(state: *State, arena: std.mem.Allocator, first: usize, count: usize) ![]const []const u8 { + const screen = state.vt.screens.active; + const tl = screen.pages.pin(.{ .screen = .{ .y = @intCast(first) } }) orelse return error.NoRow; + const br = screen.pages.pin(.{ .screen = .{ + .x = screen.pages.cols - 1, + .y = @intCast(first + count - 1), + } }) orelse return error.NoRow; + var output: std.Io.Writer.Allocating = .init(arena); + try screen.dumpString(&output.writer, .{ .tl = tl, .br = br, .unwrap = false }); + const shell = try arena.alloc([]const u8, count); + var raw = std.mem.splitScalar(u8, output.written(), '\n'); + var pins = tl.rowIterator(.right_down, br); + for (shell) |*row| { + const text = raw.next() orelse ""; + row.* = if (pins.next()) |pin| + if (pin.rowAndCell().row.semantic_prompt != .none) promptRow(pin, text) else text + else + text; + } + return shell; +} + /// A step read its rows (its history too): a replica replaying the step /// must hold them (`State.read`). pub fn markRead(state: *State) void { @@ -1203,247 +1192,43 @@ pub fn viewport(state: *State, arena: std.mem.Allocator, cut: bool) ![]const []c return vp; } -// Match surviving edit-buffer rows to their original terminal styles. -const EditAnchors = struct { - /// the buffer's own text, walked in order: a line the VIEWPORT skipped still - /// consumes the row it came from, so the lines below it stay aligned - text: []const u8 = &.{}, - at: usize = 0, - shell: []const []const u8 = &.{}, - /// the covered span, absolute grid rows, as `[first, end)` - first: usize = 0, - end: usize = 0, - lines: usize = 0, - /// the line `at` names, and the first row still unclaimed - idx: usize = 0, - cursor: usize = 0, - budget: usize = 0, - active: bool = false, - - fn init(p: *Pardes, pane: *Pane, o: EditBuffer) EditAnchors { - if (o.rows <= 0 or o.row < 0) return .{}; - const first: usize = @intCast(o.row); - const screen = pane.terminal.?.vt.screens.active; - const total = screen.pages.scrollbar().total; - if (first >= total) return .{}; - const covered: usize = @intCast(o.rows); - const count = @min(covered, total - first); - const tl = screen.pages.pin(.{ .screen = .{ .y = @intCast(first) } }) orelse return .{}; - const br = screen.pages.pin(.{ .screen = .{ - .x = screen.pages.cols - 1, - .y = @intCast(first + count - 1), - } }) orelse return .{}; - const arena = p.scratch.allocator(); - var output: std.Io.Writer.Allocating = .init(arena); - screen.dumpString(&output.writer, .{ .tl = tl, .br = br, .unwrap = false }) catch return .{}; - const shell = arena.alloc([]const u8, count) catch return .{}; - var raw = std.mem.splitScalar(u8, output.written(), '\n'); - var pins = tl.rowIterator(.right_down, br); - for (shell) |*row| { - const text = raw.next() orelse ""; - row.* = if (pins.next()) |pin| - if (pin.rowAndCell().row.semantic_prompt != .none) promptRow(pin, text) else text - else - text; - } - const lines = std.mem.count(u8, o.text, "\n") + 1; - return .{ - .text = o.text, - .shell = shell, - .first = first, - .end = first + count, - .lines = lines, - .cursor = first, - .budget = covered + 4 * lines, - .active = true, - }; - } - - fn shellRow(a: *EditAnchors, idx: usize) ?Anchor { - if (!a.active or idx >= a.lines) return null; - var found: ?Anchor = null; - while (a.idx <= idx) : (a.idx += 1) found = a.claim(a.nextLine() orelse return null); - return found; - } - - fn nextLine(a: *EditAnchors) ?[]const u8 { - if (a.at > a.text.len) return null; - const rest = a.text[a.at..]; - if (std.mem.indexOfScalar(u8, rest, '\n')) |n| { - a.at += n + 1; - return rest[0..n]; - } - // The last line has no terminator; one past the end ends the walk. - a.at = a.text.len + 1; - return rest; - } - - /// Where this line still stands over the grid, if anywhere. - fn claim(a: *EditAnchors, line: []const u8) ?Anchor { - const end = if (line.len == 0) @min(a.cursor + 1, a.end) else a.end; - var k = a.cursor; - while (k < end) : (k += 1) { - if (a.budget == 0) return null; - a.budget -= 1; - if (!std.mem.eql(u8, line, a.shell[k - a.first])) continue; - a.cursor = k + 1; - return .{ .row = @intCast(k) }; - } - if (a.cursor >= a.end) return null; - const shell = a.shell[a.cursor - a.first]; - var p: usize = 0; - while (p < line.len and p < shell.len and line[p] == shell[p]) p += 1; - var s: usize = 0; - const room = @min(line.len, shell.len) - p; - while (s < room and line[line.len - 1 - s] == shell[shell.len - 1 - s]) s += 1; - if (p + s == 0) return null; - if (line.len != p + s and shell.len - (p + s) > shell.len / 2) return null; - const row = a.cursor; - if (s > 0 or p >= shell.len) a.cursor += 1; - return .{ .row = @intCast(row), .prefix = p, .suffix = s, .shell_len = shell.len }; - } -}; - -const Anchor = struct { - /// the row, absolute while it comes from `EditAnchors`, viewport once - /// `recolorAnsi` has subtracted the walk's offset - row: i32, - prefix: usize = std.math.maxInt(usize), - suffix: usize = 0, - /// the row's own dumped length — what the suffix is measured from on the - /// GRID side, where the edit may have changed the byte count - shell_len: usize = 0, - - fn whole(an: Anchor) bool { - return an.prefix == std.math.maxInt(usize); - } -}; - -pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, body: []const u8) void { - // No emulator, no ANSI cells: the whole pass — and the 256-colour theme - // projection behind it — is compiled out. - if (pane.terminal == null) return; - const body_y = p.bodyTop(pane, r); - var filtered_storage: FilteredColors = undefined; - const filtered: ?*FilteredColors = if (pane.tty_filter) blk: { - const tz_filter = tracy.zone(@src(), "filterInit"); - defer tz_filter.end(); - filtered_storage = FilteredColors.init(p, &pane.terminal.?.vt); - break :blk &filtered_storage; +/// Its view's rows as cells, for the core's paint (Term.paint, the core's +/// src/ansi.zig): `rows.len` rows of `cols` cells into `cells`, each its +/// style in the theme's colours (through the filter when `filtered`), the +/// bytes it spells in the view's text and its width; each row whether a +/// prompt starts it, and how much of it that is. +pub fn paint(state: *State, ink: *const Ink, palette: *FilterPalette, filtered: bool, cols: u16, cells: []Painted, rows: []PaintedRow) void { + var filter_storage: Filter = undefined; + const filter: ?*Filter = if (filtered) blk: { + filter_storage = filterOf(ink, palette, &state.vt); + break :blk &filter_storage; } else null; // DECSCNM, read once: the filtered palette folds it in itself, the raw // path needs it per cell. - const scnm = pane.terminal.?.vt.modes.get(.reverse_colors); - const pages = &pane.terminal.?.vt.screens.active.pages; - const vp_rows: i32 = @intCast(grid(pane.terminal).len); - // Which buffer lines the user has not actually changed, so a row swallowed - // by a growing buffer keeps the colour it still stands over. - var anchors: ?EditAnchors = null; - var walk: BodyWalk = .init(pane); - var lines = std.mem.splitScalar(u8, body, '\n'); - var vr: u16 = 0; - while (walk.next()) |row| : (vr += 1) { - if (vr >= body_h) break; - // Before any early exit below, or the lines fall out of step with rows. - const text = lines.next() orelse ""; - const anchor: Anchor = switch (row) { - .edit => |e| blk: { - if (anchors == null) anchors = .init(p, pane, pane.ovl.?); - var an = anchors.?.shellRow(e.idx) orelse continue; - an.row -= walk.goff; - break :blk an; - }, - .grid => |v| .{ .row = v }, - }; - const vi = anchor.row; - if (vi < 0 or vi >= vp_rows) continue; + const scnm = state.vt.modes.get(.reverse_colors); + const pages = &state.vt.screens.active.pages; + for (rows, 0..) |*row, vi| { + row.* = .{ .prompt = null, .placeholder = false, .len = 0 }; const row_pin = pages.pin(.{ .viewport = .{ .y = @intCast(vi) } }) orelse continue; - const cut: u16 = if (pane.body.mode == .tty) 0 else cut: { - if (row_pin.rowAndCell().row.semantic_prompt == .none) break :cut 0; - break :cut switch (promptCut(row_pin)) { - .keep => 0, - // Blanked end to end: the row shows nothing of the grid, so - // projecting the prompt's own colours onto it would be a lie. - .blank => continue, - .cut => |n| std.math.cast(u16, n) orelse continue, - }; - }; - if (cut > 0 and text.len == 0) continue; - var at: usize = 0; - var sc: u16 = 0; - var gc: u16 = cut; - var sb: usize = 0; - const mine_from = @min(anchor.prefix, text.len); - const mine_to = text.len - @min(anchor.suffix, text.len); - var crossed = false; - while (at < text.len and sc < tw) { - const stop = modal.nextGrapheme(text, at); - if (stop <= at) break; - const span: u16 = if (sc + 1 < tw and s.at(tx + sc + 1, body_y + vr).len == 0) 2 else 1; - if (at >= mine_from and at < mine_to) { - sc += span; - at = stop; - continue; - } - if (at >= mine_to and !crossed) { - crossed = true; - const upto = anchor.shell_len - @min(anchor.suffix, anchor.shell_len); - while (sb < upto) { - const ci = cellAt(pages, row_pin, gc) orelse break; - sb += dumpedBytes(row_pin, ci.cell); - gc = std.math.add(u16, gc, 1) catch break; - } - } - const want = stop - at; - // Consume every cell that contributed to this grapheme. A cluster - // ghostty split across several cells is still ONE printed glyph. - var covered: usize = 0; - var style: ?plain.CellStyle = null; - while (covered < want) { - const ci = cellAt(pages, row_pin, gc) orelse break; - if (style == null and ci.cell.wide != .spacer_tail and ci.cell.wide != .spacer_head) - style = cellStyle(p, ci, filtered, scnm); - covered += dumpedBytes(row_pin, ci.cell); - gc = std.math.add(u16, gc, 1) catch break; - // A spacer contributes no bytes; without this the loop would - // spin on a row that ends in one. - if (covered == 0 and gc >= pane.cols) break; - } - while (cellAt(pages, row_pin, gc)) |t| { - if (t.cell.wide != .spacer_tail) break; - gc = std.math.add(u16, gc, 1) catch break; - } - if (style) |st| for (0..span) |k| { - const cell = s.at(tx + sc + @as(u16, @intCast(k)), body_y + vr); - if (cell.default and filtered == null) continue; - cell.default = false; - cell.style = st; - }; - sb += covered; - sc += span; - at = stop; - } - var tail: ?plain.CellStyle = null; - if (anchor.whole() or anchor.suffix > 0) { - while (sc < tw) { - const ci = cellAt(pages, row_pin, gc) orelse break; - gc = std.math.add(u16, gc, 1) catch break; - if (ci.cell.wide == .spacer_tail) continue; - const cell = s.at(tx + sc, body_y + vr); - sc += 1; - const style = cellStyle(p, ci, filtered, scnm); - tail = style; - if (cell.default and filtered == null) continue; - cell.default = false; - cell.style = style; - } - if (tail) |style| while (sc < tw) : (sc += 1) { - const cell = s.at(tx + sc, body_y + vr); - if (cell.default and filtered == null) continue; - cell.default = false; - cell.style = style; + const flags = row_pin.rowAndCell().row; + if (flags.semantic_prompt != .none) row.prompt = promptCut(row_pin); + row.placeholder = flags.kitty_virtual_placeholder; + const out = cells[vi * cols ..][0..cols]; + var x: u16 = 0; + while (x < cols) : (x += 1) { + const ci = cellAt(pages, row_pin, x) orelse break; + out[x] = .{ + .style = cellStyle(ink, ci, filter, scnm), + .bytes = @intCast(@min(dumpedBytes(row_pin, ci.cell), std.math.maxInt(u16))), + .wide = switch (ci.cell.wide) { + .narrow => .narrow, + .wide => .wide, + .spacer_tail => .spacer_tail, + .spacer_head => .spacer_head, + }, }; } + row.len = x; } } @@ -1464,11 +1249,11 @@ comptime { if (@backingInt(@field(ghostty_vt.sgr.Attribute.Underline, if (std.mem.eql(u8, name, "off")) "none" else name)) != i) @compileError("a cell's underline styles are not SGR's"); } -fn cellStyle(p: *Pardes, ci: ghostty_vt.PageList.Cell, filtered: ?*FilteredColors, scnm: bool) plain.CellStyle { +fn cellStyle(ink: *const Ink, ci: ghostty_vt.PageList.Cell, filtered: ?*Filter, scnm: bool) plain.CellStyle { const style = ci.style(); var cs: plain.CellStyle = .{ - .fg = if (filtered) |colors| colors.fg(style) else ghostColor(p, style.fg_color, scnm), - .bg = if (filtered) |colors| colors.bg(style, ci.cell) else ghostColor(p, style.bg_color, !scnm), + .fg = if (filtered) |colors| filterFg(colors, style) else ghostColor(ink, style.fg_color, scnm), + .bg = if (filtered) |colors| filterBg(colors, style, ci.cell) else ghostColor(ink, style.bg_color, !scnm), .bold = style.flags.bold, .dim = style.flags.faint, .italic = style.flags.italic, @@ -1479,7 +1264,7 @@ fn cellStyle(p: *Pardes, ci: ghostty_vt.PageList.Cell, filtered: ?*FilteredColor .ul = @fromBackingInt(@intCast(@backingInt(style.flags.underline))), }; if (filtered == null) switch (ci.cell.content_tag) { - .bg_color_palette => cs.bg = palColor(p, ci.cell.content.color_palette.data), + .bg_color_palette => cs.bg = ink.palColor(ci.cell.content.color_palette.data), .bg_color_rgb => { const rgb = ci.cell.content.color_rgb; cs.bg = .{ .rgb = .{ rgb.r, rgb.g, rgb.b } }; @@ -1493,148 +1278,134 @@ fn asPardesColor(rgb: GColor.RGB) plain.Color { return .{ .rgb = .{ rgb.r, rgb.g, rgb.b } }; } -/// A program's colours mapped to the theme (Filter): a terminal's cells, or -/// a +Pager's spans (`vt` null, the program's palette xterm's own). -pub const FilteredColors = struct { - source: GColor.Palette, - target: *const GColor.Palette, - theme_bg: GColor.RGB, - theme_fg: GColor.RGB, - dynamic_bg: ?GColor.RGB, - dynamic_fg: ?GColor.RGB, - default_bg: GColor.RGB, - /// What a foreground too near `default_bg` becomes instead. - fallback_fg: GColor.RGB, - default_bg_luminance: f64, - fg_for_palette: [256]plain.Color = undefined, - bg_for_palette: [256]plain.Color = undefined, - /// The two answers for a cell that names no colour of its own. - fg_default: plain.Color = undefined, - bg_default: plain.Color = undefined, - cache_rgb: [256]GColor.RGB = undefined, - cache_key: [256]u8 = undefined, - cache_valid: [256]bool = @splat(false), - - pub fn init(p: *Pardes, vt: ?*const ghostty_vt.Terminal) FilteredColors { - var source = GColor.default; - if (vt) |t| { - for (&source, 0..) |*rgb, i| - rgb.* = t.colorForXterm(.{ .palette = @intCast(i) }) orelse rgb.*; - } - const theme = p.theme(); - var theme_bg = asGhostRgb(theme.bg orelse theme.tag_bg); - var theme_fg = asGhostRgb(theme.fg orelse theme.tag_fg); - var dynamic_bg = if (vt) |t| t.colorForXterm(.{ .dynamic = .background }) else null; - var dynamic_fg = if (vt) |t| t.colorForXterm(.{ .dynamic = .foreground }) else null; - if (vt != null and vt.?.modes.get(.reverse_colors)) { - std.mem.swap(GColor.RGB, &theme_bg, &theme_fg); - std.mem.swap(?GColor.RGB, &dynamic_bg, &dynamic_fg); - } - var self: FilteredColors = .{ - .source = source, - .target = p.tty_filter_palette.get(theme), - .theme_bg = theme_bg, - .theme_fg = theme_fg, - .dynamic_bg = dynamic_bg, - .dynamic_fg = dynamic_fg, - .default_bg = theme_bg, - .fallback_fg = theme_fg, - .default_bg_luminance = luminanceOf(theme_bg), - }; - if (dynamic_bg) |rgb| self.default_bg = self.keyedRgb(rgb); - self.default_bg_luminance = luminanceOf(self.default_bg); - if (self.theme_bg.contrast(self.default_bg) > self.theme_fg.contrast(self.default_bg)) - self.fallback_fg = self.theme_bg; +fn asTriple(rgb: GColor.RGB) [3]u8 { + return .{ rgb.r, rgb.g, rgb.b }; +} - self.fg_default = asPardesColor(self.legible( - if (self.dynamic_fg) |rgb| self.keyedRgb(rgb) else self.theme_fg, - )); - self.bg_default = asPardesColor(self.default_bg); - for (&self.source, 0..) |current, i| { - const idx: u8 = @intCast(i); - const mapped = self.paletteRgb(idx, current); - self.fg_for_palette[idx] = asPardesColor(self.legible(mapped)); - self.bg_for_palette[idx] = asPardesColor(mapped); - } - return self; +/// A program's colours mapped to the theme (Term.Filter): for a terminal's +/// cells (`vt`), or a +Pager's spans (`vt` null, the program's palette +/// xterm's own). +pub fn filterOf(ink: *const Ink, palette: *FilterPalette, vt: ?*const ghostty_vt.Terminal) Filter { + var source = GColor.default; + if (vt) |t| { + for (&source, 0..) |*rgb, i| + rgb.* = t.colorForXterm(.{ .palette = @intCast(i) }) orelse rgb.*; } - - fn fg(self: *FilteredColors, style: ghostty_vt.Style) plain.Color { - return switch (style.fg_color) { - .none => self.fg_default, - .palette => |idx| self.fg_for_palette[idx], - .rgb => asPardesColor(self.legible(self.keyedRgb(style.fg(.{ - .default = self.dynamic_fg orelse self.theme_fg, - .palette = &self.source, - .bold = null, - })))), - }; + var theme_bg = asGhostRgb(ink.bg orelse ink.tag_bg); + var theme_fg = asGhostRgb(ink.fg orelse ink.tag_fg); + var dynamic_bg = if (vt) |t| t.colorForXterm(.{ .dynamic = .background }) else null; + var dynamic_fg = if (vt) |t| t.colorForXterm(.{ .dynamic = .foreground }) else null; + if (vt != null and vt.?.modes.get(.reverse_colors)) { + std.mem.swap(GColor.RGB, &theme_bg, &theme_fg); + std.mem.swap(?GColor.RGB, &dynamic_bg, &dynamic_fg); } + var self: Filter = .{ + .target = filterPalette(palette, ink), + .theme_bg = asTriple(theme_bg), + .theme_fg = asTriple(theme_fg), + .default_bg = asTriple(theme_bg), + .fallback_fg = asTriple(theme_fg), + .default_bg_luminance = luminanceOf(theme_bg), + .fg_for_palette = undefined, + .bg_for_palette = undefined, + .fg_default = undefined, + .bg_default = undefined, + }; + var default_bg = theme_bg; + if (dynamic_bg) |rgb| default_bg = keyedRgb(&self, rgb); + self.default_bg = asTriple(default_bg); + self.default_bg_luminance = luminanceOf(default_bg); + if (theme_bg.contrast(default_bg) > theme_fg.contrast(default_bg)) + self.fallback_fg = asTriple(theme_bg); - fn bg(self: *FilteredColors, style: ghostty_vt.Style, cell: *const ghostty_vt.Cell) plain.Color { - switch (cell.content_tag) { - .bg_color_palette => return self.bg_for_palette[cell.content.color_palette.data], - .bg_color_rgb => {}, - else => switch (style.bg_color) { - .none => return self.bg_default, - .palette => |idx| return self.bg_for_palette[idx], - .rgb => {}, - }, - } - // Truecolour, from either the cell or its style. - return asPardesColor(self.keyedRgb(style.bg(cell, &self.source).?)); + self.fg_default = asPardesColor(legible(&self, if (dynamic_fg) |rgb| keyedRgb(&self, rgb) else theme_fg)); + self.bg_default = asPardesColor(default_bg); + for (&source, 0..) |current, i| { + const idx: u8 = @intCast(i); + const mapped = paletteRgb(&self, idx, current); + self.fg_for_palette[idx] = asPardesColor(legible(&self, mapped)); + self.bg_for_palette[idx] = asPardesColor(mapped); } + return self; +} - /// A +Pager span's colour (Term.page), as `fg` and `bg` map a cell's. - pub fn spanColor(self: *FilteredColors, color: plain.Color, is_bg: bool) plain.Color { - return switch (color) { - .default => color, - .index => |idx| if (is_bg) self.bg_for_palette[idx] else self.fg_for_palette[idx], - .rgb => |rgb| blk: { - const keyed = self.keyedRgb(asGhostRgb(rgb)); - break :blk asPardesColor(if (is_bg) keyed else self.legible(keyed)); - }, - }; - } +/// The filter a +Pager's spans are drawn through (Term.filter). +pub fn pagerFilter(ink: *const Ink, palette: *FilterPalette) Filter { + return filterOf(ink, palette, null); +} - fn legible(self: *const FilteredColors, rgb: GColor.RGB) GColor.RGB { - if (contrastOf(luminanceOf(rgb), self.default_bg_luminance) >= - config.tty_filter_min_contrast) return rgb; - return self.fallback_fg; - } +fn filterFg(self: *Filter, style: ghostty_vt.Style) plain.Color { + return switch (style.fg_color) { + .none => self.fg_default, + .palette => |idx| self.fg_for_palette[idx], + .rgb => |rgb| asPardesColor(legible(self, keyedRgb(self, rgb))), + }; +} - fn paletteRgb(self: *FilteredColors, idx: u8, current: GColor.RGB) GColor.RGB { - if (current.eql(GColor.default[idx])) return self.target[idx]; - return self.keyedRgb(current); +fn filterBg(self: *Filter, style: ghostty_vt.Style, cell: *const ghostty_vt.Cell) plain.Color { + switch (cell.content_tag) { + .bg_color_palette => return self.bg_for_palette[cell.content.color_palette.data], + .bg_color_rgb => {}, + else => switch (style.bg_color) { + .none => return self.bg_default, + .palette => |idx| return self.bg_for_palette[idx], + .rgb => {}, + }, } + // Truecolour, from either the cell or its style. + return asPardesColor(keyedRgb(self, style.bg(cell, &GColor.default).?)); +} - fn keyedRgb(self: *FilteredColors, rgb: GColor.RGB) GColor.RGB { - return self.target[self.nearestKey(rgb)]; - } +/// A +Pager span's colour (Term.page), as `filterFg` and `filterBg` map a +/// cell's. +pub fn spanColor(self: *Filter, color: plain.Color, is_bg: bool) plain.Color { + return switch (color) { + .default => color, + .index => |idx| if (is_bg) self.bg_for_palette[idx] else self.fg_for_palette[idx], + .rgb => |rgb| blk: { + const keyed = keyedRgb(self, asGhostRgb(rgb)); + break :blk asPardesColor(if (is_bg) keyed else legible(self, keyed)); + }, + }; +} - fn nearestKey(self: *FilteredColors, rgb: GColor.RGB) u8 { - const rgb24 = (@as(u32, rgb.r) << 16) | (@as(u32, rgb.g) << 8) | rgb.b; - const slot: u8 = @truncate((rgb24 *% 0x9e3779b1) >> 24); - if (self.cache_valid[slot] and self.cache_rgb[slot].eql(rgb)) - return self.cache_key[slot]; +fn legible(self: *const Filter, rgb: GColor.RGB) GColor.RGB { + if (contrastOf(luminanceOf(rgb), self.default_bg_luminance) >= + min_contrast) return rgb; + return asGhostRgb(self.fallback_fg); +} - var best: u8 = 0; - var best_distance: u32 = std.math.maxInt(u32); - for (GColor.default, 0..) |candidate, i| { - const distance = colorDistance(rgb, candidate); - // Strict comparison makes duplicate-colour ties stable at the - // lowest canonical xterm key. - if (distance < best_distance) { - best_distance = distance; - best = @intCast(i); - } +fn paletteRgb(self: *Filter, idx: u8, current: GColor.RGB) GColor.RGB { + if (current.eql(GColor.default[idx])) return asGhostRgb(self.target[idx]); + return keyedRgb(self, current); +} + +fn keyedRgb(self: *Filter, rgb: GColor.RGB) GColor.RGB { + return asGhostRgb(self.target[nearestKey(self, rgb)]); +} + +fn nearestKey(self: *Filter, rgb: GColor.RGB) u8 { + const rgb24 = (@as(u32, rgb.r) << 16) | (@as(u32, rgb.g) << 8) | rgb.b; + const slot: u8 = @truncate((rgb24 *% 0x9e3779b1) >> 24); + if (self.cache_valid[slot] and asGhostRgb(self.cache_rgb[slot]).eql(rgb)) + return self.cache_key[slot]; + + var best: u8 = 0; + var best_distance: u32 = std.math.maxInt(u32); + for (GColor.default, 0..) |candidate, i| { + const distance = colorDistance(rgb, candidate); + // Strict comparison makes duplicate-colour ties stable at the + // lowest canonical xterm key. + if (distance < best_distance) { + best_distance = distance; + best = @intCast(i); } - self.cache_rgb[slot] = rgb; - self.cache_key[slot] = best; - self.cache_valid[slot] = true; - return best; } -}; + self.cache_rgb[slot] = asTriple(rgb); + self.cache_key[slot] = best; + self.cache_valid[slot] = true; + return best; +} const channel_luminance: [256]f64 = blk: { @setEvalBranchQuota(20000); @@ -1669,23 +1440,17 @@ fn colorDistance(a: GColor.RGB, b: GColor.RGB) u32 { return @intCast(dr * dr + dg * dg + db * db); } -pub fn palColor(p: *Pardes, idx: u8) plain.Color { - if (p.theme().palette) |pal| if (idx < 16) return .{ .rgb = pal[idx] }; - return .{ .index = idx }; -} - -fn ghostColor(p: *Pardes, color: ghostty_vt.Style.Color, is_bg: bool) plain.Color { +fn ghostColor(ink: *const Ink, color: ghostty_vt.Style.Color, is_bg: bool) plain.Color { return switch (color) { .none => blk: { - const t = if (is_bg) p.theme().bg else p.theme().fg; + const t = if (is_bg) ink.bg else ink.fg; break :blk if (t) |c| .{ .rgb = c } else .default; }, - .palette => |idx| palColor(p, idx), + .palette => |idx| ink.palColor(idx), .rgb => |rgb| .{ .rgb = .{ rgb.r, rgb.g, rgb.b } }, }; } - /// When the pane's next kitty animation frame is due (ms on the core's /// clock), as its last drawn frame left it; null when nothing plays. pub fn due(state: *const State) ?u64 { @@ -1715,39 +1480,30 @@ pub fn hasImages(maybe: ?*const State) bool { return state.vt.screens.active.kitty_images.images.count() > 0; } -/// Draw the pane's kitty graphics images over its body rows (after the -/// rows and their colours: placeholder cells are blanked here). -pub fn drawImages(p: *Pardes, s: *Surface, pane: *Pane, id: usize, tx: u16, tw: u16, body_y: u16, body_h: u16) void { +/// Its kitty graphics over the body's rows, as placements appended to +/// `out` (after the rows and their colours: placeholder cells are blanked +/// in `frame`, `frame_cols` wide). `at.rows_of` maps its view's rows to +/// the body's; a prompt row outside tty mode is cut or hidden, its columns +/// not the grid's, and shows none. +pub fn images(state: *State, gpa: std.mem.Allocator, at: ImagesAt, out: *std.ArrayList(plain.Placement), frame: []plain.Cell, frame_cols: u16) void { if (comptime !images_enabled) return; - const state = pane.terminal orelse return; const screen = state.vt.screens.active; - cellPixels(state, p.view().cell_pixels.w, p.view().cell_pixels.h); - // A body taller than this draws its images on the rows that fit. var rows_of: [1024]?u16 = undefined; - const vp_rows: usize = @min(grid(state).len, rows_of.len); - @memset(rows_of[0..vp_rows], null); + const vp_rows = @min(at.rows_of.len, rows_of.len); var any_placeholder = false; - var walk: BodyWalk = .init(pane); - var vr: u16 = 0; - while (walk.next()) |row| : (vr += 1) { - if (vr >= body_h) break; - const vi = switch (row) { - .grid => |v| v, - .edit => continue, - }; - if (vi < 0 or vi >= vp_rows) continue; + for (rows_of[0..vp_rows], at.rows_of[0..vp_rows], 0..) |*slot, body_row, vi| { + slot.* = null; + const vr = body_row orelse continue; const row_pin = screen.pages.pin(.{ .viewport = .{ .y = @intCast(vi) } }) orelse continue; const flags = row_pin.rowAndCell().row; - // A prompt row outside tty mode is cut or hidden: its columns are - // not the grid's. - if (pane.body.mode != .tty and flags.semantic_prompt != .none) continue; + if (!at.tty_mode and flags.semantic_prompt != .none) continue; any_placeholder = any_placeholder or flags.kitty_virtual_placeholder; - rows_of[@intCast(vi)] = vr; + slot.* = vr; } // Nothing to draw, no placeholder to blank and nothing cached to let // go: the common case costs the rows' flags. if (!any_placeholder and screen.kitty_images.placements.count() == 0 and state.images.converted.count() == 0) return; - state.images.draw(p, s, &state.vt, @intCast(id), pane.serial, tx, body_y, @min(tw, pane.cols), rows_of[0..vp_rows]); + state.images.draw(gpa, &state.vt, at, rows_of[0..vp_rows], out, frame, frame_cols); } /// Every byte the core wrote to each pane's pty since the last call, `\n` @@ -2068,11 +1824,11 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu _ = frame.reset(.retain_capacity); const filtered = try p.render(frame.allocator()); var expected_cache: FilterPalette = .{}; - const expected = expected_cache.get(p.theme()); - try testing.expectEqual(asPardesColor(expected[1]), filtered.at(tx, body_y).style.fg); - try testing.expectEqual(asPardesColor(expected[196]), filtered.at(tx + 1, body_y).style.fg); + const expected = filterPalette(&expected_cache, &ansi.inkOf(p.theme())); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[1])), filtered.at(tx, body_y).style.fg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), filtered.at(tx + 1, body_y).style.fg); try testing.expectEqual(filtered.at(tx + 1, body_y).style.fg, filtered.at(tx + 2, body_y).style.fg); - try testing.expectEqual(asPardesColor(expected[25]), filtered.at(tx + 3, body_y).style.bg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[25])), filtered.at(tx + 3, body_y).style.bg); try testing.expectEqual(filtered.at(tx + 3, body_y).style.bg, filtered.at(tx + 4, body_y).style.bg); const attrs = filtered.at(tx + 5, body_y).style; @@ -2087,8 +1843,8 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu const erased = pane.terminal.?.vt.screens.active.pages.getCell(.{ .viewport = .{ .x = 6, .y = 0 } }).?; try testing.expectEqual(.bg_color_palette, erased.cell.content_tag); - try testing.expectEqual(asPardesColor(expected[25]), filtered.at(tx + 6, body_y).style.bg); - try testing.expectEqual(asPardesColor(expected[2]), filtered.at(tx, body_y + 1).style.fg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[25])), filtered.at(tx + 6, body_y).style.bg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[2])), filtered.at(tx, body_y + 1).style.fg); try testing.expectEqual(filtered.at(tx, body_y + 1).style, filtered.at(tx + 1, body_y + 1).style); // A filtered terminal never delegates either colour to a backend palette, // including cells which were empty/default before the pass. @@ -2113,7 +1869,7 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu try testing.expect(osc_red.eql(pane.terminal.?.vt.colorForXterm(.{ .palette = 1 }).?)); _ = frame.reset(.retain_capacity); const osc_palette = try p.render(frame.allocator()); - try testing.expectEqual(asPardesColor(expected[196]), osc_palette.at(tx, body_y).style.fg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), osc_palette.at(tx, body_y).style.fg); // Dynamic default foreground/background colours key every default cell, // including the otherwise blank end of the row. @@ -2126,19 +1882,19 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu _ = frame.reset(.retain_capacity); const dynamic = try p.render(frame.allocator()); const edge = dynamic.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; - try testing.expectEqual(asPardesColor(expected[196]), edge.fg); - try testing.expectEqual(asPardesColor(expected[59]), edge.bg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), edge.fg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[59])), edge.bg); const explicit_before_reverse = dynamic.at(tx, body_y).style.fg; - try testing.expectEqual(asPardesColor(expected[196]), explicit_before_reverse); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), explicit_before_reverse); p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5h" } }); _ = frame.reset(.retain_capacity); const reversed = try p.render(frame.allocator()); const reversed_blank = reversed.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; - try testing.expectEqual(asPardesColor(expected[59]), reversed_blank.fg); - try testing.expectEqual(asPardesColor(expected[196]), reversed_blank.bg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[59])), reversed_blank.fg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), reversed_blank.bg); const reversed_explicit = reversed.at(tx, body_y).style; - try testing.expectEqual(asPardesColor(expected[196]), reversed_explicit.bg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), reversed_explicit.bg); try testing.expectEqual(plain.Color{ .rgb = p.theme().bg.? }, reversed_explicit.fg); try testing.expect(!std.meta.eql(reversed_explicit.fg, reversed_explicit.bg)); @@ -2146,8 +1902,8 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu _ = frame.reset(.retain_capacity); const unreversed = try p.render(frame.allocator()); const unreversed_blank = unreversed.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; - try testing.expectEqual(asPardesColor(expected[196]), unreversed_blank.fg); - try testing.expectEqual(asPardesColor(expected[59]), unreversed_blank.bg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[196])), unreversed_blank.fg); + try testing.expectEqual(asPardesColor(asGhostRgb(expected[59])), unreversed_blank.bg); // The cache is keyed by values, not a theme name. Replacing a custom // theme in place immediately recolours already-rendered indexed cells. @@ -2171,9 +1927,9 @@ test "terminal Filter keeps extended keys dark-to-light on a light theme" { defer p.deinit(); try std.testing.expectEqualStrings("acme", p.theme().name); var cache: FilterPalette = .{}; - const palette = cache.get(p.theme()); - try std.testing.expect(palette[16].eql(asGhostRgb(p.theme().fg.?))); - try std.testing.expect(palette[231].eql(asGhostRgb(p.theme().bg.?))); + const palette = filterPalette(&cache, &ansi.inkOf(p.theme())); + try std.testing.expect(asGhostRgb(palette[16]).eql(asGhostRgb(p.theme().fg.?))); + try std.testing.expect(asGhostRgb(palette[231]).eql(asGhostRgb(p.theme().bg.?))); } test "terminal Filter preserves exact palette-null light theme default roles" { @@ -2222,23 +1978,23 @@ test "terminal Filter maps the default roles before it maps anything else" { // the null-page fallback that only the legacy dark theme exercises. for (0..9) |t| { p.settings.theme = @intCast(t); - const stage_one = FilteredColors.init(p, &pane.terminal.?.vt); + const stage_one = filterOf(&ansi.inkOf(p.theme()), &p.tty_filter_palette, &pane.terminal.?.vt); // `dark` declares no background of its own, which is exactly why the // resolver reads the tag colours as the fallback rather than `.?`. const theme = p.theme(); - try testing.expect(stage_one.theme_bg.eql(asGhostRgb(theme.bg orelse theme.tag_bg))); - try testing.expect(stage_one.theme_fg.eql(asGhostRgb(theme.fg orelse theme.tag_fg))); + try testing.expect(asGhostRgb(stage_one.theme_bg).eql(asGhostRgb(theme.bg orelse theme.tag_bg))); + try testing.expect(asGhostRgb(stage_one.theme_fg).eql(asGhostRgb(theme.fg orelse theme.tag_fg))); // With no OSC 11 in play the mapped page IS that anchor, and the // fallback is the other one: a background never contrasts with itself. - try testing.expect(stage_one.default_bg.eql(stage_one.theme_bg)); - try testing.expect(stage_one.fallback_fg.eql(stage_one.theme_fg)); + try testing.expect(asGhostRgb(stage_one.default_bg).eql(asGhostRgb(stage_one.theme_bg))); + try testing.expect(asGhostRgb(stage_one.fallback_fg).eql(asGhostRgb(stage_one.theme_fg))); } p.settings.theme = 0; p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]11;#5f5f5f\x1b\\" } }); - var moved = FilteredColors.init(p, &pane.terminal.?.vt); - try testing.expect(!moved.default_bg.eql(moved.theme_bg)); - try testing.expect(moved.default_bg.eql(moved.keyedRgb(.{ .r = 0x5f, .g = 0x5f, .b = 0x5f }))); + var moved = filterOf(&ansi.inkOf(p.theme()), &p.tty_filter_palette, &pane.terminal.?.vt); + try testing.expect(!asGhostRgb(moved.default_bg).eql(asGhostRgb(moved.theme_bg))); + try testing.expect(asGhostRgb(moved.default_bg).eql(keyedRgb(&moved, .{ .r = 0x5f, .g = 0x5f, .b = 0x5f }))); } test "terminal Filter refuses a foreground that would collapse onto the page" { @@ -2261,16 +2017,16 @@ test "terminal Filter refuses a foreground that would collapse onto the page" { for (0..9) |t| { p.settings.theme = @intCast(t); - var fc = FilteredColors.init(p, &pane.terminal.?.vt); - const page = asPardesColor(fc.default_bg); - const rescued = asPardesColor(fc.fallback_fg); + var fc = filterOf(&ansi.inkOf(p.theme()), &p.tty_filter_palette, &pane.terminal.?.vt); + const page = asPardesColor(asGhostRgb(fc.default_bg)); + const rescued = asPardesColor(asGhostRgb(fc.fallback_fg)); _ = frame.reset(.retain_capacity); const g = try p.render(frame.allocator()); // The colour each of the three would have been given with no floor. - const raw_white = fc.keyedRgb(.{ .r = 255, .g = 255, .b = 255 }); - const raw_black = fc.paletteRgb(0, GColor.default[0]); - const raw_red = fc.paletteRgb(1, GColor.default[1]); + const raw_white = keyedRgb(&fc, .{ .r = 255, .g = 255, .b = 255 }); + const raw_black = paletteRgb(&fc, 0, GColor.default[0]); + const raw_red = paletteRgb(&fc, 1, GColor.default[1]); for ([_]struct { at: u16, raw: GColor.RGB }{ .{ .at = 0, .raw = raw_white }, @@ -2279,7 +2035,7 @@ test "terminal Filter refuses a foreground that would collapse onto the page" { }) |case| { const cell = g.at(tx + case.at, body_y).style; try testing.expectEqual(page, cell.bg); - if (case.raw.contrast(fc.default_bg) < config.tty_filter_min_contrast) { + if (case.raw.contrast(asGhostRgb(fc.default_bg)) < min_contrast) { // Refused: the projection's answer is not painted, the anchor is. try testing.expectEqual(rescued, cell.fg); try testing.expect(!std.meta.eql(cell.fg, cell.bg)); @@ -2289,18 +2045,18 @@ test "terminal Filter refuses a foreground that would collapse onto the page" { } // Either way a filtered cell delegates neither colour to a backend. switch (cell.fg) { - .rgb => |ink| try testing.expect(asGhostRgb(ink).contrast(fc.default_bg) >= - config.tty_filter_min_contrast), + .rgb => |ink| try testing.expect(asGhostRgb(ink).contrast(asGhostRgb(fc.default_bg)) >= + min_contrast), else => return error.FilteredForegroundWasNotRgb, } } // At least one of the three has to have been a real collapse, or this // theme proved nothing: white on the light theme, black on the dark. - try testing.expect(raw_white.contrast(fc.default_bg) < config.tty_filter_min_contrast or - raw_black.contrast(fc.default_bg) < config.tty_filter_min_contrast); + try testing.expect(raw_white.contrast(asGhostRgb(fc.default_bg)) < min_contrast or + raw_black.contrast(asGhostRgb(fc.default_bg)) < min_contrast); // A saturated red is never the page on any curated theme. - try testing.expect(raw_red.contrast(fc.default_bg) >= config.tty_filter_min_contrast); + try testing.expect(raw_red.contrast(asGhostRgb(fc.default_bg)) >= min_contrast); } } @@ -2313,20 +2069,21 @@ test "terminal Filter holds every projected foreground off the page" { for (0..9) |t| { p.settings.theme = @intCast(t); - var fc = FilteredColors.init(p, &pane.terminal.?.vt); + var fc = filterOf(&ansi.inkOf(p.theme()), &p.tty_filter_palette, &pane.terminal.?.vt); var refused: usize = 0; - for (fc.target, 0..) |projected, slot| { - const ink = fc.legible(projected); - try testing.expect(ink.contrast(fc.default_bg) >= config.tty_filter_min_contrast); + for (fc.target, 0..) |projected_triple, slot| { + const projected = asGhostRgb(projected_triple); + const ink = legible(&fc, projected); + try testing.expect(ink.contrast(asGhostRgb(fc.default_bg)) >= min_contrast); if (!ink.eql(projected)) { refused += 1; - try testing.expect(ink.eql(fc.fallback_fg)); + try testing.expect(ink.eql(asGhostRgb(fc.fallback_fg))); // Only ever refused for being too near the page. - try testing.expect(projected.contrast(fc.default_bg) < config.tty_filter_min_contrast); + try testing.expect(projected.contrast(asGhostRgb(fc.default_bg)) < min_contrast); } // The key a background asks for is handed back untouched, including // the one whose value is the page itself. - try testing.expect(fc.keyedRgb(GColor.default[slot]).eql(fc.target[fc.nearestKey(GColor.default[slot])])); + try testing.expect(keyedRgb(&fc, GColor.default[slot]).eql(asGhostRgb(fc.target[nearestKey(&fc, GColor.default[slot])]))); } // Every curated theme owns at least one collapsing key — that is why // the floor exists — and the floor must not be flattening the palette. |
