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