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