╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
946 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options) !Scroll { 947 var layout_inner = FlowBox.init(allocator, options); 948 errdefer layout_inner.deinit(allocator); 949 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .vert); 950 errdefer scroll.deinit(allocator); 951 return .{ .scroll = scroll }; 952 } 953 954 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 955 self.scroll.deinit(allocator); 956 } 957 958 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const []const u8, links: ?[]const []const u8) !void { 959 self.scroll.x = 0; 960 self.scroll.y = 0; 961 try self.inner().setItems(allocator, items, links); 962 } 963 964 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 965 try self.scroll.build(allocator, constraint, root_focus); 966 } 967 968 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 969 _ = allocator; 970 const in = self.inner(); 971 const child_id = in.focus.child_id orelse return; 972 const current_index = in.indexOfFocusId(child_id) orelse return; 973 const count = in.text_boxes.items.len; 974 if (count == 0) return; 975 const cols = in.last_cols; 976 const slot_height = in.options.cell_height + FlowBox.border_rows; 977 978 var index = current_index; 979 switch (key) { 980 .arrow_up => index -|= cols, 981 .arrow_down => if (index + cols < count) { 982 index += cols; 983 }, 984 .arrow_left => index -|= 1, 985 .arrow_right => if (index + 1 < count) { 986 index += 1; 987 }, 988 .home => index = 0, 989 .end => index = count - 1, 990 .page_up => { 991 if (self.scroll.grid) |grid| if (slot_height > 0) { 992 const rows_per_page = grid.size.height / slot_height; 993 index -|= rows_per_page * cols; 994 }; 995 }, 996 .page_down => { 997 if (self.scroll.grid) |grid| if (slot_height > 0) { 998 const rows_per_page = grid.size.height / slot_height; 999 index = @min(index + rows_per_page * cols, count - 1); 1000 }; 1001 }, 1002 // scroll wheel moves the focused cell by a full row so the 1003 // viewport (via scrollToRect) follows in row-sized steps, 1004 // matching how a scroll wheel feels in a grid view. 1005 .mouse => |mouse| switch (mouse.action) { 1006 .scroll => |dir| switch (dir) { 1007 .up => index -|= cols, 1008 .down => if (index + cols < count) { 1009 index += cols; 1010 }, 1011 }, 1012 else => {}, 1013 }, 1014 else => {}, 1015 } 1016 1017 if (index != current_index) { 1018 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1019 if (in.cellRect(index)) |rect| { 1020 self.scroll.scrollToRect(rect); 1021 } 1022 } 1023 } 1024 1025 pub fn clearGrid(self: *Scroll) void { 1026 self.scroll.clearGrid(); 1027 } 1028 1029 pub fn getGrid(self: Scroll) ?Grid { 1030 return self.scroll.getGrid(); 1031 } 1032 1033 pub fn getFocus(self: *Scroll) *Focus { 1034 return self.scroll.getFocus(); 1035 } 1036 1037 pub fn getSelectedIndex(self: Scroll) ?usize { 1038 const in = self.scroll.child.flow_box; 1039 const child_id = in.focus.child_id orelse return null; 1040 return in.indexOfFocusId(child_id); 1041 } 1042 1043 fn inner(self: *Scroll) *FlowBox { 1044 return &self.scroll.child.flow_box; 1045 } 1046 }; 1047 }; 1048 1049 // an invisible widget that fills the horizontal space granted by its parent. 1050 // used inside Box(horiz) with a min_size so the box reserves space for the 1051 // children that follow, pushing them to the right. 1052 pub const Spacer = struct { 1053 focus: Focus, 1054 grid: ?Grid, 1055 1056 pub fn init() Spacer { 1057 return .{ 1058 .focus = Focus.init(.container), 1059 .grid = null, 1060 }; 1061 } 1062 1063 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1064 self.focus.deinit(allocator); 1065 if (self.grid) |*grid| { 1066 grid.deinit(); 1067 self.grid = null; 1068 } 1069 } 1070 1071 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1072 _ = root_focus; 1073 self.clearGrid(); 1074 const width = constraint.max_size.width orelse return; 1075 if (width == 0) return; 1076 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1077 } 1078 1079 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1080 _ = self; 1081 _ = allocator; 1082 _ = key; 1083 _ = root_focus; 1084 } 1085 1086 pub fn clearGrid(self: *Spacer) void { 1087 if (self.grid) |*grid| { 1088 grid.deinit(); 1089 self.grid = null; 1090 } 1091 } 1092 1093 pub fn getGrid(self: Spacer) ?Grid { 1094 return self.grid; 1095 } 1096 1097 pub fn getFocus(self: *Spacer) *Focus { 1098 return &self.focus; 1099 } 1100 }; 1101 1102 // a single-child wrapper that builds the child at its natural size and 1103 // positions its grid in the middle of the area granted by the parent 1104 pub const Center = struct { 1105 focus: Focus, 1106 grid: ?Grid, 1107 child: *Widget, 1108 direction: Direction, 1109 1110 pub const Direction = enum { both, horiz, vert }; 1111 1112 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1113 const child = try allocator.create(Widget); 1114 errdefer allocator.destroy(child); 1115 child.* = child_widget; 1116 return .{ 1117 .focus = Focus.init(.container), 1118 .grid = null, 1119 .child = child, 1120 .direction = direction, 1121 }; 1122 } 1123 1124 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1125 self.focus.deinit(allocator); 1126 if (self.grid) |*grid| { 1127 grid.deinit(); 1128 self.grid = null; 1129 } 1130 self.child.deinit(allocator); 1131 allocator.destroy(self.child); 1132 } 1133 1134 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1135 self.clearGrid(); 1136 self.getFocus().clear(); 1137 1138 // build the child without forcing it to fill min_size; it sizes 1139 // itself to its content within the available max. 1140 try self.child.build(allocator, .{ 1141 .min_size = .{ .width = null, .height = null }, 1142 .max_size = constraint.max_size, 1143 }, root_focus); 1144 1145 const child_grid = self.child.getGrid() orelse return; 1146 1147 // prefer max when given; otherwise grow to max(min, child) so that 1148 // when only a min is set (e.g. the wasm path passes viewport rows 1149 // as min_height with no max), we can still vertically center while 1150 // letting taller content extend past the min. 1151 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; 1152 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; 1153 if (width == 0 or height == 0) return; 1154 1155 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1156 (width -| child_grid.size.width) / 2 1157 else 1158 0; 1159 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1160 (height -| child_grid.size.height) / 2 1161 else 1162 0; 1163 1164 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1165 errdefer grid.deinit(); 1166 try grid.drawGrid(child_grid, offset_x, offset_y); 1167 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1168 self.getFocus().child_id = self.child.getFocus().id; 1169 1170 self.grid = grid; 1171 } 1172 1173 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1174 try self.child.input(allocator, key, root_focus); 1175 } 1176 1177 pub fn clearGrid(self: *Center) void { 1178 if (self.grid) |*grid| { 1179 grid.deinit(); 1180 self.grid = null; 1181 } 1182 self.child.clearGrid(); 1183 } 1184 1185 pub fn getGrid(self: Center) ?Grid { 1186 return self.grid; 1187 } 1188 1189 pub fn getFocus(self: *Center) *Focus { 1190 return &self.focus; 1191 } 1192 }; 1193 1194 // renders truecolor ANSI art 1195 pub const AnsiArt = struct { 1196 focus: Focus, 1197 grid: ?Grid, 1198 content: []const u8, 1199 1200 pub fn init(content: []const u8) AnsiArt { 1201 return .{ 1202 .focus = Focus.init(.container), 1203 .grid = null, 1204 .content = content, 1205 }; 1206 } 1207 1208 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1209 self.focus.deinit(allocator); 1210 if (self.grid) |*grid| { 1211 grid.deinit(); 1212 self.grid = null; 1213 } 1214 } 1215 1216 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1217 _ = root_focus; 1218 self.clearGrid(); 1219 1220 // parse into rows of cells, then copy into a rectangular grid 1221 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1222 defer { 1223 for (rows.items) |*row| row.deinit(allocator); 1224 rows.deinit(allocator); 1225 } 1226 var row: std.ArrayList(Grid.Cell) = .empty; 1227 errdefer row.deinit(allocator); 1228 1229 var style: Grid.Style = .{}; 1230 var width: usize = 0; 1231 const content = self.content; 1232 var i: usize = 0; 1233 while (i < content.len) { 1234 const byte = content[i]; 1235 if (byte == '\n') { 1236 width = @max(width, row.items.len); 1237 try rows.append(allocator, row); 1238 row = .empty; 1239 i += 1; 1240 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1241 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1242 var j = i + 2; 1243 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1244 if (j >= content.len) { 1245 i = content.len; // malformed trailing escape; stop 1246 } else { 1247 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1248 i = j + 1; 1249 } 1250 } else { 1251 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1252 const end = @min(content.len, i + len); 1253 const rune = content[i..end]; 1254 const transparent = end - i == 1 and rune[0] == ' ' and 1255 style.fg == null and style.bg == null and !style.inverted; 1256 try row.append(allocator, .{ 1257 .rune = if (transparent) null else rune, 1258 .style = style, 1259 }); 1260 i = end; 1261 } 1262 } 1263 // a trailing row with content but no closing newline 1264 if (row.items.len > 0) { 1265 width = @max(width, row.items.len); 1266 try rows.append(allocator, row); 1267 } else { 1268 row.deinit(allocator); 1269 } 1270 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1271 1272 const height = rows.items.len; 1273 if (width == 0 or height == 0) return; 1274 1275 const clamped_w = @min(width, constraint.max_size.width orelse width); 1276 const clamped_h = @min(height, constraint.max_size.height orelse height); 1277 1278 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1279 errdefer grid.deinit(); 1280 for (rows.items[0..clamped_h], 0..) |r, y| { 1281 const n = @min(r.items.len, clamped_w); 1282 for (r.items[0..n], 0..) |cell, x| { 1283 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1284 } 1285 } 1286 self.grid = grid; 1287 } 1288 1289 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1290 _ = self; 1291 _ = allocator; 1292 _ = key; 1293 _ = root_focus; 1294 } 1295 1296 pub fn clearGrid(self: *AnsiArt) void { 1297 if (self.grid) |*grid| { 1298 grid.deinit(); 1299 self.grid = null; 1300 } 1301 } 1302 1303 pub fn getGrid(self: AnsiArt) ?Grid { 1304 return self.grid; 1305 } 1306 1307 pub fn getFocus(self: *AnsiArt) *Focus { 1308 return &self.focus; 1309 } 1310 1311 fn applySgr(style: *Grid.Style, params: []const u8) void { 1312 var nums: [16]u32 = undefined; 1313 var n: usize = 0; 1314 var it = std.mem.splitScalar(u8, params, ';'); 1315 while (it.next()) |tok| { 1316 if (n >= nums.len) break; 1317 // an empty parameter (e.g. bare "\x1b[m") means 0 1318 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1319 n += 1; 1320 } 1321 if (n == 0) { 1322 style.* = .{}; 1323 return; 1324 } 1325 var i: usize = 0; 1326 while (i < n) : (i += 1) { 1327 switch (nums[i]) { 1328 0 => style.* = .{}, 1329 7 => style.inverted = true, 1330 27 => style.inverted = false, 1331 39 => style.fg = null, 1332 49 => style.bg = null, 1333 38, 48 => { 1334 // truecolor form: 38;2;r;g;b — anything else is ignored 1335 if (i + 4 < n and nums[i + 1] == 2) { 1336 const c = Grid.Color{ 1337 .r = @truncate(nums[i + 2]), 1338 .g = @truncate(nums[i + 3]), 1339 .b = @truncate(nums[i + 4]), 1340 }; 1341 if (nums[i] == 38) style.fg = c else style.bg = c; 1342 i += 4; 1343 } 1344 }, 1345 else => {}, 1346 } 1347 } 1348 } 1349 }; 1350 1351 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1352 pub const AnsiBackground = struct { 1353 grid: ?Grid, 1354 child: *Widget, 1355 art: AnsiArt, 1356 session: *Session, 1357 1358 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1359 var cw = child_widget; 1360 const child = allocator.create(Widget) catch |e| { 1361 cw.deinit(allocator); 1362 return e; 1363 }; 1364 child.* = cw; 1365 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content), .session = session }; 1366 } 1367 1368 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1369 if (self.grid) |*grid| grid.deinit(); 1370 self.art.deinit(allocator); 1371 self.child.deinit(allocator); 1372 allocator.destroy(self.child); 1373 } 1374 1375 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1376 self.clearGrid(); 1377 1378 // make the wrapped page fill the whole available area 1379 try self.child.build(allocator, .{ 1380 .min_size = .{ 1381 .width = constraint.max_size.width orelse constraint.min_size.width, 1382 .height = constraint.max_size.height orelse constraint.min_size.height, 1383 }, 1384 .max_size = constraint.max_size, 1385 }, root_focus); 1386 1387 if (self.session.data.enable_ansi) { 1388 if (self.child.getGrid()) |fg| { 1389 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1390 } 1391 } 1392 } 1393 1394 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1395 try self.child.input(allocator, key, root_focus); 1396 } 1397 1398 pub fn clearGrid(self: *AnsiBackground) void { 1399 if (self.grid) |*grid| { 1400 grid.deinit(); 1401 self.grid = null; 1402 } 1403 self.child.clearGrid(); 1404 self.art.clearGrid(); 1405 } 1406 1407 pub fn getGrid(self: AnsiBackground) ?Grid { 1408 return self.grid orelse self.child.getGrid(); 1409 } 1410 1411 pub fn getFocus(self: *AnsiBackground) *Focus { 1412 return self.child.getFocus(); 1413 } 1414 1415 // which top corner the art hugs. 1416 const ArtAnchor = enum { top_left, top_right }; 1417 1418 // a foreground cell counts as blank if it's empty or a bare space without styling 1419 fn cellIsBlank(cell: Grid.Cell) bool { 1420 if (cell.rune) |rune| { 1421 return std.mem.eql(u8, rune, " ") and 1422 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1423 } 1424 return true; 1425 } 1426 1427 // a single representative color for an art cell 1428 fn artCellColor(style: Grid.Style) ?Grid.Color { 1429 const f = style.fg orelse return style.bg; 1430 const b = style.bg orelse return f; 1431 return .{ 1432 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1433 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1434 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1435 }; 1436 } 1437 1438 // behind a visible glyph the art color is dimmed to this fraction of its 1439 // brightness so light terminal text stays legible on top of it 1440 const glyph_bg_brightness = 45; // percent 1441 1442 // a glyph sitting over the art also gets its foreground pinned to this color 1443 // (unless it set its own), so the text stays legible 1444 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1445 1446 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1447 const c = color orelse return null; 1448 return .{ 1449 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1450 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1451 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1452 }; 1453 } 1454 1455 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1456 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1457 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1458 // snap such channels clear of that range so the backdrop doesn't blink or 1459 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1460 // actually render truecolor. 1461 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1462 const c = color orelse return null; 1463 const snap = struct { 1464 fn f(v: u8) u8 { 1465 return if (v >= 1 and v <= 9) 10 else v; 1466 } 1467 }.f; 1468 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1469 } 1470 1471 // composites ANSI art behind a foreground grid 1472 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1473 art.clearGrid(); 1474 try art.build(allocator, .{ 1475 .min_size = .{ .width = null, .height = null }, 1476 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1477 }, root_focus); 1478 1479 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1480 errdefer out.deinit(); 1481 1482 if (art.getGrid()) |art_grid| { 1483 const anchor_x = switch (anchor) { 1484 .top_left => 0, 1485 .top_right => foreground.size.width -| art_grid.size.width, 1486 }; 1487 for (0..art_grid.size.height) |y| { 1488 for (0..art_grid.size.width) |x| { 1489 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1490 if (src.rune == null) continue; 1491 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1492 const dst = &out.cells.items[idx]; 1493 if (cellIsBlank(dst.*)) { 1494 dst.* = src; 1495 dst.style.fg = sgrSafe(dst.style.fg); 1496 dst.style.bg = sgrSafe(dst.style.bg); 1497 } else if (dst.style.bg == null) { 1498 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1499 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1500 } 1501 } 1502 } 1503 } 1504 return out; 1505 } 1506 }; 1507