╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1908 }; 1909 } 1910 1911 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1912 self.focus.destroy(allocator); 1913 if (self.grid) |*grid| { 1914 grid.deinit(); 1915 self.grid = null; 1916 } 1917 } 1918 1919 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1920 _ = root_focus; 1921 self.clearGrid(); 1922 1923 // parse into rows of cells, then copy into a rectangular grid 1924 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1925 defer { 1926 for (rows.items) |*row| row.deinit(allocator); 1927 rows.deinit(allocator); 1928 } 1929 var row: std.ArrayList(Grid.Cell) = .empty; 1930 errdefer row.deinit(allocator); 1931 1932 var style: Grid.Style = .{}; 1933 var width: usize = 0; 1934 const content = self.content; 1935 var i: usize = 0; 1936 while (i < content.len) { 1937 const byte = content[i]; 1938 if (byte == '\n') { 1939 width = @max(width, row.items.len); 1940 try rows.append(allocator, row); 1941 row = .empty; 1942 i += 1; 1943 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1944 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1945 var j = i + 2; 1946 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1947 if (j >= content.len) { 1948 i = content.len; // malformed trailing escape; stop 1949 } else { 1950 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1951 i = j + 1; 1952 } 1953 } else { 1954 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1955 const end = @min(content.len, i + len); 1956 const rune = content[i..end]; 1957 const transparent = end - i == 1 and rune[0] == ' ' and 1958 style.fg == null and style.bg == null and !style.inverted; 1959 try row.append(allocator, .{ 1960 .rune = if (transparent) null else rune, 1961 .style = style, 1962 }); 1963 i = end; 1964 } 1965 } 1966 // a trailing row with content but no closing newline 1967 if (row.items.len > 0) { 1968 width = @max(width, row.items.len); 1969 try rows.append(allocator, row); 1970 } else { 1971 row.deinit(allocator); 1972 } 1973 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1974 1975 const height = rows.items.len; 1976 if (width == 0 or height == 0) return; 1977 1978 const clamped_w = @min(width, constraint.max_size.width orelse width); 1979 const clamped_h = @min(height, constraint.max_size.height orelse height); 1980 1981 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1982 errdefer grid.deinit(); 1983 for (rows.items[0..clamped_h], 0..) |r, y| { 1984 const n = @min(r.items.len, clamped_w); 1985 for (r.items[0..n], 0..) |cell, x| { 1986 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1987 } 1988 } 1989 self.grid = grid; 1990 } 1991 1992 pub fn clearGrid(self: *AnsiArt) void { 1993 if (self.grid) |*grid| { 1994 grid.deinit(); 1995 self.grid = null; 1996 } 1997 } 1998 1999 pub fn getGrid(self: AnsiArt) ?Grid { 2000 return self.grid; 2001 } 2002 2003 pub fn getFocus(self: *AnsiArt) *Focus { 2004 return self.focus; 2005 } 2006 2007 fn applySgr(style: *Grid.Style, params: []const u8) void { 2008 var nums: [16]u32 = undefined; 2009 var n: usize = 0; 2010 var it = std.mem.splitScalar(u8, params, ';'); 2011 while (it.next()) |tok| { 2012 if (n >= nums.len) break; 2013 // an empty parameter (e.g. bare "\x1b[m") means 0 2014 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2015 n += 1; 2016 } 2017 if (n == 0) { 2018 style.* = .{}; 2019 return; 2020 } 2021 var i: usize = 0; 2022 while (i < n) : (i += 1) { 2023 switch (nums[i]) { 2024 0 => style.* = .{}, 2025 7 => style.inverted = true, 2026 27 => style.inverted = false, 2027 39 => style.fg = null, 2028 49 => style.bg = null, 2029 38, 48 => { 2030 // truecolor form: 38;2;r;g;b — anything else is ignored 2031 if (i + 4 < n and nums[i + 1] == 2) { 2032 const c = Grid.Color{ 2033 .r = @truncate(nums[i + 2]), 2034 .g = @truncate(nums[i + 3]), 2035 .b = @truncate(nums[i + 4]), 2036 }; 2037 if (nums[i] == 38) style.fg = c else style.bg = c; 2038 i += 4; 2039 } 2040 }, 2041 else => {}, 2042 } 2043 } 2044 } 2045 }; 2046 2047 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2048 pub const AnsiBackground = struct { 2049 grid: ?Grid, 2050 child: *Widget, 2051 art: AnsiArt, 2052 session: *Session, 2053 2054 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2055 var cw = child_widget; 2056 const child = allocator.create(Widget) catch |e| { 2057 cw.deinit(allocator); 2058 return e; 2059 }; 2060 child.* = cw; 2061 errdefer { 2062 child.deinit(allocator); 2063 allocator.destroy(child); 2064 } 2065 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2066 } 2067 2068 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2069 if (self.grid) |*grid| grid.deinit(); 2070 self.art.deinit(allocator); 2071 self.child.deinit(allocator); 2072 allocator.destroy(self.child); 2073 } 2074 2075 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2076 self.clearGrid(); 2077 2078 // make the wrapped page fill the whole available area 2079 try self.child.build(allocator, .{ 2080 .min_size = .{ 2081 .width = constraint.max_size.width orelse constraint.min_size.width, 2082 .height = constraint.max_size.height orelse constraint.min_size.height, 2083 }, 2084 .max_size = constraint.max_size, 2085 }, root_focus); 2086 2087 if (self.session.data.enable_ansi) { 2088 if (self.child.getGrid()) |fg| { 2089 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2090 } 2091 } 2092 } 2093 2094 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2095 try self.child.input(allocator, key, root_focus); 2096 } 2097 2098 pub fn clearGrid(self: *AnsiBackground) void { 2099 if (self.grid) |*grid| { 2100 grid.deinit(); 2101 self.grid = null; 2102 } 2103 self.child.clearGrid(); 2104 self.art.clearGrid(); 2105 } 2106 2107 pub fn getGrid(self: AnsiBackground) ?Grid { 2108 return self.grid orelse self.child.getGrid(); 2109 } 2110 2111 pub fn getFocus(self: *AnsiBackground) *Focus { 2112 return self.child.getFocus(); 2113 } 2114 2115 // which top corner the art hugs. 2116 const ArtAnchor = enum { top_left, top_right }; 2117 2118 // a foreground cell counts as blank if it's empty or a bare space without styling 2119 fn cellIsBlank(cell: Grid.Cell) bool { 2120 if (cell.rune) |rune| { 2121 return std.mem.eql(u8, rune, " ") and 2122 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2123 } 2124 return true; 2125 } 2126 2127 // the art is dimmed to this fraction of its brightness so foreground text 2128 // stays legible over it 2129 const art_brightness = 45; // percent 2130 2131 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2132 const c = color orelse return null; 2133 return .{ 2134 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2135 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2136 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2137 }; 2138 } 2139 2140 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2141 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2142 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2143 // snap such channels clear of that range so the backdrop doesn't blink or 2144 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2145 // actually render truecolor. 2146 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2147 const c = color orelse return null; 2148 const snap = struct { 2149 fn f(v: u8) u8 { 2150 return if (v >= 1 and v <= 9) 10 else v; 2151 } 2152 }.f; 2153 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2154 } 2155 2156 // composites ANSI art behind a foreground grid 2157 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2158 art.clearGrid(); 2159 try art.build(allocator, .{ 2160 .min_size = .{ .width = null, .height = null }, 2161 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2162 }, root_focus); 2163 2164 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2165 errdefer out.deinit(); 2166 2167 if (art.getGrid()) |art_grid| { 2168 const anchor_x = switch (anchor) { 2169 .top_left => 0, 2170 .top_right => foreground.size.width -| art_grid.size.width, 2171 }; 2172 for (0..art_grid.size.height) |y| { 2173 for (0..art_grid.size.width) |x| { 2174 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2175 if (src.rune == null) continue; 2176 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2177 const dst = &out.cells.items[idx]; 2178 // composite the dimmed art behind every cell so it shows 2179 // through the whole UI: a blank cell becomes the art, while a 2180 // cell with a glyph keeps the glyph but takes the dimmed art as 2181 // its background (replacing whatever background it had, which 2182 // would otherwise obscure the art). 2183 if (cellIsBlank(dst.*)) { 2184 dst.* = src; 2185 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2186 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2187 } else { 2188 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2189 } 2190 } 2191 } 2192 } 2193 return out; 2194 } 2195 }; 2196