╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2062 self.focus.destroy(allocator); 2063 if (self.grid) |*grid| { 2064 grid.deinit(); 2065 self.grid = null; 2066 } 2067 } 2068 2069 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2070 _ = root_focus; 2071 self.clearGrid(); 2072 2073 // parse into rows of cells, then copy into a rectangular grid 2074 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2075 defer { 2076 for (rows.items) |*row| row.deinit(allocator); 2077 rows.deinit(allocator); 2078 } 2079 var row: std.ArrayList(Grid.Cell) = .empty; 2080 errdefer row.deinit(allocator); 2081 2082 var style: Grid.Style = .{}; 2083 var width: usize = 0; 2084 const content = self.content; 2085 var i: usize = 0; 2086 while (i < content.len) { 2087 const byte = content[i]; 2088 if (byte == '\n') { 2089 width = @max(width, row.items.len); 2090 try rows.append(allocator, row); 2091 row = .empty; 2092 i += 1; 2093 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2094 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2095 var j = i + 2; 2096 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2097 if (j >= content.len) { 2098 i = content.len; // malformed trailing escape; stop 2099 } else { 2100 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2101 i = j + 1; 2102 } 2103 } else { 2104 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2105 const end = @min(content.len, i + len); 2106 const rune = content[i..end]; 2107 const transparent = end - i == 1 and rune[0] == ' ' and 2108 style.fg == null and style.bg == null and !style.inverted; 2109 try row.append(allocator, .{ 2110 .rune = if (transparent) null else rune, 2111 .style = style, 2112 }); 2113 i = end; 2114 } 2115 } 2116 // a trailing row with content but no closing newline 2117 if (row.items.len > 0) { 2118 width = @max(width, row.items.len); 2119 try rows.append(allocator, row); 2120 } else { 2121 row.deinit(allocator); 2122 } 2123 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2124 2125 const height = rows.items.len; 2126 if (width == 0 or height == 0) return; 2127 2128 const clamped_w = @min(width, constraint.max_size.width orelse width); 2129 const clamped_h = @min(height, constraint.max_size.height orelse height); 2130 2131 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2132 errdefer grid.deinit(); 2133 for (rows.items[0..clamped_h], 0..) |r, y| { 2134 const n = @min(r.items.len, clamped_w); 2135 for (r.items[0..n], 0..) |cell, x| { 2136 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2137 } 2138 } 2139 self.grid = grid; 2140 } 2141 2142 pub fn clearGrid(self: *AnsiArt) void { 2143 if (self.grid) |*grid| { 2144 grid.deinit(); 2145 self.grid = null; 2146 } 2147 } 2148 2149 pub fn getGrid(self: AnsiArt) ?Grid { 2150 return self.grid; 2151 } 2152 2153 pub fn getFocus(self: *AnsiArt) *Focus { 2154 return self.focus; 2155 } 2156 2157 fn applySgr(style: *Grid.Style, params: []const u8) void { 2158 var nums: [16]u32 = undefined; 2159 var n: usize = 0; 2160 var it = std.mem.splitScalar(u8, params, ';'); 2161 while (it.next()) |tok| { 2162 if (n >= nums.len) break; 2163 // an empty parameter (e.g. bare "\x1b[m") means 0 2164 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2165 n += 1; 2166 } 2167 if (n == 0) { 2168 style.* = .{}; 2169 return; 2170 } 2171 var i: usize = 0; 2172 while (i < n) : (i += 1) { 2173 switch (nums[i]) { 2174 0 => style.* = .{}, 2175 7 => style.inverted = true, 2176 27 => style.inverted = false, 2177 39 => style.fg = null, 2178 49 => style.bg = null, 2179 38, 48 => { 2180 // truecolor form: 38;2;r;g;b — anything else is ignored 2181 if (i + 4 < n and nums[i + 1] == 2) { 2182 const c = Grid.Color{ 2183 .r = @truncate(nums[i + 2]), 2184 .g = @truncate(nums[i + 3]), 2185 .b = @truncate(nums[i + 4]), 2186 }; 2187 if (nums[i] == 38) style.fg = c else style.bg = c; 2188 i += 4; 2189 } 2190 }, 2191 else => {}, 2192 } 2193 } 2194 } 2195 }; 2196 2197 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2198 pub const AnsiBackground = struct { 2199 grid: ?Grid, 2200 child: *Widget, 2201 art: AnsiArt, 2202 session: *Session, 2203 2204 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2205 var cw = child_widget; 2206 const child = allocator.create(Widget) catch |e| { 2207 cw.deinit(allocator); 2208 return e; 2209 }; 2210 child.* = cw; 2211 errdefer { 2212 child.deinit(allocator); 2213 allocator.destroy(child); 2214 } 2215 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2216 } 2217 2218 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2219 if (self.grid) |*grid| grid.deinit(); 2220 self.art.deinit(allocator); 2221 self.child.deinit(allocator); 2222 allocator.destroy(self.child); 2223 } 2224 2225 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2226 self.clearGrid(); 2227 2228 // make the wrapped page fill the whole available area 2229 try self.child.build(allocator, .{ 2230 .min_size = .{ 2231 .width = constraint.max_size.width orelse constraint.min_size.width, 2232 .height = constraint.max_size.height orelse constraint.min_size.height, 2233 }, 2234 .max_size = constraint.max_size, 2235 }, root_focus); 2236 2237 if (self.session.data.enable_ansi) { 2238 if (self.child.getGrid()) |fg| { 2239 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2240 } 2241 } 2242 } 2243 2244 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2245 try self.child.input(allocator, key, root_focus); 2246 } 2247 2248 pub fn clearGrid(self: *AnsiBackground) void { 2249 if (self.grid) |*grid| { 2250 grid.deinit(); 2251 self.grid = null; 2252 } 2253 self.child.clearGrid(); 2254 self.art.clearGrid(); 2255 } 2256 2257 pub fn getGrid(self: AnsiBackground) ?Grid { 2258 return self.grid orelse self.child.getGrid(); 2259 } 2260 2261 pub fn getFocus(self: *AnsiBackground) *Focus { 2262 return self.child.getFocus(); 2263 } 2264 2265 // which top corner the art hugs. 2266 const ArtAnchor = enum { top_left, top_right }; 2267 2268 // a foreground cell counts as blank if it's empty or a bare space without styling 2269 fn cellIsBlank(cell: Grid.Cell) bool { 2270 if (cell.rune) |rune| { 2271 return std.mem.eql(u8, rune, " ") and 2272 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2273 } 2274 return true; 2275 } 2276 2277 // the art is dimmed to this fraction of its brightness so foreground text 2278 // stays legible over it 2279 const art_brightness = 45; // percent 2280 2281 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2282 const c = color orelse return null; 2283 return .{ 2284 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2285 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2286 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2287 }; 2288 } 2289 2290 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2291 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2292 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2293 // snap such channels clear of that range so the backdrop doesn't blink or 2294 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2295 // actually render truecolor. 2296 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2297 const c = color orelse return null; 2298 const snap = struct { 2299 fn f(v: u8) u8 { 2300 return if (v >= 1 and v <= 9) 10 else v; 2301 } 2302 }.f; 2303 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2304 } 2305 2306 // composites ANSI art behind a foreground grid 2307 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2308 art.clearGrid(); 2309 try art.build(allocator, .{ 2310 .min_size = .{ .width = null, .height = null }, 2311 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2312 }, root_focus); 2313 2314 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2315 errdefer out.deinit(); 2316 2317 if (art.getGrid()) |art_grid| { 2318 const anchor_x = switch (anchor) { 2319 .top_left => 0, 2320 .top_right => foreground.size.width -| art_grid.size.width, 2321 }; 2322 for (0..art_grid.size.height) |y| { 2323 for (0..art_grid.size.width) |x| { 2324 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2325 if (src.rune == null) continue; 2326 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2327 const dst = &out.cells.items[idx]; 2328 // composite the dimmed art behind every cell so it shows 2329 // through the whole UI: a blank cell becomes the art, while a 2330 // cell with a glyph keeps the glyph but takes the dimmed art as 2331 // its background (replacing whatever background it had, which 2332 // would otherwise obscure the art). 2333 if (cellIsBlank(dst.*)) { 2334 dst.* = src; 2335 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2336 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2337 } else { 2338 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2339 } 2340 } 2341 } 2342 } 2343 return out; 2344 } 2345 }; 2346