╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
882 self.clearGrid(); 883 884 // make the wrapped page fill the whole available area 885 try self.child.build(allocator, .{ 886 .min_size = .{ 887 .width = constraint.max_size.width orelse constraint.min_size.width, 888 .height = constraint.max_size.height orelse constraint.min_size.height, 889 }, 890 .max_size = constraint.max_size, 891 }, root_focus); 892 893 if (self.session.data.enable_ansi) { 894 if (self.child.getGrid()) |fg| { 895 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 896 } 897 } 898 } 899 900 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 901 try self.child.input(allocator, key, root_focus); 902 } 903 904 pub fn clearGrid(self: *AnsiBackground) void { 905 if (self.grid) |*grid| { 906 grid.deinit(); 907 self.grid = null; 908 } 909 self.child.clearGrid(); 910 self.art.clearGrid(); 911 } 912 913 pub fn getGrid(self: AnsiBackground) ?Grid { 914 return self.grid orelse self.child.getGrid(); 915 } 916 917 pub fn getFocus(self: *AnsiBackground) *Focus { 918 return self.child.getFocus(); 919 } 920 921 // which top corner the art hugs. 922 const ArtAnchor = enum { top_left, top_right }; 923 924 // a foreground cell counts as blank if it's empty or a bare space without styling 925 fn cellIsBlank(cell: Grid.Cell) bool { 926 if (cell.rune) |rune| { 927 return std.mem.eql(u8, rune, " ") and 928 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 929 } 930 return true; 931 } 932 933 // a single representative color for an art cell 934 fn artCellColor(style: Grid.Style) ?Grid.Color { 935 const f = style.fg orelse return style.bg; 936 const b = style.bg orelse return f; 937 return .{ 938 .r = @intCast((@as(u16, f.r) + b.r) / 2), 939 .g = @intCast((@as(u16, f.g) + b.g) / 2), 940 .b = @intCast((@as(u16, f.b) + b.b) / 2), 941 }; 942 } 943 944 // behind a visible glyph the art color is dimmed to this fraction of its 945 // brightness so light terminal text stays legible on top of it 946 const glyph_bg_brightness = 45; // percent 947 948 // a glyph sitting over the art also gets its foreground pinned to this color 949 // (unless it set its own), so the text stays legible 950 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 951 952 fn dimColor(color: ?Grid.Color) ?Grid.Color { 953 const c = color orelse return null; 954 return .{ 955 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 956 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 957 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 958 }; 959 } 960 961 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 962 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 963 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 964 // snap such channels clear of that range so the backdrop doesn't blink or 965 // hide text there; the ≤9/255 shift is imperceptible on terminals that 966 // actually render truecolor. 967 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 968 const c = color orelse return null; 969 const snap = struct { 970 fn f(v: u8) u8 { 971 return if (v >= 1 and v <= 9) 10 else v; 972 } 973 }.f; 974 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 975 } 976 977 // composites ANSI art behind a foreground grid 978 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 979 art.clearGrid(); 980 try art.build(allocator, .{ 981 .min_size = .{ .width = null, .height = null }, 982 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 983 }, root_focus); 984 985 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 986 errdefer out.deinit(); 987 988 if (art.getGrid()) |art_grid| { 989 const anchor_x = switch (anchor) { 990 .top_left => 0, 991 .top_right => foreground.size.width -| art_grid.size.width, 992 }; 993 for (0..art_grid.size.height) |y| { 994 for (0..art_grid.size.width) |x| { 995 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 996 if (src.rune == null) continue; 997 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 998 const dst = &out.cells.items[idx]; 999 if (cellIsBlank(dst.*)) { 1000 dst.* = src; 1001 dst.style.fg = sgrSafe(dst.style.fg); 1002 dst.style.bg = sgrSafe(dst.style.bg); 1003 } else if (dst.style.bg == null) { 1004 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1005 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1006 } 1007 } 1008 } 1009 } 1010 return out; 1011 } 1012 }; 1013