╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1096 if (self.scroll.grid) |grid| if (slot_height > 0) { 1097 const rows_per_page = grid.size.height / slot_height; 1098 index = @min(index + rows_per_page * cols, count - 1); 1099 }; 1100 }, 1101 // scroll wheel moves the focused cell by a full row so the 1102 // viewport (via scrollToRect) follows in row-sized steps, 1103 // matching how a scroll wheel feels in a grid view. 1104 .mouse => |mouse| switch (mouse.action) { 1105 .scroll => |dir| switch (dir) { 1106 .up => index -|= cols, 1107 .down => if (index + cols < count) { 1108 index += cols; 1109 }, 1110 }, 1111 else => {}, 1112 }, 1113 else => {}, 1114 } 1115 1116 if (index != current_index) { 1117 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1118 if (in.cellRect(index)) |rect| { 1119 self.scroll.scrollToRect(rect); 1120 } 1121 } 1122 } 1123 1124 pub fn clearGrid(self: *Scroll) void { 1125 self.scroll.clearGrid(); 1126 } 1127 1128 pub fn getGrid(self: Scroll) ?Grid { 1129 return self.scroll.getGrid(); 1130 } 1131 1132 pub fn getFocus(self: *Scroll) *Focus { 1133 return self.scroll.getFocus(); 1134 } 1135 1136 pub fn getSelectedIndex(self: Scroll) ?usize { 1137 const in = self.scroll.child.flow_box; 1138 const child_id = in.focus.child_id orelse return null; 1139 return in.indexOfFocusId(child_id); 1140 } 1141 1142 fn inner(self: *Scroll) *FlowBox { 1143 return &self.scroll.child.flow_box; 1144 } 1145 }; 1146 }; 1147 1148 // an invisible widget that fills the horizontal space granted by its parent. 1149 // used inside Box(horiz) with a min_size so the box reserves space for the 1150 // children that follow, pushing them to the right. 1151 pub const Spacer = struct { 1152 focus: *Focus, 1153 grid: ?Grid, 1154 1155 pub fn init(allocator: std.mem.Allocator) !Spacer { 1156 return .{ 1157 .focus = try Focus.create(allocator, .container), 1158 .grid = null, 1159 }; 1160 } 1161 1162 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1163 self.focus.destroy(allocator); 1164 if (self.grid) |*grid| { 1165 grid.deinit(); 1166 self.grid = null; 1167 } 1168 } 1169 1170 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1171 _ = root_focus; 1172 self.clearGrid(); 1173 const width = constraint.max_size.width orelse return; 1174 if (width == 0) return; 1175 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1176 } 1177 1178 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1179 _ = self; 1180 _ = allocator; 1181 _ = key; 1182 _ = root_focus; 1183 } 1184 1185 pub fn clearGrid(self: *Spacer) void { 1186 if (self.grid) |*grid| { 1187 grid.deinit(); 1188 self.grid = null; 1189 } 1190 } 1191 1192 pub fn getGrid(self: Spacer) ?Grid { 1193 return self.grid; 1194 } 1195 1196 pub fn getFocus(self: *Spacer) *Focus { 1197 return self.focus; 1198 } 1199 }; 1200 1201 // a one-row, non-focusable status bar showing the current page's url 1202 pub const Footer = struct { 1203 focus: *Focus, 1204 grid: ?Grid, 1205 session: *Session, 1206 arena: std.heap.ArenaAllocator, 1207 1208 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1209 return .{ 1210 .focus = try Focus.create(allocator, .container), 1211 .grid = null, 1212 .session = session, 1213 .arena = std.heap.ArenaAllocator.init(allocator), 1214 }; 1215 } 1216 1217 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1218 self.focus.destroy(allocator); 1219 if (self.grid) |*grid| grid.deinit(); 1220 self.arena.deinit(); 1221 } 1222 1223 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1224 _ = root_focus; 1225 self.clearGrid(); 1226 const width = constraint.max_size.width orelse return; 1227 if (width == 0) return; 1228 1229 _ = self.arena.reset(.retain_capacity); 1230 const aa = self.arena.allocator(); 1231 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1232 const text = if (self.session.web_port) |port| 1233 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1234 else 1235 path; 1236 1237 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1238 errdefer grid.deinit(); 1239 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1240 var i: usize = 0; 1241 while (utf8.nextCodepointSlice()) |ch| { 1242 if (i == width) break; 1243 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1244 i += 1; 1245 } 1246 self.grid = grid; 1247 } 1248 1249 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1250 _ = self; 1251 _ = allocator; 1252 _ = key; 1253 _ = root_focus; 1254 } 1255 1256 pub fn clearGrid(self: *Footer) void { 1257 if (self.grid) |*grid| { 1258 grid.deinit(); 1259 self.grid = null; 1260 } 1261 } 1262 1263 pub fn getGrid(self: Footer) ?Grid { 1264 return self.grid; 1265 } 1266 1267 pub fn getFocus(self: *Footer) *Focus { 1268 return self.focus; 1269 } 1270 }; 1271 1272 // a single-child wrapper that builds the child at its natural size and 1273 // positions its grid in the middle of the area granted by the parent 1274 pub const Center = struct { 1275 focus: *Focus, 1276 grid: ?Grid, 1277 child: *Widget, 1278 direction: Direction, 1279 1280 pub const Direction = enum { both, horiz, vert }; 1281 1282 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1283 const child = try allocator.create(Widget); 1284 errdefer allocator.destroy(child); 1285 child.* = child_widget; 1286 return .{ 1287 .focus = try Focus.create(allocator, .container), 1288 .grid = null, 1289 .child = child, 1290 .direction = direction, 1291 }; 1292 } 1293 1294 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1295 self.focus.destroy(allocator); 1296 if (self.grid) |*grid| { 1297 grid.deinit(); 1298 self.grid = null; 1299 } 1300 self.child.deinit(allocator); 1301 allocator.destroy(self.child); 1302 } 1303 1304 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1305 self.clearGrid(); 1306 self.getFocus().clear(); 1307 1308 // build the child without forcing it to fill min_size; it sizes 1309 // itself to its content within the available max. 1310 try self.child.build(allocator, .{ 1311 .min_size = .{ .width = null, .height = null }, 1312 .max_size = constraint.max_size, 1313 }, root_focus); 1314 1315 const child_grid = self.child.getGrid() orelse return; 1316 1317 // prefer max when given; otherwise grow to max(min, child) so that 1318 // when only a min is set (e.g. the wasm path passes viewport rows 1319 // as min_height with no max), we can still vertically center while 1320 // letting taller content extend past the min. 1321 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; 1322 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; 1323 if (width == 0 or height == 0) return; 1324 1325 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1326 (width -| child_grid.size.width) / 2 1327 else 1328 0; 1329 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1330 (height -| child_grid.size.height) / 2 1331 else 1332 0; 1333 1334 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1335 errdefer grid.deinit(); 1336 try grid.drawGrid(child_grid, offset_x, offset_y); 1337 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1338 self.getFocus().child_id = self.child.getFocus().id; 1339 1340 self.grid = grid; 1341 } 1342 1343 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1344 try self.child.input(allocator, key, root_focus); 1345 } 1346 1347 pub fn clearGrid(self: *Center) void { 1348 if (self.grid) |*grid| { 1349 grid.deinit(); 1350 self.grid = null; 1351 } 1352 self.child.clearGrid(); 1353 } 1354 1355 pub fn getGrid(self: Center) ?Grid { 1356 return self.grid; 1357 } 1358 1359 pub fn getFocus(self: *Center) *Focus { 1360 return self.focus; 1361 } 1362 }; 1363 1364 // renders truecolor ANSI art 1365 pub const AnsiArt = struct { 1366 focus: *Focus, 1367 grid: ?Grid, 1368 content: []const u8, 1369 1370 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1371 return .{ 1372 .focus = try Focus.create(allocator, .container), 1373 .grid = null, 1374 .content = content, 1375 }; 1376 } 1377 1378 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1379 self.focus.destroy(allocator); 1380 if (self.grid) |*grid| { 1381 grid.deinit(); 1382 self.grid = null; 1383 } 1384 } 1385 1386 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1387 _ = root_focus; 1388 self.clearGrid(); 1389 1390 // parse into rows of cells, then copy into a rectangular grid 1391 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1392 defer { 1393 for (rows.items) |*row| row.deinit(allocator); 1394 rows.deinit(allocator); 1395 } 1396 var row: std.ArrayList(Grid.Cell) = .empty; 1397 errdefer row.deinit(allocator); 1398 1399 var style: Grid.Style = .{}; 1400 var width: usize = 0; 1401 const content = self.content; 1402 var i: usize = 0; 1403 while (i < content.len) { 1404 const byte = content[i]; 1405 if (byte == '\n') { 1406 width = @max(width, row.items.len); 1407 try rows.append(allocator, row); 1408 row = .empty; 1409 i += 1; 1410 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1411 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1412 var j = i + 2; 1413 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1414 if (j >= content.len) { 1415 i = content.len; // malformed trailing escape; stop 1416 } else { 1417 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1418 i = j + 1; 1419 } 1420 } else { 1421 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1422 const end = @min(content.len, i + len); 1423 const rune = content[i..end]; 1424 const transparent = end - i == 1 and rune[0] == ' ' and 1425 style.fg == null and style.bg == null and !style.inverted; 1426 try row.append(allocator, .{ 1427 .rune = if (transparent) null else rune, 1428 .style = style, 1429 }); 1430 i = end; 1431 } 1432 } 1433 // a trailing row with content but no closing newline 1434 if (row.items.len > 0) { 1435 width = @max(width, row.items.len); 1436 try rows.append(allocator, row); 1437 } else { 1438 row.deinit(allocator); 1439 } 1440 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1441 1442 const height = rows.items.len; 1443 if (width == 0 or height == 0) return; 1444 1445 const clamped_w = @min(width, constraint.max_size.width orelse width); 1446 const clamped_h = @min(height, constraint.max_size.height orelse height); 1447 1448 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1449 errdefer grid.deinit(); 1450 for (rows.items[0..clamped_h], 0..) |r, y| { 1451 const n = @min(r.items.len, clamped_w); 1452 for (r.items[0..n], 0..) |cell, x| { 1453 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1454 } 1455 } 1456 self.grid = grid; 1457 } 1458 1459 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1460 _ = self; 1461 _ = allocator; 1462 _ = key; 1463 _ = root_focus; 1464 } 1465 1466 pub fn clearGrid(self: *AnsiArt) void { 1467 if (self.grid) |*grid| { 1468 grid.deinit(); 1469 self.grid = null; 1470 } 1471 } 1472 1473 pub fn getGrid(self: AnsiArt) ?Grid { 1474 return self.grid; 1475 } 1476 1477 pub fn getFocus(self: *AnsiArt) *Focus { 1478 return self.focus; 1479 } 1480 1481 fn applySgr(style: *Grid.Style, params: []const u8) void { 1482 var nums: [16]u32 = undefined; 1483 var n: usize = 0; 1484 var it = std.mem.splitScalar(u8, params, ';'); 1485 while (it.next()) |tok| { 1486 if (n >= nums.len) break; 1487 // an empty parameter (e.g. bare "\x1b[m") means 0 1488 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1489 n += 1; 1490 } 1491 if (n == 0) { 1492 style.* = .{}; 1493 return; 1494 } 1495 var i: usize = 0; 1496 while (i < n) : (i += 1) { 1497 switch (nums[i]) { 1498 0 => style.* = .{}, 1499 7 => style.inverted = true, 1500 27 => style.inverted = false, 1501 39 => style.fg = null, 1502 49 => style.bg = null, 1503 38, 48 => { 1504 // truecolor form: 38;2;r;g;b — anything else is ignored 1505 if (i + 4 < n and nums[i + 1] == 2) { 1506 const c = Grid.Color{ 1507 .r = @truncate(nums[i + 2]), 1508 .g = @truncate(nums[i + 3]), 1509 .b = @truncate(nums[i + 4]), 1510 }; 1511 if (nums[i] == 38) style.fg = c else style.bg = c; 1512 i += 4; 1513 } 1514 }, 1515 else => {}, 1516 } 1517 } 1518 } 1519 }; 1520 1521 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1522 pub const AnsiBackground = struct { 1523 grid: ?Grid, 1524 child: *Widget, 1525 art: AnsiArt, 1526 session: *Session, 1527 1528 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1529 var cw = child_widget; 1530 const child = allocator.create(Widget) catch |e| { 1531 cw.deinit(allocator); 1532 return e; 1533 }; 1534 child.* = cw; 1535 errdefer { 1536 child.deinit(allocator); 1537 allocator.destroy(child); 1538 } 1539 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1540 } 1541 1542 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1543 if (self.grid) |*grid| grid.deinit(); 1544 self.art.deinit(allocator); 1545 self.child.deinit(allocator); 1546 allocator.destroy(self.child); 1547 } 1548 1549 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1550 self.clearGrid(); 1551 1552 // make the wrapped page fill the whole available area 1553 try self.child.build(allocator, .{ 1554 .min_size = .{ 1555 .width = constraint.max_size.width orelse constraint.min_size.width, 1556 .height = constraint.max_size.height orelse constraint.min_size.height, 1557 }, 1558 .max_size = constraint.max_size, 1559 }, root_focus); 1560 1561 if (self.session.data.enable_ansi) { 1562 if (self.child.getGrid()) |fg| { 1563 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1564 } 1565 } 1566 } 1567 1568 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1569 try self.child.input(allocator, key, root_focus); 1570 } 1571 1572 pub fn clearGrid(self: *AnsiBackground) void { 1573 if (self.grid) |*grid| { 1574 grid.deinit(); 1575 self.grid = null; 1576 } 1577 self.child.clearGrid(); 1578 self.art.clearGrid(); 1579 } 1580 1581 pub fn getGrid(self: AnsiBackground) ?Grid { 1582 return self.grid orelse self.child.getGrid(); 1583 } 1584 1585 pub fn getFocus(self: *AnsiBackground) *Focus { 1586 return self.child.getFocus(); 1587 } 1588 1589 // which top corner the art hugs. 1590 const ArtAnchor = enum { top_left, top_right }; 1591 1592 // a foreground cell counts as blank if it's empty or a bare space without styling 1593 fn cellIsBlank(cell: Grid.Cell) bool { 1594 if (cell.rune) |rune| { 1595 return std.mem.eql(u8, rune, " ") and 1596 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1597 } 1598 return true; 1599 } 1600 1601 // a single representative color for an art cell 1602 fn artCellColor(style: Grid.Style) ?Grid.Color { 1603 const f = style.fg orelse return style.bg; 1604 const b = style.bg orelse return f; 1605 return .{ 1606 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1607 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1608 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1609 }; 1610 } 1611 1612 // behind a visible glyph the art color is dimmed to this fraction of its 1613 // brightness so light terminal text stays legible on top of it 1614 const glyph_bg_brightness = 45; // percent 1615 1616 // a glyph sitting over the art also gets its foreground pinned to this color 1617 // (unless it set its own), so the text stays legible 1618 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1619 1620 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1621 const c = color orelse return null; 1622 return .{ 1623 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1624 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1625 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1626 }; 1627 } 1628 1629 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1630 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1631 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1632 // snap such channels clear of that range so the backdrop doesn't blink or 1633 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1634 // actually render truecolor. 1635 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1636 const c = color orelse return null; 1637 const snap = struct { 1638 fn f(v: u8) u8 { 1639 return if (v >= 1 and v <= 9) 10 else v; 1640 } 1641 }.f; 1642 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1643 } 1644 1645 // composites ANSI art behind a foreground grid 1646 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1647 art.clearGrid(); 1648 try art.build(allocator, .{ 1649 .min_size = .{ .width = null, .height = null }, 1650 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1651 }, root_focus); 1652 1653 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1654 errdefer out.deinit(); 1655 1656 if (art.getGrid()) |art_grid| { 1657 const anchor_x = switch (anchor) { 1658 .top_left => 0, 1659 .top_right => foreground.size.width -| art_grid.size.width, 1660 }; 1661 for (0..art_grid.size.height) |y| { 1662 for (0..art_grid.size.width) |x| { 1663 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1664 if (src.rune == null) continue; 1665 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1666 const dst = &out.cells.items[idx]; 1667 if (cellIsBlank(dst.*)) { 1668 dst.* = src; 1669 dst.style.fg = sgrSafe(dst.style.fg); 1670 dst.style.bg = sgrSafe(dst.style.bg); 1671 } else if (dst.style.bg == null) { 1672 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1673 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1674 } 1675 } 1676 } 1677 } 1678 return out; 1679 } 1680 }; 1681