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