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