╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1493 style.* = .{}; 1494 return; 1495 } 1496 var i: usize = 0; 1497 while (i < n) : (i += 1) { 1498 switch (nums[i]) { 1499 0 => style.* = .{}, 1500 7 => style.inverted = true, 1501 27 => style.inverted = false, 1502 39 => style.fg = null, 1503 49 => style.bg = null, 1504 38, 48 => { 1505 // truecolor form: 38;2;r;g;b — anything else is ignored 1506 if (i + 4 < n and nums[i + 1] == 2) { 1507 const c = Grid.Color{ .rgb = .{ 1508 .r = @truncate(nums[i + 2]), 1509 .g = @truncate(nums[i + 3]), 1510 .b = @truncate(nums[i + 4]), 1511 } }; 1512 if (nums[i] == 38) style.fg = c else style.bg = c; 1513 i += 4; 1514 } 1515 }, 1516 else => {}, 1517 } 1518 } 1519 } 1520 }; 1521 1522 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1523 pub const AnsiBackground = struct { 1524 grid: ?Grid, 1525 child: *Widget, 1526 art: AnsiArt, 1527 session: *ui.Session, 1528 1529 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1530 var cw = child_widget; 1531 const child = allocator.create(Widget) catch |e| { 1532 cw.deinit(allocator); 1533 return e; 1534 }; 1535 child.* = cw; 1536 errdefer { 1537 child.deinit(allocator); 1538 allocator.destroy(child); 1539 } 1540 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1541 } 1542 1543 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1544 if (self.grid) |*grid| grid.deinit(); 1545 self.art.deinit(allocator); 1546 self.child.deinit(allocator); 1547 allocator.destroy(self.child); 1548 } 1549 1550 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1551 self.clearGrid(); 1552 1553 // make the wrapped page fill the whole available area 1554 try self.child.build(allocator, .{ 1555 .min_size = .{ 1556 .width = constraint.max_size.width orelse constraint.min_size.width, 1557 .height = constraint.max_size.height orelse constraint.min_size.height, 1558 }, 1559 .max_size = constraint.max_size, 1560 }, root_focus); 1561 1562 if (!self.session.data.enable_ansi) return; 1563 const foreground = self.child.getGrid() orelse return; 1564 self.art.content = self.session.data.ansi_art; 1565 try self.buildArt(allocator, foreground.size, root_focus); 1566 if (!self.session.is_terminal) return; 1567 const art_grid = self.art.getGrid() orelse return; 1568 self.grid = try artBehind(allocator, foreground, art_grid); 1569 } 1570 1571 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1572 try self.child.input(allocator, key, root_focus); 1573 } 1574 1575 pub fn clearGrid(self: *AnsiBackground) void { 1576 if (self.grid) |*grid| { 1577 grid.deinit(); 1578 self.grid = null; 1579 } 1580 self.child.clearGrid(); 1581 self.art.clearGrid(); 1582 } 1583 1584 pub fn getGrid(self: AnsiBackground) ?Grid { 1585 return self.grid orelse self.child.getGrid(); 1586 } 1587 1588 pub fn getFocus(self: *AnsiBackground) *Focus { 1589 return self.child.getFocus(); 1590 } 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.continuation) return false; 1595 if (cell.rune) |rune| { 1596 return rune == ' ' and 1597 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1598 } 1599 return true; 1600 } 1601 1602 // the art is dimmed to this fraction of its brightness so foreground text 1603 // stays legible over it 1604 const art_brightness = 35; // percent 1605 1606 // the art parser only emits truecolor, so a palette color never occurs 1607 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1608 const c = color orelse return null; 1609 return switch (c) { 1610 .rgb => |v| v, 1611 else => null, 1612 }; 1613 } 1614 1615 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1616 const v = artRgb(color) orelse return color; 1617 return .{ .rgb = .{ 1618 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1619 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1620 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1621 } }; 1622 } 1623 1624 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1625 try self.art.build(allocator, .{ 1626 .min_size = .{ .width = null, .height = null }, 1627 .max_size = .{ .width = size.width, .height = size.height }, 1628 }, root_focus); 1629 if (self.art.grid) |*grid| { 1630 for (grid.cells) |*cell| { 1631 cell.style.fg = dimColor(cell.style.fg); 1632 cell.style.bg = dimColor(cell.style.bg); 1633 } 1634 } 1635 } 1636 1637 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1638 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1639 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1640 // snap such channels clear of that range so the backdrop doesn't blink or 1641 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1642 // actually render truecolor. 1643 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1644 const v = artRgb(color) orelse return color; 1645 const snap = struct { 1646 fn f(c: u8) u8 { 1647 return if (c >= 1 and c <= 9) 10 else c; 1648 } 1649 }.f; 1650 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1651 } 1652 1653 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1654 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1655 dst.style.bg = background_maybe; 1656 // change unstyled text to contrast with the art behind it 1657 if (dst.style.fg == null) { 1658 if (artRgb(background_maybe)) |v| { 1659 // use near-black and near-white, which is easier on the eyes 1660 // while retaining high contrast 1661 const luminance = (@as(u32, v.r) * 299 + 1662 @as(u32, v.g) * 587 + 1663 @as(u32, v.b) * 114) / 1000; 1664 dst.style.fg = if (luminance >= 128) 1665 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1666 else 1667 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1668 } 1669 } 1670 } 1671 1672 // composites ANSI art behind a foreground grid 1673 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1674 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1675 errdefer out.deinit(); 1676 1677 const anchor_x = foreground.size.width -| art_grid.size.width; 1678 for (0..art_grid.size.height) |y| { 1679 for (0..art_grid.size.width) |x| { 1680 const src = (try art_grid.cell(x, y)).*; 1681 if (src.rune == null) continue; 1682 const dst = try out.cell(anchor_x + x, y); 1683 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1684 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1685 // blank cells take the art; occupied cells take its background 1686 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1687 dst.style = src.style; 1688 dst.style.fg = sgrSafe(dst.style.fg); 1689 dst.style.bg = sgrSafe(dst.style.bg); 1690 try out.setRune(anchor_x + x, y, src.rune); 1691 } else { 1692 applyArtBackground(dst, src); 1693 if (next_dst) |next| applyArtBackground(next, src); 1694 } 1695 } 1696 } 1697 return out; 1698 } 1699 }; 1700 1701 pub const CopyableText = struct { 1702 pub const Choice = struct { 1703 selector: []const u8 = "", 1704 text: []const u8, 1705 copyable_text: ?[]const u8 = null, 1706 label: []const u8 = "", 1707 bottom_label: []const u8 = "", 1708 }; 1709 1710 box: wgt.Box(Widget), 1711 session: *ui.Session, 1712 choices: []Choice, 1713 selected: usize = 0, 1714 1715 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1716 if (choices.len == 0) return error.MissingCopyableText; 1717 1718 const owned_choices = try allocator.dupe(Choice, choices); 1719 errdefer allocator.free(owned_choices); 1720 1721 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1722 errdefer box.deinit(allocator); 1723 1724 if (choices.len > 1) for (choices) |choice| { 1725 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1726 errdefer selector_box.deinit(allocator); 1727 selector_box.getFocus().mode = .all; 1728 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1729 }; 1730 1731 var text_input = try wgt.TextInput.init(allocator, .{ 1732 .border_style = .single, 1733 .round_corners = true, 1734 .label = choices[0].label, 1735 .bottom_label = choices[0].bottom_label, 1736 .read_only = true, 1737 .render_content = session.is_terminal, 1738 .visible_width = null, 1739 }); 1740 errdefer text_input.deinit(allocator); 1741 text_input.getFocus().mode = .all; 1742 try text_input.setContent(allocator, choices[0].text); 1743 box.getFocus().child_id = text_input.getFocus().id; 1744 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1745 1746 return .{ .box = box, .session = session, .choices = owned_choices }; 1747 } 1748 1749 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1750 const aa = session.page_arena.allocator(); 1751 var choices: [2]Choice = undefined; 1752 var count: usize = 0; 1753 switch (location) { 1754 .repo => |identity| { 1755 if (session.data.git_http_port) |port| { 1756 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1757 choices[count] = .{ 1758 .selector = "http", 1759 .text = url, 1760 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1761 .label = " clone ", 1762 }; 1763 count += 1; 1764 } 1765 if (session.data.git_ssh_port) |port| { 1766 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1767 choices[count] = .{ 1768 .selector = "ssh", 1769 .text = url, 1770 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1771 .label = " clone ", 1772 }; 1773 count += 1; 1774 } 1775 }, 1776 .fork => |f| if (session.data.git_ssh_port) |port| { 1777 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1778 choices[count] = .{ 1779 .text = url, 1780 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1781 .label = " clone ", 1782 }; 1783 count += 1; 1784 }, 1785 } 1786 if (count == 0) return null; 1787 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1788 } 1789 1790 pub fn minWidth(self: *const CopyableText) usize { 1791 var width = maxWidth(self.choices) + 2; 1792 if (self.choices.len > 1) { 1793 for (self.choices) |choice| width += choice.selector.len + 2; 1794 } 1795 return width; 1796 } 1797 1798 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1799 self.box.deinit(allocator); 1800 allocator.free(self.choices); 1801 } 1802 1803 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1804 self.clearGrid(); 1805 1806 if (self.choices.len > 1) { 1807 if (root_focus.grandchild_id) |id| { 1808 for (0..self.choices.len) |i| { 1809 if (id != self.selector(i).getFocus().id) continue; 1810 if (i != self.selected) try self.select(allocator, i); 1811 root_focus.setFocus(self.textInput().getFocus().id); 1812 break; 1813 } 1814 } 1815 1816 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1817 for (0..self.choices.len) |i| { 1818 self.selector(i).options.border_style = if (i == self.selected) 1819 (if (focused) .double else .single) 1820 else 1821 .hidden; 1822 } 1823 } 1824 self.textInput().options.border_style = .single; 1825 self.textInput().cursor = 0; 1826 self.textInput().scroll_offset = 0; 1827 try self.box.build(allocator, constraint, root_focus); 1828 1829 if (!self.session.is_terminal) { 1830 const aa = self.session.arena.allocator(); 1831 const id = self.textInput().getFocus().id; 1832 try self.session.text_inputs.put(aa, id, self.textInput()); 1833 } 1834 } 1835 1836 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1837 switch (key) { 1838 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1839 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1840 .enter => if (self.session.is_terminal) self.show(), 1841 .mouse => |mouse| { 1842 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1843 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1844 try self.select(allocator, i); 1845 root_focus.setFocus(self.textInput().getFocus().id); 1846 return; 1847 }; 1848 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1849 }, 1850 else => {}, 1851 } 1852 } 1853 1854 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1855 self.selected = selected; 1856 const choice = self.choices[selected]; 1857 const text_input = self.textInput(); 1858 text_input.options.label = choice.label; 1859 text_input.options.bottom_label = choice.bottom_label; 1860 try text_input.setContent(allocator, choice.text); 1861 } 1862 1863 fn show(self: *CopyableText) void { 1864 const choice = self.choices[self.selected]; 1865 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1866 } 1867 1868 fn textInput(self: *CopyableText) *wgt.TextInput { 1869 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1870 } 1871 1872 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1873 return &self.box.children.values()[index].widget.text_box; 1874 } 1875 1876 fn maxWidth(choices: []const Choice) usize { 1877 var width: usize = 0; 1878 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1879 return width; 1880 } 1881 1882 pub fn clearGrid(self: *CopyableText) void { 1883 self.box.clearGrid(); 1884 } 1885 1886 pub fn getGrid(self: CopyableText) ?Grid { 1887 return self.box.getGrid(); 1888 } 1889 1890 pub fn getFocus(self: *CopyableText) *Focus { 1891 return self.box.getFocus(); 1892 } 1893 }; 1894