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