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