╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2076 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2077 try self.child.input(allocator, key, root_focus); 2078 } 2079 2080 pub fn clearGrid(self: *AnsiBackground) void { 2081 if (self.grid) |*grid| { 2082 grid.deinit(); 2083 self.grid = null; 2084 } 2085 self.child.clearGrid(); 2086 self.art.clearGrid(); 2087 } 2088 2089 pub fn getGrid(self: AnsiBackground) ?Grid { 2090 return self.grid orelse self.child.getGrid(); 2091 } 2092 2093 pub fn getFocus(self: *AnsiBackground) *Focus { 2094 return self.child.getFocus(); 2095 } 2096 2097 // which top corner the art hugs. 2098 const ArtAnchor = enum { top_left, top_right }; 2099 2100 // a foreground cell counts as blank if it's empty or a bare space without styling 2101 fn cellIsBlank(cell: Grid.Cell) bool { 2102 if (cell.rune) |rune| { 2103 return std.mem.eql(u8, rune, " ") and 2104 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2105 } 2106 return true; 2107 } 2108 2109 // the art is dimmed to this fraction of its brightness so foreground text 2110 // stays legible over it 2111 const art_brightness = 45; // percent 2112 2113 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2114 const c = color orelse return null; 2115 return .{ 2116 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2117 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2118 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2119 }; 2120 } 2121 2122 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2123 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2124 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2125 // snap such channels clear of that range so the backdrop doesn't blink or 2126 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2127 // actually render truecolor. 2128 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2129 const c = color orelse return null; 2130 const snap = struct { 2131 fn f(v: u8) u8 { 2132 return if (v >= 1 and v <= 9) 10 else v; 2133 } 2134 }.f; 2135 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2136 } 2137 2138 // composites ANSI art behind a foreground grid 2139 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2140 art.clearGrid(); 2141 try art.build(allocator, .{ 2142 .min_size = .{ .width = null, .height = null }, 2143 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2144 }, root_focus); 2145 2146 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2147 errdefer out.deinit(); 2148 2149 if (art.getGrid()) |art_grid| { 2150 const anchor_x = switch (anchor) { 2151 .top_left => 0, 2152 .top_right => foreground.size.width -| art_grid.size.width, 2153 }; 2154 for (0..art_grid.size.height) |y| { 2155 for (0..art_grid.size.width) |x| { 2156 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2157 if (src.rune == null) continue; 2158 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2159 const dst = &out.cells.items[idx]; 2160 // composite the dimmed art behind every cell so it shows 2161 // through the whole UI: a blank cell becomes the art, while a 2162 // cell with a glyph keeps the glyph but takes the dimmed art as 2163 // its background (replacing whatever background it had, which 2164 // would otherwise obscure the art). 2165 if (cellIsBlank(dst.*)) { 2166 dst.* = src; 2167 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2168 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2169 } else { 2170 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2171 } 2172 } 2173 } 2174 } 2175 return out; 2176 } 2177 }; 2178