╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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