╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1278 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1279 errdefer scroll.deinit(allocator); 1280 return .{ .scroll = scroll }; 1281 } 1282 1283 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1284 self.scroll.deinit(allocator); 1285 } 1286 1287 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1288 self.scroll.x = 0; 1289 self.scroll.y = 0; 1290 try self.inner().setItems(allocator, items); 1291 } 1292 1293 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1294 try self.scroll.build(allocator, constraint, root_focus); 1295 } 1296 1297 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1298 _ = allocator; 1299 const in = self.inner(); 1300 const child_id = in.focus.child_id orelse return; 1301 const current_index = in.indexOfFocusId(child_id) orelse return; 1302 const count = in.text_boxes.items.len; 1303 if (count == 0) return; 1304 const cols = in.last_cols; 1305 const slot_height = in.options.cell_height + FlowBox.border_rows; 1306 1307 var index = current_index; 1308 switch (key) { 1309 .arrow_up => index -|= cols, 1310 .arrow_down => if (index + cols < count) { 1311 index += cols; 1312 }, 1313 .arrow_left => index -|= 1, 1314 .arrow_right => if (index + 1 < count) { 1315 index += 1; 1316 }, 1317 .home => index = 0, 1318 .end => index = count - 1, 1319 .page_up => { 1320 if (self.scroll.grid) |grid| if (slot_height > 0) { 1321 const rows_per_page = grid.size.height / slot_height; 1322 index -|= rows_per_page * cols; 1323 }; 1324 }, 1325 .page_down => { 1326 if (self.scroll.grid) |grid| if (slot_height > 0) { 1327 const rows_per_page = grid.size.height / slot_height; 1328 index = @min(index + rows_per_page * cols, count - 1); 1329 }; 1330 }, 1331 // scroll wheel moves the focused cell by a full row so the 1332 // viewport (via scrollToRect) follows in row-sized steps, 1333 // matching how a scroll wheel feels in a grid view. 1334 .mouse => |mouse| switch (mouse.action) { 1335 .scroll => |dir| switch (dir) { 1336 .up => index -|= cols, 1337 .down => if (index + cols < count) { 1338 index += cols; 1339 }, 1340 }, 1341 else => {}, 1342 }, 1343 else => {}, 1344 } 1345 1346 if (index != current_index) { 1347 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1348 if (in.cellRect(index)) |rect| { 1349 self.scroll.scrollToRect(rect); 1350 } 1351 } 1352 } 1353 1354 pub fn clearGrid(self: *Scroll) void { 1355 self.scroll.clearGrid(); 1356 } 1357 1358 pub fn getGrid(self: Scroll) ?Grid { 1359 return self.scroll.getGrid(); 1360 } 1361 1362 pub fn getFocus(self: *Scroll) *Focus { 1363 return self.scroll.getFocus(); 1364 } 1365 1366 pub fn getSelectedIndex(self: Scroll) ?usize { 1367 const in = self.scroll.child.flow_box; 1368 const child_id = in.focus.child_id orelse return null; 1369 return in.indexOfFocusId(child_id); 1370 } 1371 1372 fn inner(self: *Scroll) *FlowBox { 1373 return &self.scroll.child.flow_box; 1374 } 1375 }; 1376 }; 1377 1378 // an invisible widget that fills the horizontal space granted by its parent. 1379 // used inside Box(horiz) with a min_size so the box reserves space for the 1380 // children that follow, pushing them to the right. 1381 pub const Spacer = struct { 1382 focus: *Focus, 1383 grid: ?Grid, 1384 1385 pub fn init(allocator: std.mem.Allocator) !Spacer { 1386 return .{ 1387 .focus = try Focus.create(allocator, .container), 1388 .grid = null, 1389 }; 1390 } 1391 1392 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1393 self.focus.destroy(allocator); 1394 if (self.grid) |*grid| { 1395 grid.deinit(); 1396 self.grid = null; 1397 } 1398 } 1399 1400 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1401 _ = root_focus; 1402 self.clearGrid(); 1403 const width = constraint.max_size.width orelse return; 1404 if (width == 0) return; 1405 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1406 } 1407 1408 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1409 _ = self; 1410 _ = allocator; 1411 _ = key; 1412 _ = root_focus; 1413 } 1414 1415 pub fn clearGrid(self: *Spacer) void { 1416 if (self.grid) |*grid| { 1417 grid.deinit(); 1418 self.grid = null; 1419 } 1420 } 1421 1422 pub fn getGrid(self: Spacer) ?Grid { 1423 return self.grid; 1424 } 1425 1426 pub fn getFocus(self: *Spacer) *Focus { 1427 return self.focus; 1428 } 1429 }; 1430 1431 // a one-row, non-focusable status bar showing the current page's url 1432 pub const Footer = struct { 1433 focus: *Focus, 1434 grid: ?Grid, 1435 session: *Session, 1436 arena: std.heap.ArenaAllocator, 1437 1438 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1439 return .{ 1440 .focus = try Focus.create(allocator, .container), 1441 .grid = null, 1442 .session = session, 1443 .arena = std.heap.ArenaAllocator.init(allocator), 1444 }; 1445 } 1446 1447 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1448 self.focus.destroy(allocator); 1449 if (self.grid) |*grid| grid.deinit(); 1450 self.arena.deinit(); 1451 } 1452 1453 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1454 _ = root_focus; 1455 self.clearGrid(); 1456 const width = constraint.max_size.width orelse return; 1457 if (width == 0) return; 1458 1459 _ = self.arena.reset(.retain_capacity); 1460 const aa = self.arena.allocator(); 1461 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1462 const text = if (self.session.web_port) |port| 1463 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1464 else 1465 path; 1466 1467 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1468 errdefer grid.deinit(); 1469 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1470 var i: usize = 0; 1471 while (utf8.nextCodepointSlice()) |ch| { 1472 if (i == width) break; 1473 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1474 i += 1; 1475 } 1476 self.grid = grid; 1477 } 1478 1479 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1480 _ = self; 1481 _ = allocator; 1482 _ = key; 1483 _ = root_focus; 1484 } 1485 1486 pub fn clearGrid(self: *Footer) void { 1487 if (self.grid) |*grid| { 1488 grid.deinit(); 1489 self.grid = null; 1490 } 1491 } 1492 1493 pub fn getGrid(self: Footer) ?Grid { 1494 return self.grid; 1495 } 1496 1497 pub fn getFocus(self: *Footer) *Focus { 1498 return self.focus; 1499 } 1500 }; 1501 1502 // a single-child wrapper that builds the child at its natural size and 1503 // positions its grid in the middle of the area granted by the parent 1504 pub const Center = struct { 1505 focus: *Focus, 1506 grid: ?Grid, 1507 child: *Widget, 1508 direction: Direction, 1509 1510 pub const Direction = enum { both, horiz, vert }; 1511 1512 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1513 const child = try allocator.create(Widget); 1514 errdefer allocator.destroy(child); 1515 child.* = child_widget; 1516 return .{ 1517 .focus = try Focus.create(allocator, .container), 1518 .grid = null, 1519 .child = child, 1520 .direction = direction, 1521 }; 1522 } 1523 1524 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1525 self.focus.destroy(allocator); 1526 if (self.grid) |*grid| { 1527 grid.deinit(); 1528 self.grid = null; 1529 } 1530 self.child.deinit(allocator); 1531 allocator.destroy(self.child); 1532 } 1533 1534 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1535 self.clearGrid(); 1536 self.getFocus().clear(); 1537 1538 // build the child without forcing it to fill min_size; it sizes 1539 // itself to its content within the available max. 1540 try self.child.build(allocator, .{ 1541 .min_size = .{ .width = null, .height = null }, 1542 .max_size = constraint.max_size, 1543 }, root_focus); 1544 1545 const child_grid = self.child.getGrid() orelse return; 1546 1547 // prefer max when given; otherwise grow to max(min, child) so that 1548 // when only a min is set (e.g. the wasm path passes viewport rows 1549 // as min_height with no max), we can still vertically center while 1550 // letting taller content extend past the min. 1551 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; 1552 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; 1553 if (width == 0 or height == 0) return; 1554 1555 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1556 (width -| child_grid.size.width) / 2 1557 else 1558 0; 1559 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1560 (height -| child_grid.size.height) / 2 1561 else 1562 0; 1563 1564 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1565 errdefer grid.deinit(); 1566 try grid.drawGrid(child_grid, offset_x, offset_y); 1567 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1568 self.getFocus().child_id = self.child.getFocus().id; 1569 1570 self.grid = grid; 1571 } 1572 1573 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1574 try self.child.input(allocator, key, root_focus); 1575 } 1576 1577 pub fn clearGrid(self: *Center) void { 1578 if (self.grid) |*grid| { 1579 grid.deinit(); 1580 self.grid = null; 1581 } 1582 self.child.clearGrid(); 1583 } 1584 1585 pub fn getGrid(self: Center) ?Grid { 1586 return self.grid; 1587 } 1588 1589 pub fn getFocus(self: *Center) *Focus { 1590 return self.focus; 1591 } 1592 }; 1593 1594 // renders truecolor ANSI art 1595 pub const AnsiArt = struct { 1596 focus: *Focus, 1597 grid: ?Grid, 1598 content: []const u8, 1599 1600 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1601 return .{ 1602 .focus = try Focus.create(allocator, .container), 1603 .grid = null, 1604 .content = content, 1605 }; 1606 } 1607 1608 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1609 self.focus.destroy(allocator); 1610 if (self.grid) |*grid| { 1611 grid.deinit(); 1612 self.grid = null; 1613 } 1614 } 1615 1616 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1617 _ = root_focus; 1618 self.clearGrid(); 1619 1620 // parse into rows of cells, then copy into a rectangular grid 1621 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1622 defer { 1623 for (rows.items) |*row| row.deinit(allocator); 1624 rows.deinit(allocator); 1625 } 1626 var row: std.ArrayList(Grid.Cell) = .empty; 1627 errdefer row.deinit(allocator); 1628 1629 var style: Grid.Style = .{}; 1630 var width: usize = 0; 1631 const content = self.content; 1632 var i: usize = 0; 1633 while (i < content.len) { 1634 const byte = content[i]; 1635 if (byte == '\n') { 1636 width = @max(width, row.items.len); 1637 try rows.append(allocator, row); 1638 row = .empty; 1639 i += 1; 1640 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1641 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1642 var j = i + 2; 1643 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1644 if (j >= content.len) { 1645 i = content.len; // malformed trailing escape; stop 1646 } else { 1647 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1648 i = j + 1; 1649 } 1650 } else { 1651 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1652 const end = @min(content.len, i + len); 1653 const rune = content[i..end]; 1654 const transparent = end - i == 1 and rune[0] == ' ' and 1655 style.fg == null and style.bg == null and !style.inverted; 1656 try row.append(allocator, .{ 1657 .rune = if (transparent) null else rune, 1658 .style = style, 1659 }); 1660 i = end; 1661 } 1662 } 1663 // a trailing row with content but no closing newline 1664 if (row.items.len > 0) { 1665 width = @max(width, row.items.len); 1666 try rows.append(allocator, row); 1667 } else { 1668 row.deinit(allocator); 1669 } 1670 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1671 1672 const height = rows.items.len; 1673 if (width == 0 or height == 0) return; 1674 1675 const clamped_w = @min(width, constraint.max_size.width orelse width); 1676 const clamped_h = @min(height, constraint.max_size.height orelse height); 1677 1678 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1679 errdefer grid.deinit(); 1680 for (rows.items[0..clamped_h], 0..) |r, y| { 1681 const n = @min(r.items.len, clamped_w); 1682 for (r.items[0..n], 0..) |cell, x| { 1683 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1684 } 1685 } 1686 self.grid = grid; 1687 } 1688 1689 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1690 _ = self; 1691 _ = allocator; 1692 _ = key; 1693 _ = root_focus; 1694 } 1695 1696 pub fn clearGrid(self: *AnsiArt) void { 1697 if (self.grid) |*grid| { 1698 grid.deinit(); 1699 self.grid = null; 1700 } 1701 } 1702 1703 pub fn getGrid(self: AnsiArt) ?Grid { 1704 return self.grid; 1705 } 1706 1707 pub fn getFocus(self: *AnsiArt) *Focus { 1708 return self.focus; 1709 } 1710 1711 fn applySgr(style: *Grid.Style, params: []const u8) void { 1712 var nums: [16]u32 = undefined; 1713 var n: usize = 0; 1714 var it = std.mem.splitScalar(u8, params, ';'); 1715 while (it.next()) |tok| { 1716 if (n >= nums.len) break; 1717 // an empty parameter (e.g. bare "\x1b[m") means 0 1718 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1719 n += 1; 1720 } 1721 if (n == 0) { 1722 style.* = .{}; 1723 return; 1724 } 1725 var i: usize = 0; 1726 while (i < n) : (i += 1) { 1727 switch (nums[i]) { 1728 0 => style.* = .{}, 1729 7 => style.inverted = true, 1730 27 => style.inverted = false, 1731 39 => style.fg = null, 1732 49 => style.bg = null, 1733 38, 48 => { 1734 // truecolor form: 38;2;r;g;b — anything else is ignored 1735 if (i + 4 < n and nums[i + 1] == 2) { 1736 const c = Grid.Color{ 1737 .r = @truncate(nums[i + 2]), 1738 .g = @truncate(nums[i + 3]), 1739 .b = @truncate(nums[i + 4]), 1740 }; 1741 if (nums[i] == 38) style.fg = c else style.bg = c; 1742 i += 4; 1743 } 1744 }, 1745 else => {}, 1746 } 1747 } 1748 } 1749 }; 1750 1751 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1752 pub const AnsiBackground = struct { 1753 grid: ?Grid, 1754 child: *Widget, 1755 art: AnsiArt, 1756 session: *Session, 1757 1758 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1759 var cw = child_widget; 1760 const child = allocator.create(Widget) catch |e| { 1761 cw.deinit(allocator); 1762 return e; 1763 }; 1764 child.* = cw; 1765 errdefer { 1766 child.deinit(allocator); 1767 allocator.destroy(child); 1768 } 1769 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1770 } 1771 1772 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1773 if (self.grid) |*grid| grid.deinit(); 1774 self.art.deinit(allocator); 1775 self.child.deinit(allocator); 1776 allocator.destroy(self.child); 1777 } 1778 1779 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1780 self.clearGrid(); 1781 1782 // make the wrapped page fill the whole available area 1783 try self.child.build(allocator, .{ 1784 .min_size = .{ 1785 .width = constraint.max_size.width orelse constraint.min_size.width, 1786 .height = constraint.max_size.height orelse constraint.min_size.height, 1787 }, 1788 .max_size = constraint.max_size, 1789 }, root_focus); 1790 1791 if (self.session.data.enable_ansi) { 1792 if (self.child.getGrid()) |fg| { 1793 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1794 } 1795 } 1796 } 1797 1798 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1799 try self.child.input(allocator, key, root_focus); 1800 } 1801 1802 pub fn clearGrid(self: *AnsiBackground) void { 1803 if (self.grid) |*grid| { 1804 grid.deinit(); 1805 self.grid = null; 1806 } 1807 self.child.clearGrid(); 1808 self.art.clearGrid(); 1809 } 1810 1811 pub fn getGrid(self: AnsiBackground) ?Grid { 1812 return self.grid orelse self.child.getGrid(); 1813 } 1814 1815 pub fn getFocus(self: *AnsiBackground) *Focus { 1816 return self.child.getFocus(); 1817 } 1818 1819 // which top corner the art hugs. 1820 const ArtAnchor = enum { top_left, top_right }; 1821 1822 // a foreground cell counts as blank if it's empty or a bare space without styling 1823 fn cellIsBlank(cell: Grid.Cell) bool { 1824 if (cell.rune) |rune| { 1825 return std.mem.eql(u8, rune, " ") and 1826 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1827 } 1828 return true; 1829 } 1830 1831 // the art is dimmed to this fraction of its brightness so foreground text 1832 // stays legible over it 1833 const art_brightness = 45; // percent 1834 1835 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1836 const c = color orelse return null; 1837 return .{ 1838 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1839 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1840 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1841 }; 1842 } 1843 1844 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1845 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1846 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1847 // snap such channels clear of that range so the backdrop doesn't blink or 1848 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1849 // actually render truecolor. 1850 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1851 const c = color orelse return null; 1852 const snap = struct { 1853 fn f(v: u8) u8 { 1854 return if (v >= 1 and v <= 9) 10 else v; 1855 } 1856 }.f; 1857 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1858 } 1859 1860 // composites ANSI art behind a foreground grid 1861 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1862 art.clearGrid(); 1863 try art.build(allocator, .{ 1864 .min_size = .{ .width = null, .height = null }, 1865 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1866 }, root_focus); 1867 1868 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1869 errdefer out.deinit(); 1870 1871 if (art.getGrid()) |art_grid| { 1872 const anchor_x = switch (anchor) { 1873 .top_left => 0, 1874 .top_right => foreground.size.width -| art_grid.size.width, 1875 }; 1876 for (0..art_grid.size.height) |y| { 1877 for (0..art_grid.size.width) |x| { 1878 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1879 if (src.rune == null) continue; 1880 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1881 const dst = &out.cells.items[idx]; 1882 // composite the dimmed art behind every cell so it shows 1883 // through the whole UI: a blank cell becomes the art, while a 1884 // cell with a glyph keeps the glyph but takes the dimmed art as 1885 // its background (replacing whatever background it had, which 1886 // would otherwise obscure the art). 1887 if (cellIsBlank(dst.*)) { 1888 dst.* = src; 1889 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1890 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1891 } else { 1892 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1893 } 1894 } 1895 } 1896 } 1897 return out; 1898 } 1899 }; 1900