╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1989 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1990 errdefer grid.deinit(); 1991 for (rows.items[0..clamped_h], 0..) |r, y| { 1992 const n = @min(r.items.len, clamped_w); 1993 for (r.items[0..n], 0..) |cell, x| { 1994 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1995 } 1996 } 1997 self.grid = grid; 1998 } 1999 2000 pub fn clearGrid(self: *AnsiArt) void { 2001 if (self.grid) |*grid| { 2002 grid.deinit(); 2003 self.grid = null; 2004 } 2005 } 2006 2007 pub fn getGrid(self: AnsiArt) ?Grid { 2008 return self.grid; 2009 } 2010 2011 pub fn getFocus(self: *AnsiArt) *Focus { 2012 return self.focus; 2013 } 2014 2015 fn applySgr(style: *Grid.Style, params: []const u8) void { 2016 var nums: [16]u32 = undefined; 2017 var n: usize = 0; 2018 var it = std.mem.splitScalar(u8, params, ';'); 2019 while (it.next()) |tok| { 2020 if (n >= nums.len) break; 2021 // an empty parameter (e.g. bare "\x1b[m") means 0 2022 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2023 n += 1; 2024 } 2025 if (n == 0) { 2026 style.* = .{}; 2027 return; 2028 } 2029 var i: usize = 0; 2030 while (i < n) : (i += 1) { 2031 switch (nums[i]) { 2032 0 => style.* = .{}, 2033 7 => style.inverted = true, 2034 27 => style.inverted = false, 2035 39 => style.fg = null, 2036 49 => style.bg = null, 2037 38, 48 => { 2038 // truecolor form: 38;2;r;g;b — anything else is ignored 2039 if (i + 4 < n and nums[i + 1] == 2) { 2040 const c = Grid.Color{ 2041 .r = @truncate(nums[i + 2]), 2042 .g = @truncate(nums[i + 3]), 2043 .b = @truncate(nums[i + 4]), 2044 }; 2045 if (nums[i] == 38) style.fg = c else style.bg = c; 2046 i += 4; 2047 } 2048 }, 2049 else => {}, 2050 } 2051 } 2052 } 2053 }; 2054 2055 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2056 pub const AnsiBackground = struct { 2057 grid: ?Grid, 2058 child: *Widget, 2059 art: AnsiArt, 2060 session: *Session, 2061 2062 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2063 var cw = child_widget; 2064 const child = allocator.create(Widget) catch |e| { 2065 cw.deinit(allocator); 2066 return e; 2067 }; 2068 child.* = cw; 2069 errdefer { 2070 child.deinit(allocator); 2071 allocator.destroy(child); 2072 } 2073 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2074 } 2075 2076 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2077 if (self.grid) |*grid| grid.deinit(); 2078 self.art.deinit(allocator); 2079 self.child.deinit(allocator); 2080 allocator.destroy(self.child); 2081 } 2082 2083 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2084 self.clearGrid(); 2085 2086 // make the wrapped page fill the whole available area 2087 try self.child.build(allocator, .{ 2088 .min_size = .{ 2089 .width = constraint.max_size.width orelse constraint.min_size.width, 2090 .height = constraint.max_size.height orelse constraint.min_size.height, 2091 }, 2092 .max_size = constraint.max_size, 2093 }, root_focus); 2094 2095 if (self.session.data.enable_ansi) { 2096 if (self.child.getGrid()) |fg| { 2097 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2098 } 2099 } 2100 } 2101 2102 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2103 try self.child.input(allocator, key, root_focus); 2104 } 2105 2106 pub fn clearGrid(self: *AnsiBackground) void { 2107 if (self.grid) |*grid| { 2108 grid.deinit(); 2109 self.grid = null; 2110 } 2111 self.child.clearGrid(); 2112 self.art.clearGrid(); 2113 } 2114 2115 pub fn getGrid(self: AnsiBackground) ?Grid { 2116 return self.grid orelse self.child.getGrid(); 2117 } 2118 2119 pub fn getFocus(self: *AnsiBackground) *Focus { 2120 return self.child.getFocus(); 2121 } 2122 2123 // which top corner the art hugs. 2124 const ArtAnchor = enum { top_left, top_right }; 2125 2126 // a foreground cell counts as blank if it's empty or a bare space without styling 2127 fn cellIsBlank(cell: Grid.Cell) bool { 2128 if (cell.rune) |rune| { 2129 return std.mem.eql(u8, rune, " ") and 2130 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2131 } 2132 return true; 2133 } 2134 2135 // the art is dimmed to this fraction of its brightness so foreground text 2136 // stays legible over it 2137 const art_brightness = 45; // percent 2138 2139 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2140 const c = color orelse return null; 2141 return .{ 2142 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2143 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2144 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2145 }; 2146 } 2147 2148 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2149 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2150 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2151 // snap such channels clear of that range so the backdrop doesn't blink or 2152 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2153 // actually render truecolor. 2154 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2155 const c = color orelse return null; 2156 const snap = struct { 2157 fn f(v: u8) u8 { 2158 return if (v >= 1 and v <= 9) 10 else v; 2159 } 2160 }.f; 2161 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2162 } 2163 2164 // composites ANSI art behind a foreground grid 2165 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2166 art.clearGrid(); 2167 try art.build(allocator, .{ 2168 .min_size = .{ .width = null, .height = null }, 2169 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2170 }, root_focus); 2171 2172 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2173 errdefer out.deinit(); 2174 2175 if (art.getGrid()) |art_grid| { 2176 const anchor_x = switch (anchor) { 2177 .top_left => 0, 2178 .top_right => foreground.size.width -| art_grid.size.width, 2179 }; 2180 for (0..art_grid.size.height) |y| { 2181 for (0..art_grid.size.width) |x| { 2182 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2183 if (src.rune == null) continue; 2184 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2185 const dst = &out.cells.items[idx]; 2186 // composite the dimmed art behind every cell so it shows 2187 // through the whole UI: a blank cell becomes the art, while a 2188 // cell with a glyph keeps the glyph but takes the dimmed art as 2189 // its background (replacing whatever background it had, which 2190 // would otherwise obscure the art). 2191 if (cellIsBlank(dst.*)) { 2192 dst.* = src; 2193 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2194 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2195 } else { 2196 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2197 } 2198 } 2199 } 2200 } 2201 return out; 2202 } 2203 }; 2204