╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2061 fn cellIsBlank(cell: Grid.Cell) bool { 2062 if (cell.rune) |rune| { 2063 return std.mem.eql(u8, rune, " ") and 2064 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2065 } 2066 return true; 2067 } 2068 2069 // the art is dimmed to this fraction of its brightness so foreground text 2070 // stays legible over it 2071 const art_brightness = 45; // percent 2072 2073 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2074 const c = color orelse return null; 2075 return .{ 2076 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2077 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2078 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2079 }; 2080 } 2081 2082 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2083 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2084 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2085 // snap such channels clear of that range so the backdrop doesn't blink or 2086 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2087 // actually render truecolor. 2088 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2089 const c = color orelse return null; 2090 const snap = struct { 2091 fn f(v: u8) u8 { 2092 return if (v >= 1 and v <= 9) 10 else v; 2093 } 2094 }.f; 2095 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2096 } 2097 2098 // composites ANSI art behind a foreground grid 2099 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2100 art.clearGrid(); 2101 try art.build(allocator, .{ 2102 .min_size = .{ .width = null, .height = null }, 2103 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2104 }, root_focus); 2105 2106 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2107 errdefer out.deinit(); 2108 2109 if (art.getGrid()) |art_grid| { 2110 const anchor_x = switch (anchor) { 2111 .top_left => 0, 2112 .top_right => foreground.size.width -| art_grid.size.width, 2113 }; 2114 for (0..art_grid.size.height) |y| { 2115 for (0..art_grid.size.width) |x| { 2116 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2117 if (src.rune == null) continue; 2118 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2119 const dst = &out.cells.items[idx]; 2120 // composite the dimmed art behind every cell so it shows 2121 // through the whole UI: a blank cell becomes the art, while a 2122 // cell with a glyph keeps the glyph but takes the dimmed art as 2123 // its background (replacing whatever background it had, which 2124 // would otherwise obscure the art). 2125 if (cellIsBlank(dst.*)) { 2126 dst.* = src; 2127 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2128 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2129 } else { 2130 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2131 } 2132 } 2133 } 2134 } 2135 return out; 2136 } 2137 }; 2138