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