╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1422 } 1423 1424 pub fn getGrid(self: Center) ?Grid { 1425 return self.grid; 1426 } 1427 1428 pub fn getFocus(self: *Center) *Focus { 1429 return self.focus; 1430 } 1431 }; 1432 1433 // renders truecolor ANSI art 1434 pub const AnsiArt = struct { 1435 focus: *Focus, 1436 grid: ?Grid, 1437 content: []const u8, 1438 1439 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1440 return .{ 1441 .focus = try Focus.create(allocator, .container), 1442 .grid = null, 1443 .content = content, 1444 }; 1445 } 1446 1447 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1448 self.focus.destroy(allocator); 1449 if (self.grid) |*grid| { 1450 grid.deinit(); 1451 self.grid = null; 1452 } 1453 } 1454 1455 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1456 _ = root_focus; 1457 self.clearGrid(); 1458 1459 // parse into rows of cells, then copy into a rectangular grid 1460 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1461 defer { 1462 for (rows.items) |*row| row.deinit(allocator); 1463 rows.deinit(allocator); 1464 } 1465 var row: std.ArrayList(Grid.Cell) = .empty; 1466 errdefer row.deinit(allocator); 1467 1468 var style: Grid.Style = .{}; 1469 var width: usize = 0; 1470 const content = self.content; 1471 var i: usize = 0; 1472 while (i < content.len) { 1473 const byte = content[i]; 1474 if (byte == '\n') { 1475 width = @max(width, row.items.len); 1476 try rows.append(allocator, row); 1477 row = .empty; 1478 i += 1; 1479 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1480 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1481 var j = i + 2; 1482 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1483 if (j >= content.len) { 1484 i = content.len; // malformed trailing escape; stop 1485 } else { 1486 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1487 i = j + 1; 1488 } 1489 } else { 1490 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1491 const end = @min(content.len, i + len); 1492 const rune = content[i..end]; 1493 const transparent = end - i == 1 and rune[0] == ' ' and 1494 style.fg == null and style.bg == null and !style.inverted; 1495 try row.append(allocator, .{ 1496 .rune = if (transparent) null else rune, 1497 .style = style, 1498 }); 1499 i = end; 1500 } 1501 } 1502 // a trailing row with content but no closing newline 1503 if (row.items.len > 0) { 1504 width = @max(width, row.items.len); 1505 try rows.append(allocator, row); 1506 } else { 1507 row.deinit(allocator); 1508 } 1509 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1510 1511 const height = rows.items.len; 1512 if (width == 0 or height == 0) return; 1513 1514 const clamped_w = @min(width, constraint.max_size.width orelse width); 1515 const clamped_h = @min(height, constraint.max_size.height orelse height); 1516 1517 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1518 errdefer grid.deinit(); 1519 for (rows.items[0..clamped_h], 0..) |r, y| { 1520 const n = @min(r.items.len, clamped_w); 1521 for (r.items[0..n], 0..) |cell, x| { 1522 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1523 } 1524 } 1525 self.grid = grid; 1526 } 1527 1528 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1529 _ = self; 1530 _ = allocator; 1531 _ = key; 1532 _ = root_focus; 1533 } 1534 1535 pub fn clearGrid(self: *AnsiArt) void { 1536 if (self.grid) |*grid| { 1537 grid.deinit(); 1538 self.grid = null; 1539 } 1540 } 1541 1542 pub fn getGrid(self: AnsiArt) ?Grid { 1543 return self.grid; 1544 } 1545 1546 pub fn getFocus(self: *AnsiArt) *Focus { 1547 return self.focus; 1548 } 1549 1550 fn applySgr(style: *Grid.Style, params: []const u8) void { 1551 var nums: [16]u32 = undefined; 1552 var n: usize = 0; 1553 var it = std.mem.splitScalar(u8, params, ';'); 1554 while (it.next()) |tok| { 1555 if (n >= nums.len) break; 1556 // an empty parameter (e.g. bare "\x1b[m") means 0 1557 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1558 n += 1; 1559 } 1560 if (n == 0) { 1561 style.* = .{}; 1562 return; 1563 } 1564 var i: usize = 0; 1565 while (i < n) : (i += 1) { 1566 switch (nums[i]) { 1567 0 => style.* = .{}, 1568 7 => style.inverted = true, 1569 27 => style.inverted = false, 1570 39 => style.fg = null, 1571 49 => style.bg = null, 1572 38, 48 => { 1573 // truecolor form: 38;2;r;g;b — anything else is ignored 1574 if (i + 4 < n and nums[i + 1] == 2) { 1575 const c = Grid.Color{ 1576 .r = @truncate(nums[i + 2]), 1577 .g = @truncate(nums[i + 3]), 1578 .b = @truncate(nums[i + 4]), 1579 }; 1580 if (nums[i] == 38) style.fg = c else style.bg = c; 1581 i += 4; 1582 } 1583 }, 1584 else => {}, 1585 } 1586 } 1587 } 1588 }; 1589 1590 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1591 pub const AnsiBackground = struct { 1592 grid: ?Grid, 1593 child: *Widget, 1594 art: AnsiArt, 1595 session: *Session, 1596 1597 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1598 var cw = child_widget; 1599 const child = allocator.create(Widget) catch |e| { 1600 cw.deinit(allocator); 1601 return e; 1602 }; 1603 child.* = cw; 1604 errdefer { 1605 child.deinit(allocator); 1606 allocator.destroy(child); 1607 } 1608 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1609 } 1610 1611 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1612 if (self.grid) |*grid| grid.deinit(); 1613 self.art.deinit(allocator); 1614 self.child.deinit(allocator); 1615 allocator.destroy(self.child); 1616 } 1617 1618 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1619 self.clearGrid(); 1620 1621 // make the wrapped page fill the whole available area 1622 try self.child.build(allocator, .{ 1623 .min_size = .{ 1624 .width = constraint.max_size.width orelse constraint.min_size.width, 1625 .height = constraint.max_size.height orelse constraint.min_size.height, 1626 }, 1627 .max_size = constraint.max_size, 1628 }, root_focus); 1629 1630 if (self.session.data.enable_ansi) { 1631 if (self.child.getGrid()) |fg| { 1632 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1633 } 1634 } 1635 } 1636 1637 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1638 try self.child.input(allocator, key, root_focus); 1639 } 1640 1641 pub fn clearGrid(self: *AnsiBackground) void { 1642 if (self.grid) |*grid| { 1643 grid.deinit(); 1644 self.grid = null; 1645 } 1646 self.child.clearGrid(); 1647 self.art.clearGrid(); 1648 } 1649 1650 pub fn getGrid(self: AnsiBackground) ?Grid { 1651 return self.grid orelse self.child.getGrid(); 1652 } 1653 1654 pub fn getFocus(self: *AnsiBackground) *Focus { 1655 return self.child.getFocus(); 1656 } 1657 1658 // which top corner the art hugs. 1659 const ArtAnchor = enum { top_left, top_right }; 1660 1661 // a foreground cell counts as blank if it's empty or a bare space without styling 1662 fn cellIsBlank(cell: Grid.Cell) bool { 1663 if (cell.rune) |rune| { 1664 return std.mem.eql(u8, rune, " ") and 1665 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1666 } 1667 return true; 1668 } 1669 1670 // the art is dimmed to this fraction of its brightness so foreground text 1671 // stays legible over it 1672 const art_brightness = 45; // percent 1673 1674 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1675 const c = color orelse return null; 1676 return .{ 1677 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1678 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1679 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1680 }; 1681 } 1682 1683 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1684 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1685 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1686 // snap such channels clear of that range so the backdrop doesn't blink or 1687 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1688 // actually render truecolor. 1689 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1690 const c = color orelse return null; 1691 const snap = struct { 1692 fn f(v: u8) u8 { 1693 return if (v >= 1 and v <= 9) 10 else v; 1694 } 1695 }.f; 1696 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1697 } 1698 1699 // composites ANSI art behind a foreground grid 1700 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1701 art.clearGrid(); 1702 try art.build(allocator, .{ 1703 .min_size = .{ .width = null, .height = null }, 1704 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1705 }, root_focus); 1706 1707 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1708 errdefer out.deinit(); 1709 1710 if (art.getGrid()) |art_grid| { 1711 const anchor_x = switch (anchor) { 1712 .top_left => 0, 1713 .top_right => foreground.size.width -| art_grid.size.width, 1714 }; 1715 for (0..art_grid.size.height) |y| { 1716 for (0..art_grid.size.width) |x| { 1717 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1718 if (src.rune == null) continue; 1719 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1720 const dst = &out.cells.items[idx]; 1721 // composite the dimmed art behind every cell so it shows 1722 // through the whole UI: a blank cell becomes the art, while a 1723 // cell with a glyph keeps the glyph but takes the dimmed art as 1724 // its background (replacing whatever background it had, which 1725 // would otherwise obscure the art). 1726 if (cellIsBlank(dst.*)) { 1727 dst.* = src; 1728 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1729 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1730 } else { 1731 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1732 } 1733 } 1734 } 1735 } 1736 return out; 1737 } 1738 }; 1739