╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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