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