╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2017 var i: usize = 0; 2018 while (i < n) : (i += 1) { 2019 switch (nums[i]) { 2020 0 => style.* = .{}, 2021 7 => style.inverted = true, 2022 27 => style.inverted = false, 2023 39 => style.fg = null, 2024 49 => style.bg = null, 2025 38, 48 => { 2026 // truecolor form: 38;2;r;g;b — anything else is ignored 2027 if (i + 4 < n and nums[i + 1] == 2) { 2028 const c = Grid.Color{ 2029 .r = @truncate(nums[i + 2]), 2030 .g = @truncate(nums[i + 3]), 2031 .b = @truncate(nums[i + 4]), 2032 }; 2033 if (nums[i] == 38) style.fg = c else style.bg = c; 2034 i += 4; 2035 } 2036 }, 2037 else => {}, 2038 } 2039 } 2040 } 2041 }; 2042 2043 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2044 pub const AnsiBackground = struct { 2045 grid: ?Grid, 2046 child: *Widget, 2047 art: AnsiArt, 2048 session: *Session, 2049 2050 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2051 var cw = child_widget; 2052 const child = allocator.create(Widget) catch |e| { 2053 cw.deinit(allocator); 2054 return e; 2055 }; 2056 child.* = cw; 2057 errdefer { 2058 child.deinit(allocator); 2059 allocator.destroy(child); 2060 } 2061 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2062 } 2063 2064 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2065 if (self.grid) |*grid| grid.deinit(); 2066 self.art.deinit(allocator); 2067 self.child.deinit(allocator); 2068 allocator.destroy(self.child); 2069 } 2070 2071 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2072 self.clearGrid(); 2073 2074 // make the wrapped page fill the whole available area 2075 try self.child.build(allocator, .{ 2076 .min_size = .{ 2077 .width = constraint.max_size.width orelse constraint.min_size.width, 2078 .height = constraint.max_size.height orelse constraint.min_size.height, 2079 }, 2080 .max_size = constraint.max_size, 2081 }, root_focus); 2082 2083 if (self.session.data.enable_ansi) { 2084 if (self.child.getGrid()) |fg| { 2085 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2086 } 2087 } 2088 } 2089 2090 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2091 try self.child.input(allocator, key, root_focus); 2092 } 2093 2094 pub fn clearGrid(self: *AnsiBackground) void { 2095 if (self.grid) |*grid| { 2096 grid.deinit(); 2097 self.grid = null; 2098 } 2099 self.child.clearGrid(); 2100 self.art.clearGrid(); 2101 } 2102 2103 pub fn getGrid(self: AnsiBackground) ?Grid { 2104 return self.grid orelse self.child.getGrid(); 2105 } 2106 2107 pub fn getFocus(self: *AnsiBackground) *Focus { 2108 return self.child.getFocus(); 2109 } 2110 2111 // which top corner the art hugs. 2112 const ArtAnchor = enum { top_left, top_right }; 2113 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