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