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