╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
834 835 pub const Scroll = struct { 836 scroll: wgt.Scroll(Widget), 837 838 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options) !Scroll { 839 var layout_inner = FlowBox.init(allocator, options); 840 errdefer layout_inner.deinit(allocator); 841 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .vert); 842 errdefer scroll.deinit(allocator); 843 return .{ .scroll = scroll }; 844 } 845 846 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 847 self.scroll.deinit(allocator); 848 } 849 850 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const []const u8, links: ?[]const []const u8) !void { 851 self.scroll.x = 0; 852 self.scroll.y = 0; 853 try self.inner().setItems(allocator, items, links); 854 } 855 856 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 857 try self.scroll.build(allocator, constraint, root_focus); 858 } 859 860 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 861 _ = allocator; 862 const in = self.inner(); 863 const child_id = in.focus.child_id orelse return; 864 const current_index = in.indexOfFocusId(child_id) orelse return; 865 const count = in.text_boxes.items.len; 866 if (count == 0) return; 867 const cols = in.last_cols; 868 const slot_height = in.options.cell_height + FlowBox.border_rows; 869 870 var index = current_index; 871 switch (key) { 872 .arrow_up => index -|= cols, 873 .arrow_down => if (index + cols < count) { 874 index += cols; 875 }, 876 .arrow_left => index -|= 1, 877 .arrow_right => if (index + 1 < count) { 878 index += 1; 879 }, 880 .home => index = 0, 881 .end => index = count - 1, 882 .page_up => { 883 if (self.scroll.grid) |grid| if (slot_height > 0) { 884 const rows_per_page = grid.size.height / slot_height; 885 index -|= rows_per_page * cols; 886 }; 887 }, 888 .page_down => { 889 if (self.scroll.grid) |grid| if (slot_height > 0) { 890 const rows_per_page = grid.size.height / slot_height; 891 index = @min(index + rows_per_page * cols, count - 1); 892 }; 893 }, 894 // scroll wheel moves the focused cell by a full row so the 895 // viewport (via scrollToRect) follows in row-sized steps, 896 // matching how a scroll wheel feels in a grid view. 897 .mouse => |mouse| switch (mouse.action) { 898 .scroll => |dir| switch (dir) { 899 .up => index -|= cols, 900 .down => if (index + cols < count) { 901 index += cols; 902 }, 903 }, 904 else => {}, 905 }, 906 else => {}, 907 } 908 909 if (index != current_index) { 910 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 911 if (in.cellRect(index)) |rect| { 912 self.scroll.scrollToRect(rect); 913 } 914 } 915 } 916 917 pub fn clearGrid(self: *Scroll) void { 918 self.scroll.clearGrid(); 919 } 920 921 pub fn getGrid(self: Scroll) ?Grid { 922 return self.scroll.getGrid(); 923 } 924 925 pub fn getFocus(self: *Scroll) *Focus { 926 return self.scroll.getFocus(); 927 } 928 929 pub fn getSelectedIndex(self: Scroll) ?usize { 930 const in = self.scroll.child.flow_box; 931 const child_id = in.focus.child_id orelse return null; 932 return in.indexOfFocusId(child_id); 933 } 934 935 fn inner(self: *Scroll) *FlowBox { 936 return &self.scroll.child.flow_box; 937 } 938 }; 939 }; 940 941 // an invisible widget that fills the horizontal space granted by its parent. 942 // used inside Box(horiz) with a min_size so the box reserves space for the 943 // children that follow, pushing them to the right. 944 pub const Spacer = struct { 945 focus: Focus, 946 grid: ?Grid, 947 948 pub fn init() Spacer { 949 return .{ 950 .focus = Focus.init(.container), 951 .grid = null, 952 }; 953 } 954 955 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 956 self.focus.deinit(allocator); 957 if (self.grid) |*grid| { 958 grid.deinit(); 959 self.grid = null; 960 } 961 } 962 963 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 964 _ = root_focus; 965 self.clearGrid(); 966 const width = constraint.max_size.width orelse return; 967 if (width == 0) return; 968 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 969 } 970 971 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 972 _ = self; 973 _ = allocator; 974 _ = key; 975 _ = root_focus; 976 } 977 978 pub fn clearGrid(self: *Spacer) void { 979 if (self.grid) |*grid| { 980 grid.deinit(); 981 self.grid = null; 982 } 983 } 984 985 pub fn getGrid(self: Spacer) ?Grid { 986 return self.grid; 987 } 988 989 pub fn getFocus(self: *Spacer) *Focus { 990 return &self.focus; 991 } 992 }; 993 994 // a single-child wrapper that builds the child at its natural size and 995 // positions its grid in the middle of the area granted by the parent 996 pub const Center = struct { 997 focus: Focus, 998 grid: ?Grid, 999 child: *Widget, 1000 direction: Direction, 1001 1002 pub const Direction = enum { both, horiz, vert }; 1003 1004 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1005 const child = try allocator.create(Widget); 1006 errdefer allocator.destroy(child); 1007 child.* = child_widget; 1008 return .{ 1009 .focus = Focus.init(.container), 1010 .grid = null, 1011 .child = child, 1012 .direction = direction, 1013 }; 1014 } 1015 1016 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1017 self.focus.deinit(allocator); 1018 if (self.grid) |*grid| { 1019 grid.deinit(); 1020 self.grid = null; 1021 } 1022 self.child.deinit(allocator); 1023 allocator.destroy(self.child); 1024 } 1025 1026 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1027 self.clearGrid(); 1028 self.getFocus().clear(); 1029 1030 // build the child without forcing it to fill min_size; it sizes 1031 // itself to its content within the available max. 1032 try self.child.build(allocator, .{ 1033 .min_size = .{ .width = null, .height = null }, 1034 .max_size = constraint.max_size, 1035 }, root_focus); 1036 1037 const child_grid = self.child.getGrid() orelse return; 1038 1039 // prefer max when given; otherwise grow to max(min, child) so that 1040 // when only a min is set (e.g. the wasm path passes viewport rows 1041 // as min_height with no max), we can still vertically center while 1042 // letting taller content extend past the min. 1043 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1044 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1045 if (width == 0 or height == 0) return; 1046 1047 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1048 (width -| child_grid.size.width) / 2 1049 else 1050 0; 1051 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1052 (height -| child_grid.size.height) / 2 1053 else 1054 0; 1055 1056 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1057 errdefer grid.deinit(); 1058 try grid.drawGrid(child_grid, offset_x, offset_y); 1059 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1060 self.getFocus().child_id = self.child.getFocus().id; 1061 1062 self.grid = grid; 1063 } 1064 1065 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1066 try self.child.input(allocator, key, root_focus); 1067 } 1068 1069 pub fn clearGrid(self: *Center) void { 1070 if (self.grid) |*grid| { 1071 grid.deinit(); 1072 self.grid = null; 1073 } 1074 self.child.clearGrid(); 1075 } 1076 1077 pub fn getGrid(self: Center) ?Grid { 1078 return self.grid; 1079 } 1080 1081 pub fn getFocus(self: *Center) *Focus { 1082 return &self.focus; 1083 } 1084 }; 1085 1086 // renders truecolor ANSI art 1087 pub const AnsiArt = struct { 1088 focus: Focus, 1089 grid: ?Grid, 1090 content: []const u8, 1091 1092 pub fn init(content: []const u8) AnsiArt { 1093 return .{ 1094 .focus = Focus.init(.container), 1095 .grid = null, 1096 .content = content, 1097 }; 1098 } 1099 1100 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1101 self.focus.deinit(allocator); 1102 if (self.grid) |*grid| { 1103 grid.deinit(); 1104 self.grid = null; 1105 } 1106 } 1107 1108 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1109 _ = root_focus; 1110 self.clearGrid(); 1111 1112 // parse into rows of cells, then copy into a rectangular grid 1113 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1114 defer { 1115 for (rows.items) |*row| row.deinit(allocator); 1116 rows.deinit(allocator); 1117 } 1118 var row: std.ArrayList(Grid.Cell) = .empty; 1119 errdefer row.deinit(allocator); 1120 1121 var style: Grid.Style = .{}; 1122 var width: usize = 0; 1123 const content = self.content; 1124 var i: usize = 0; 1125 while (i < content.len) { 1126 const byte = content[i]; 1127 if (byte == '\n') { 1128 width = @max(width, row.items.len); 1129 try rows.append(allocator, row); 1130 row = .empty; 1131 i += 1; 1132 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1133 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1134 var j = i + 2; 1135 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1136 if (j >= content.len) { 1137 i = content.len; // malformed trailing escape; stop 1138 } else { 1139 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1140 i = j + 1; 1141 } 1142 } else { 1143 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1144 const end = @min(content.len, i + len); 1145 const rune = content[i..end]; 1146 const transparent = end - i == 1 and rune[0] == ' ' and 1147 style.fg == null and style.bg == null and !style.inverted; 1148 try row.append(allocator, .{ 1149 .rune = if (transparent) null else rune, 1150 .style = style, 1151 }); 1152 i = end; 1153 } 1154 } 1155 // a trailing row with content but no closing newline 1156 if (row.items.len > 0) { 1157 width = @max(width, row.items.len); 1158 try rows.append(allocator, row); 1159 } else { 1160 row.deinit(allocator); 1161 } 1162 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1163 1164 const height = rows.items.len; 1165 if (width == 0 or height == 0) return; 1166 1167 const clamped_w = @min(width, constraint.max_size.width orelse width); 1168 const clamped_h = @min(height, constraint.max_size.height orelse height); 1169 1170 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1171 errdefer grid.deinit(); 1172 for (rows.items[0..clamped_h], 0..) |r, y| { 1173 const n = @min(r.items.len, clamped_w); 1174 for (r.items[0..n], 0..) |cell, x| { 1175 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1176 } 1177 } 1178 self.grid = grid; 1179 } 1180 1181 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1182 _ = self; 1183 _ = allocator; 1184 _ = key; 1185 _ = root_focus; 1186 } 1187 1188 pub fn clearGrid(self: *AnsiArt) void { 1189 if (self.grid) |*grid| { 1190 grid.deinit(); 1191 self.grid = null; 1192 } 1193 } 1194 1195 pub fn getGrid(self: AnsiArt) ?Grid { 1196 return self.grid; 1197 } 1198 1199 pub fn getFocus(self: *AnsiArt) *Focus { 1200 return &self.focus; 1201 } 1202 1203 fn applySgr(style: *Grid.Style, params: []const u8) void { 1204 var nums: [16]u32 = undefined; 1205 var n: usize = 0; 1206 var it = std.mem.splitScalar(u8, params, ';'); 1207 while (it.next()) |tok| { 1208 if (n >= nums.len) break; 1209 // an empty parameter (e.g. bare "\x1b[m") means 0 1210 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1211 n += 1; 1212 } 1213 if (n == 0) { 1214 style.* = .{}; 1215 return; 1216 } 1217 var i: usize = 0; 1218 while (i < n) : (i += 1) { 1219 switch (nums[i]) { 1220 0 => style.* = .{}, 1221 7 => style.inverted = true, 1222 27 => style.inverted = false, 1223 39 => style.fg = null, 1224 49 => style.bg = null, 1225 38, 48 => { 1226 // truecolor form: 38;2;r;g;b — anything else is ignored 1227 if (i + 4 < n and nums[i + 1] == 2) { 1228 const c = Grid.Color{ 1229 .r = @truncate(nums[i + 2]), 1230 .g = @truncate(nums[i + 3]), 1231 .b = @truncate(nums[i + 4]), 1232 }; 1233 if (nums[i] == 38) style.fg = c else style.bg = c; 1234 i += 4; 1235 } 1236 }, 1237 else => {}, 1238 } 1239 } 1240 } 1241 }; 1242 1243 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1244 pub const AnsiBackground = struct { 1245 grid: ?Grid, 1246 child: *Widget, 1247 art: AnsiArt, 1248 session: *Session, 1249 1250 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1251 var cw = child_widget; 1252 const child = allocator.create(Widget) catch |e| { 1253 cw.deinit(allocator); 1254 return e; 1255 }; 1256 child.* = cw; 1257 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content), .session = session }; 1258 } 1259 1260 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1261 if (self.grid) |*grid| grid.deinit(); 1262 self.art.deinit(allocator); 1263 self.child.deinit(allocator); 1264 allocator.destroy(self.child); 1265 } 1266 1267 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1268 self.clearGrid(); 1269 1270 // make the wrapped page fill the whole available area 1271 try self.child.build(allocator, .{ 1272 .min_size = .{ 1273 .width = constraint.max_size.width orelse constraint.min_size.width, 1274 .height = constraint.max_size.height orelse constraint.min_size.height, 1275 }, 1276 .max_size = constraint.max_size, 1277 }, root_focus); 1278 1279 if (self.session.data.enable_ansi) { 1280 if (self.child.getGrid()) |fg| { 1281 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1282 } 1283 } 1284 } 1285 1286 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1287 try self.child.input(allocator, key, root_focus); 1288 } 1289 1290 pub fn clearGrid(self: *AnsiBackground) void { 1291 if (self.grid) |*grid| { 1292 grid.deinit(); 1293 self.grid = null; 1294 } 1295 self.child.clearGrid(); 1296 self.art.clearGrid(); 1297 } 1298 1299 pub fn getGrid(self: AnsiBackground) ?Grid { 1300 return self.grid orelse self.child.getGrid(); 1301 } 1302 1303 pub fn getFocus(self: *AnsiBackground) *Focus { 1304 return self.child.getFocus(); 1305 } 1306 1307 // which top corner the art hugs. 1308 const ArtAnchor = enum { top_left, top_right }; 1309 1310 // a foreground cell counts as blank if it's empty or a bare space without styling 1311 fn cellIsBlank(cell: Grid.Cell) bool { 1312 if (cell.rune) |rune| { 1313 return std.mem.eql(u8, rune, " ") and 1314 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1315 } 1316 return true; 1317 } 1318 1319 // a single representative color for an art cell 1320 fn artCellColor(style: Grid.Style) ?Grid.Color { 1321 const f = style.fg orelse return style.bg; 1322 const b = style.bg orelse return f; 1323 return .{ 1324 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1325 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1326 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1327 }; 1328 } 1329 1330 // behind a visible glyph the art color is dimmed to this fraction of its 1331 // brightness so light terminal text stays legible on top of it 1332 const glyph_bg_brightness = 45; // percent 1333 1334 // a glyph sitting over the art also gets its foreground pinned to this color 1335 // (unless it set its own), so the text stays legible 1336 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1337 1338 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1339 const c = color orelse return null; 1340 return .{ 1341 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1342 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1343 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1344 }; 1345 } 1346 1347 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1348 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1349 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1350 // snap such channels clear of that range so the backdrop doesn't blink or 1351 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1352 // actually render truecolor. 1353 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1354 const c = color orelse return null; 1355 const snap = struct { 1356 fn f(v: u8) u8 { 1357 return if (v >= 1 and v <= 9) 10 else v; 1358 } 1359 }.f; 1360 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1361 } 1362 1363 // composites ANSI art behind a foreground grid 1364 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1365 art.clearGrid(); 1366 try art.build(allocator, .{ 1367 .min_size = .{ .width = null, .height = null }, 1368 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1369 }, root_focus); 1370 1371 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1372 errdefer out.deinit(); 1373 1374 if (art.getGrid()) |art_grid| { 1375 const anchor_x = switch (anchor) { 1376 .top_left => 0, 1377 .top_right => foreground.size.width -| art_grid.size.width, 1378 }; 1379 for (0..art_grid.size.height) |y| { 1380 for (0..art_grid.size.width) |x| { 1381 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1382 if (src.rune == null) continue; 1383 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1384 const dst = &out.cells.items[idx]; 1385 if (cellIsBlank(dst.*)) { 1386 dst.* = src; 1387 dst.style.fg = sgrSafe(dst.style.fg); 1388 dst.style.bg = sgrSafe(dst.style.bg); 1389 } else if (dst.style.bg == null) { 1390 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1391 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1392 } 1393 } 1394 } 1395 } 1396 return out; 1397 } 1398 }; 1399