╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1985 pub fn getFocus(self: *AnsiArt) *Focus { 1986 return self.focus; 1987 } 1988 1989 fn applySgr(style: *Grid.Style, params: []const u8) void { 1990 var nums: [16]u32 = undefined; 1991 var n: usize = 0; 1992 var it = std.mem.splitScalar(u8, params, ';'); 1993 while (it.next()) |tok| { 1994 if (n >= nums.len) break; 1995 // an empty parameter (e.g. bare "\x1b[m") means 0 1996 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1997 n += 1; 1998 } 1999 if (n == 0) { 2000 style.* = .{}; 2001 return; 2002 } 2003 var i: usize = 0; 2004 while (i < n) : (i += 1) { 2005 switch (nums[i]) { 2006 0 => style.* = .{}, 2007 7 => style.inverted = true, 2008 27 => style.inverted = false, 2009 39 => style.fg = null, 2010 49 => style.bg = null, 2011 38, 48 => { 2012 // truecolor form: 38;2;r;g;b — anything else is ignored 2013 if (i + 4 < n and nums[i + 1] == 2) { 2014 const c = Grid.Color{ 2015 .r = @truncate(nums[i + 2]), 2016 .g = @truncate(nums[i + 3]), 2017 .b = @truncate(nums[i + 4]), 2018 }; 2019 if (nums[i] == 38) style.fg = c else style.bg = c; 2020 i += 4; 2021 } 2022 }, 2023 else => {}, 2024 } 2025 } 2026 } 2027 }; 2028 2029 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2030 pub const AnsiBackground = struct { 2031 grid: ?Grid, 2032 child: *Widget, 2033 art: AnsiArt, 2034 session: *Session, 2035 2036 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2037 var cw = child_widget; 2038 const child = allocator.create(Widget) catch |e| { 2039 cw.deinit(allocator); 2040 return e; 2041 }; 2042 child.* = cw; 2043 errdefer { 2044 child.deinit(allocator); 2045 allocator.destroy(child); 2046 } 2047 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2048 } 2049 2050 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2051 if (self.grid) |*grid| grid.deinit(); 2052 self.art.deinit(allocator); 2053 self.child.deinit(allocator); 2054 allocator.destroy(self.child); 2055 } 2056 2057 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2058 self.clearGrid(); 2059 2060 // make the wrapped page fill the whole available area 2061 try self.child.build(allocator, .{ 2062 .min_size = .{ 2063 .width = constraint.max_size.width orelse constraint.min_size.width, 2064 .height = constraint.max_size.height orelse constraint.min_size.height, 2065 }, 2066 .max_size = constraint.max_size, 2067 }, root_focus); 2068 2069 if (self.session.data.enable_ansi) { 2070 if (self.child.getGrid()) |fg| { 2071 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2072 } 2073 } 2074 } 2075 2076 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2077 try self.child.input(allocator, key, root_focus); 2078 } 2079 2080 pub fn clearGrid(self: *AnsiBackground) void { 2081 if (self.grid) |*grid| { 2082 grid.deinit(); 2083 self.grid = null; 2084 } 2085 self.child.clearGrid(); 2086 self.art.clearGrid(); 2087 } 2088 2089 pub fn getGrid(self: AnsiBackground) ?Grid { 2090 return self.grid orelse self.child.getGrid(); 2091 } 2092 2093 pub fn getFocus(self: *AnsiBackground) *Focus { 2094 return self.child.getFocus(); 2095 } 2096 2097 // which top corner the art hugs. 2098 const ArtAnchor = enum { top_left, top_right }; 2099 2100 // a foreground cell counts as blank if it's empty or a bare space without styling 2101 fn cellIsBlank(cell: Grid.Cell) bool { 2102 if (cell.rune) |rune| { 2103 return std.mem.eql(u8, rune, " ") and 2104 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2105 } 2106 return true; 2107 } 2108 2109 // the art is dimmed to this fraction of its brightness so foreground text 2110 // stays legible over it 2111 const art_brightness = 45; // percent 2112 2113 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2114 const c = color orelse return null; 2115 return .{ 2116 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2117 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2118 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2119 }; 2120 } 2121 2122 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2123 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2124 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2125 // snap such channels clear of that range so the backdrop doesn't blink or 2126 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2127 // actually render truecolor. 2128 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2129 const c = color orelse return null; 2130 const snap = struct { 2131 fn f(v: u8) u8 { 2132 return if (v >= 1 and v <= 9) 10 else v; 2133 } 2134 }.f; 2135 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2136 } 2137 2138 // composites ANSI art behind a foreground grid 2139 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2140 art.clearGrid(); 2141 try art.build(allocator, .{ 2142 .min_size = .{ .width = null, .height = null }, 2143 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2144 }, root_focus); 2145 2146 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2147 errdefer out.deinit(); 2148 2149 if (art.getGrid()) |art_grid| { 2150 const anchor_x = switch (anchor) { 2151 .top_left => 0, 2152 .top_right => foreground.size.width -| art_grid.size.width, 2153 }; 2154 for (0..art_grid.size.height) |y| { 2155 for (0..art_grid.size.width) |x| { 2156 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2157 if (src.rune == null) continue; 2158 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2159 const dst = &out.cells.items[idx]; 2160 // composite the dimmed art behind every cell so it shows 2161 // through the whole UI: a blank cell becomes the art, while a 2162 // cell with a glyph keeps the glyph but takes the dimmed art as 2163 // its background (replacing whatever background it had, which 2164 // would otherwise obscure the art). 2165 if (cellIsBlank(dst.*)) { 2166 dst.* = src; 2167 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2168 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2169 } else { 2170 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2171 } 2172 } 2173 } 2174 } 2175 return out; 2176 } 2177 }; 2178