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