╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1766 const page_widget: Widget = switch (page.*) { 1767 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1768 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1769 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1770 }; 1771 1772 const demon_art = @embedFile("embed/demon.ans"); 1773 1774 // on the TUI/SSH, the page sits above a one-row footer showing the url 1775 var root = if (session.is_terminal) blk: { 1776 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1777 errdefer box.deinit(allocator); 1778 const bg_id = bg_blk: { 1779 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1780 errdefer background.deinit(allocator); 1781 const id = background.getFocus().id; 1782 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1783 break :bg_blk id; 1784 }; 1785 { 1786 var footer = try Footer.init(allocator, session); 1787 errdefer footer.deinit(allocator); 1788 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1789 } 1790 box.getFocus().child_id = bg_id; 1791 break :blk Widget{ .box = box }; 1792 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1793 errdefer root.deinit(allocator); 1794 1795 // input-owning views build their TextInputs in init — so reset the 1796 // focus-id -> *TextInput map here 1797 session.text_inputs.clearRetainingCapacity(); 1798 session.input_values.clearRetainingCapacity(); 1799 1800 try root.build(allocator, .{ 1801 .min_size = .{ .width = null, .height = 40 }, 1802 .max_size = .{ .width = 100, .height = null }, 1803 }, root.getFocus()); 1804 1805 return root; 1806 } 1807 1808 pub const Widget = union(enum) { 1809 text: wgt.Text(Widget), 1810 box: wgt.Box(Widget), 1811 text_box: wgt.TextBox(Widget), 1812 text_input: wgt.TextInput(Widget), 1813 scroll: wgt.Scroll(Widget), 1814 stack: wgt.Stack(Widget), 1815 flow_box: FlowBox, 1816 flow_box_scroll: FlowBox.Scroll, 1817 tag_flow: TagFlow, 1818 spacer: Spacer, 1819 center: Center, 1820 section_label: SectionLabel, 1821 submit_button: SubmitButton, 1822 background: AnsiBackground, 1823 home: Home.View, 1824 user: User.View, 1825 repo: Repo.View, 1826 quit: Quit.View, 1827 unauthorized: Unauthorized.View, 1828 title: Title.View, 1829 sub_title: SubTitle.View, 1830 home_header: Home.Header.View, 1831 user_header: User.Header.View, 1832 repo_header: Repo.Header.View, 1833 repo_files_header: Repo.Files.Header.View, 1834 repo_commits_header: Repo.Commits.Header.View, 1835 repo_issues_header: Repo.Issues.Header, 1836 repo_discussions_header: Repo.Discussions.Header, 1837 repo_files: Repo.Files.View, 1838 repo_commits: Repo.Commits.View, 1839 repo_refs: Repo.Refs.View, 1840 repo_issues: Repo.Issues.View, 1841 repo_discussions: Repo.Discussions.View, 1842 repo_events_header: Repo.Events.Header.View, 1843 repo_events: Repo.Events.View, 1844 repo_comment: Repo.Comment.Item, 1845 clone_url: CloneUrl.View, 1846 home_users: Home.Users.View, 1847 home_repos: Home.Repos.View, 1848 auth_tab: Home.Header.AuthTab.View, 1849 home_settings: Home.Settings.View, 1850 home_auth: Home.Auth.View, 1851 footer: Footer, 1852 1853 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1854 switch (self.*) { 1855 inline else => |*case| case.deinit(allocator), 1856 } 1857 } 1858 1859 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1860 switch (self.*) { 1861 inline else => |*case| try case.build(allocator, constraint, root_focus), 1862 } 1863 } 1864 1865 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1866 switch (self.*) { 1867 inline else => |*case| try case.input(allocator, key, root_focus), 1868 } 1869 } 1870 1871 pub fn clearGrid(self: *Widget) void { 1872 switch (self.*) { 1873 inline else => |*case| case.clearGrid(), 1874 } 1875 } 1876 1877 pub fn getGrid(self: Widget) ?Grid { 1878 switch (self) { 1879 inline else => |*case| return case.getGrid(), 1880 } 1881 } 1882 1883 pub fn getFocus(self: *Widget) *Focus { 1884 switch (self.*) { 1885 inline else => |*case| return case.getFocus(), 1886 } 1887 } 1888 }; 1889 1890 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1891 // `scroll` to keep it visible. 1892 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1893 const keys = box.children.keys(); 1894 if (keys.len == 0) return; 1895 const cur_id = box.getFocus().child_id orelse return; 1896 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1897 const last: isize = @intCast(keys.len - 1); 1898 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1899 if (next == @as(usize, @intCast(cur))) return; 1900 root_focus.setFocus(keys[next]); 1901 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1902 } 1903 1904 pub const FlowBox = struct { 1905 focus: *Focus, 1906 grid: ?Grid, 1907 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1908 // backs the per-item strings the text boxes borrow: each box's `content` 1909 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1910 // wholesale on each setItems so there's no per-string ownership to track. 1911 arena: std.heap.ArenaAllocator, 1912 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1913 // up/down can step by a row's worth of items. 1914 last_cols: usize, 1915 options: Options, 1916 1917 const border_rows: usize = 2; 1918 1919 pub const Options = struct { 1920 cell_width: usize = 40, 1921 cell_height: usize = 3, 1922 }; 1923 1924 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1925 return .{ 1926 .focus = try Focus.create(allocator, .container), 1927 .grid = null, 1928 .text_boxes = .empty, 1929 .arena = std.heap.ArenaAllocator.init(allocator), 1930 .last_cols = 1, 1931 .options = options, 1932 }; 1933 } 1934 1935 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1936 self.focus.destroy(allocator); 1937 if (self.grid) |*grid| { 1938 grid.deinit(); 1939 self.grid = null; 1940 } 1941 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1942 self.text_boxes.deinit(allocator); 1943 self.arena.deinit(); 1944 } 1945 1946 // one flow item: its display text plus an optional link. a non-empty `link` 1947 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1948 // the web renderer turns into an anchor. 1949 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1950 1951 // both the item text and the link are copied into the arena, so the caller's 1952 // slices needn't outlive this call. 1953 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1954 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1955 self.text_boxes.clearAndFree(allocator); 1956 1957 // the old text boxes (and their borrowed content/links) are gone now, so 1958 // the arena that backed those strings can be reclaimed. 1959 _ = self.arena.reset(.retain_capacity); 1960 const aa = self.arena.allocator(); 1961 1962 self.focus.clear(); 1963 self.focus.child_id = null; 1964 1965 for (items) |item| { 1966 const line = try aa.dupe(u8, item.text); 1967 1968 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1969 errdefer text_box.deinit(allocator); 1970 text_box.getFocus().focusable = true; 1971 1972 if (item.link.len > 0) { 1973 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1974 } 1975 1976 try self.text_boxes.append(allocator, text_box); 1977 } 1978 1979 if (self.text_boxes.items.len > 0) { 1980 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1981 } 1982 } 1983 1984 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1985 self.clearGrid(); 1986 self.focus.clear(); 1987 1988 const cell_width = self.options.cell_width; 1989 const max_width = constraint.max_size.width orelse cell_width; 1990 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1991 self.last_cols = cols; 1992 1993 const count = self.text_boxes.items.len; 1994 if (count == 0) return; 1995 const slot_height = self.options.cell_height + border_rows; 1996 if (slot_height == 0) return; 1997 1998 // build at the slot size so every text box fits a single grid cell 1999 for (self.text_boxes.items) |*tb| { 2000 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 2001 try tb.build(allocator, .{ 2002 .min_size = .{ .width = cell_width, .height = null }, 2003 .max_size = .{ .width = cell_width, .height = slot_height }, 2004 }, root_focus); 2005 } 2006 2007 const rows = (count + cols - 1) / cols; 2008 const content_height = rows * slot_height; 2009 const total_width = cols * cell_width; 2010 if (total_width == 0 or content_height == 0) return; 2011 2012 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 2013 errdefer grid.deinit(); 2014 2015 for (self.text_boxes.items, 0..) |*tb, i| { 2016 const tb_grid = tb.getGrid() orelse continue; 2017 const col = i % cols; 2018 const row = i / cols; 2019 const cell_x = col * cell_width; 2020 const cell_y = row * slot_height; 2021 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 2022 try grid.drawGrid(tb_grid, cell_x, cell_y); 2023 } 2024 2025 self.grid = grid; 2026 } 2027 2028 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2029 _ = self; 2030 _ = allocator; 2031 _ = key; 2032 _ = root_focus; 2033 } 2034 2035 pub fn clearGrid(self: *FlowBox) void { 2036 if (self.grid) |*grid| { 2037 grid.deinit(); 2038 self.grid = null; 2039 } 2040 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2041 } 2042 2043 pub fn getGrid(self: FlowBox) ?Grid { 2044 return self.grid; 2045 } 2046 2047 pub fn getFocus(self: *FlowBox) *Focus { 2048 return self.focus; 2049 } 2050 2051 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 2052 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 2053 const slot_height = self.options.cell_height + border_rows; 2054 const col = index % self.last_cols; 2055 const row = index / self.last_cols; 2056 return .{ 2057 .x = @intCast(col * self.options.cell_width), 2058 .y = @intCast(row * slot_height), 2059 .size = .{ .width = self.options.cell_width, .height = slot_height }, 2060 }; 2061 } 2062 2063 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 2064 for (self.text_boxes.items, 0..) |tb, i| { 2065 if (tb.box.focus.id == focus_id) return i; 2066 } 2067 return null; 2068 } 2069 2070 pub const Scroll = struct { 2071 scroll: wgt.Scroll(Widget), 2072 2073 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 2074 var layout_inner = try FlowBox.init(allocator, options); 2075 errdefer layout_inner.deinit(allocator); 2076 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 2077 errdefer scroll.deinit(allocator); 2078 return .{ .scroll = scroll }; 2079 } 2080 2081 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 2082 self.scroll.deinit(allocator); 2083 } 2084 2085 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 2086 self.scroll.x = 0; 2087 self.scroll.y = 0; 2088 try self.inner().setItems(allocator, items); 2089 } 2090 2091 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2092 try self.scroll.build(allocator, constraint, root_focus); 2093 } 2094 2095 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2096 _ = allocator; 2097 const in = self.inner(); 2098 const child_id = in.focus.child_id orelse return; 2099 const current_index = in.indexOfFocusId(child_id) orelse return; 2100 const count = in.text_boxes.items.len; 2101 if (count == 0) return; 2102 const cols = in.last_cols; 2103 const slot_height = in.options.cell_height + FlowBox.border_rows; 2104 2105 var index = current_index; 2106 switch (key) { 2107 .arrow_up => index -|= cols, 2108 .arrow_down => if (index + cols < count) { 2109 index += cols; 2110 }, 2111 .arrow_left => index -|= 1, 2112 .arrow_right => if (index + 1 < count) { 2113 index += 1; 2114 }, 2115 .home => index = 0, 2116 .end => index = count - 1, 2117 .page_up => { 2118 if (self.scroll.grid) |grid| if (slot_height > 0) { 2119 const rows_per_page = grid.size.height / slot_height; 2120 index -|= rows_per_page * cols; 2121 }; 2122 }, 2123 .page_down => { 2124 if (self.scroll.grid) |grid| if (slot_height > 0) { 2125 const rows_per_page = grid.size.height / slot_height; 2126 index = @min(index + rows_per_page * cols, count - 1); 2127 }; 2128 }, 2129 // scroll wheel moves the focused cell by a full row so the 2130 // viewport (via scrollToRect) follows in row-sized steps, 2131 // matching how a scroll wheel feels in a grid view. 2132 .mouse => |mouse| switch (mouse.action) { 2133 .scroll => |dir| switch (dir) { 2134 .up => index -|= cols, 2135 .down => if (index + cols < count) { 2136 index += cols; 2137 }, 2138 }, 2139 else => {}, 2140 }, 2141 else => {}, 2142 } 2143 2144 if (index != current_index) { 2145 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 2146 if (in.cellRect(index)) |rect| { 2147 self.scroll.scrollToRect(rect); 2148 } 2149 } 2150 } 2151 2152 pub fn clearGrid(self: *Scroll) void { 2153 self.scroll.clearGrid(); 2154 } 2155 2156 pub fn getGrid(self: Scroll) ?Grid { 2157 return self.scroll.getGrid(); 2158 } 2159 2160 pub fn getFocus(self: *Scroll) *Focus { 2161 return self.scroll.getFocus(); 2162 } 2163 2164 pub fn getSelectedIndex(self: Scroll) ?usize { 2165 const in = self.scroll.child.flow_box; 2166 const child_id = in.focus.child_id orelse return null; 2167 return in.indexOfFocusId(child_id); 2168 } 2169 2170 fn inner(self: *Scroll) *FlowBox { 2171 return &self.scroll.child.flow_box; 2172 } 2173 }; 2174 }; 2175 2176 // a left-to-right flow of variable-width focusable items that wraps at the 2177 // available width, like words in a paragraph. unlike FlowBox there is no cell 2178 // grid: each item is as wide as its text. selection movement is driven by the 2179 // owning view via indexOfFocusId/verticalNeighbor. 2180 pub const TagFlow = struct { 2181 focus: *Focus, 2182 grid: ?Grid, 2183 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 2184 // last build's item rects (content space), for vertical navigation and 2185 // scroll-into-view. 2186 rects: std.ArrayList(layout.IRect), 2187 // backs the per-item strings the text boxes borrow. 2188 arena: std.heap.ArenaAllocator, 2189 2190 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 2191 2192 pub fn init(allocator: std.mem.Allocator) !TagFlow { 2193 return .{ 2194 .focus = try Focus.create(allocator, .container), 2195 .grid = null, 2196 .text_boxes = .empty, 2197 .rects = .empty, 2198 .arena = std.heap.ArenaAllocator.init(allocator), 2199 }; 2200 } 2201 2202 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 2203 self.focus.destroy(allocator); 2204 if (self.grid) |*grid| { 2205 grid.deinit(); 2206 self.grid = null; 2207 } 2208 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2209 self.text_boxes.deinit(allocator); 2210 self.rects.deinit(allocator); 2211 self.arena.deinit(); 2212 } 2213 2214 // both the item text and the link are copied into the arena, so the caller's 2215 // slices needn't outlive this call. 2216 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 2217 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2218 self.text_boxes.clearAndFree(allocator); 2219 2220 _ = self.arena.reset(.retain_capacity); 2221 const aa = self.arena.allocator(); 2222 2223 self.focus.clear(); 2224 self.focus.child_id = null; 2225 2226 for (items) |item| { 2227 const line = try aa.dupe(u8, item.text); 2228 2229 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2230 errdefer text_box.deinit(allocator); 2231 text_box.getFocus().focusable = true; 2232 2233 if (item.link.len > 0) { 2234 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 2235 } 2236 2237 try self.text_boxes.append(allocator, text_box); 2238 } 2239 2240 if (self.text_boxes.items.len > 0) { 2241 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 2242 } 2243 } 2244 2245 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2246 self.clearGrid(); 2247 self.focus.clear(); 2248 2249 if (self.text_boxes.items.len == 0) return; 2250 const max_width = constraint.max_size.width orelse 80; 2251 if (max_width == 0) return; 2252 2253 // lay the items out left to right, wrapping to a new row when the next 2254 // one wouldn't fit. 2255 self.rects.clearRetainingCapacity(); 2256 var x: usize = 0; 2257 var y: usize = 0; 2258 var row_height: usize = 0; 2259 var total_width: usize = 0; 2260 for (self.text_boxes.items) |*tb| { 2261 try tb.build(allocator, .{ 2262 .min_size = .{ .width = null, .height = null }, 2263 .max_size = .{ .width = max_width, .height = null }, 2264 }, root_focus); 2265 const tb_grid_maybe = tb.getGrid(); 2266 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 2267 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 2268 if (x > 0 and x + w > max_width) { 2269 x = 0; 2270 y += row_height; 2271 row_height = 0; 2272 } 2273 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 2274 if (h > row_height) row_height = h; 2275 x += w; 2276 if (x > total_width) total_width = x; 2277 } 2278 2279 const total_height = y + row_height; 2280 if (total_width == 0 or total_height == 0) return; 2281 2282 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 2283 errdefer grid.deinit(); 2284 2285 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 2286 const tb_grid = tb.getGrid() orelse continue; 2287 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2288 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2289 } 2290 2291 self.grid = grid; 2292 } 2293 2294 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2295 _ = self; 2296 _ = allocator; 2297 _ = key; 2298 _ = root_focus; 2299 } 2300 2301 pub fn clearGrid(self: *TagFlow) void { 2302 if (self.grid) |*grid| { 2303 grid.deinit(); 2304 self.grid = null; 2305 } 2306 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2307 } 2308 2309 pub fn getGrid(self: TagFlow) ?Grid { 2310 return self.grid; 2311 } 2312 2313 pub fn getFocus(self: *TagFlow) *Focus { 2314 return self.focus; 2315 } 2316 2317 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2318 for (self.text_boxes.items, 0..) |tb, i| { 2319 if (tb.box.focus.id == focus_id) return i; 2320 } 2321 return null; 2322 } 2323 2324 // the first item of the row below (when `downward`) or above, or null at 2325 // the flow's edge. items are laid out in order, so y is nondecreasing. 2326 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2327 const rects = self.rects.items; 2328 if (index >= rects.len) return null; 2329 const cur_y = rects[index].y; 2330 if (downward) { 2331 for (rects[index + 1 ..], index + 1..) |r, i| { 2332 if (r.y > cur_y) return i; 2333 } 2334 return null; 2335 } 2336 // walk the earlier rows; the last one visited is the row above. 2337 var prev_first: ?usize = null; 2338 var i: usize = 0; 2339 while (i < rects.len and rects[i].y < cur_y) { 2340 prev_first = i; 2341 const y = rects[i].y; 2342 while (i < rects.len and rects[i].y == y) i += 1; 2343 } 2344 return prev_first; 2345 } 2346 }; 2347 2348 // an invisible widget that fills the horizontal space granted by its parent. 2349 // used inside Box(horiz) with a min_size so the box reserves space for the 2350 // children that follow, pushing them to the right. 2351 pub const Spacer = struct { 2352 focus: *Focus, 2353 grid: ?Grid, 2354 2355 pub fn init(allocator: std.mem.Allocator) !Spacer { 2356 return .{ 2357 .focus = try Focus.create(allocator, .container), 2358 .grid = null, 2359 }; 2360 } 2361 2362 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2363 self.focus.destroy(allocator); 2364 if (self.grid) |*grid| { 2365 grid.deinit(); 2366 self.grid = null; 2367 } 2368 } 2369 2370 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2371 _ = root_focus; 2372 self.clearGrid(); 2373 const width = constraint.max_size.width orelse return; 2374 if (width == 0) return; 2375 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2376 } 2377 2378 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2379 _ = self; 2380 _ = allocator; 2381 _ = key; 2382 _ = root_focus; 2383 } 2384 2385 pub fn clearGrid(self: *Spacer) void { 2386 if (self.grid) |*grid| { 2387 grid.deinit(); 2388 self.grid = null; 2389 } 2390 } 2391 2392 pub fn getGrid(self: Spacer) ?Grid { 2393 return self.grid; 2394 } 2395 2396 pub fn getFocus(self: *Spacer) *Focus { 2397 return self.focus; 2398 } 2399 }; 2400 2401 // a one-row, non-focusable status bar showing the current page's url 2402 pub const Footer = struct { 2403 focus: *Focus, 2404 grid: ?Grid, 2405 session: *Session, 2406 arena: std.heap.ArenaAllocator, 2407 url: []const u8 = "", 2408 url_width: usize = 0, 2409 2410 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2411 return .{ 2412 .focus = try Focus.create(allocator, .container), 2413 .grid = null, 2414 .session = session, 2415 .arena = std.heap.ArenaAllocator.init(allocator), 2416 }; 2417 } 2418 2419 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2420 self.focus.destroy(allocator); 2421 if (self.grid) |*grid| grid.deinit(); 2422 self.arena.deinit(); 2423 } 2424 2425 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2426 _ = root_focus; 2427 self.clearGrid(); 2428 const width = constraint.max_size.width orelse return; 2429 if (width == 0) return; 2430 2431 _ = self.arena.reset(.retain_capacity); 2432 self.url = ""; 2433 self.url_width = 0; 2434 const aa = self.arena.allocator(); 2435 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2436 const text = terminalWebUrl(aa, self.session, path) catch return; 2437 self.url = text; 2438 2439 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2440 errdefer grid.deinit(); 2441 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2442 var i: usize = 0; 2443 while (utf8.nextCodepointSlice()) |ch| { 2444 if (i == width) break; 2445 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2446 i += 1; 2447 } 2448 self.url_width = i; 2449 self.grid = grid; 2450 } 2451 2452 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2453 _ = allocator; 2454 switch (key) { 2455 .mouse => |mouse| { 2456 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2457 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2458 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2459 }, 2460 else => {}, 2461 } 2462 } 2463 2464 pub fn clearGrid(self: *Footer) void { 2465 if (self.grid) |*grid| { 2466 grid.deinit(); 2467 self.grid = null; 2468 } 2469 } 2470 2471 pub fn getGrid(self: Footer) ?Grid { 2472 return self.grid; 2473 } 2474 2475 pub fn getFocus(self: *Footer) *Focus { 2476 return self.focus; 2477 } 2478 }; 2479 2480 // a single-child wrapper that builds the child at its natural size and 2481 // positions its grid in the middle of the area granted by the parent 2482 // a bordered label, indented past the content it precedes 2483 pub const SectionLabel = struct { 2484 box: wgt.Box(Widget), 2485 2486 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2487 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2488 errdefer box.deinit(allocator); 2489 2490 { 2491 var gap = try wgt.Text(Widget).init(allocator, " "); 2492 errdefer gap.deinit(allocator); 2493 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2494 } 2495 2496 { 2497 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2498 errdefer tb.deinit(allocator); 2499 tb.getFocus().focusable = true; 2500 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2501 } 2502 2503 box.getFocus().child_id = box.children.keys()[1]; 2504 return .{ .box = box }; 2505 } 2506 2507 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2508 self.box.deinit(allocator); 2509 } 2510 2511 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2512 try self.box.build(allocator, constraint, root_focus); 2513 } 2514 2515 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2516 _ = self; 2517 _ = allocator; 2518 _ = key; 2519 _ = root_focus; 2520 } 2521 2522 pub fn clearGrid(self: *SectionLabel) void { 2523 self.box.clearGrid(); 2524 } 2525 2526 pub fn getGrid(self: SectionLabel) ?Grid { 2527 return self.box.getGrid(); 2528 } 2529 2530 pub fn getFocus(self: *SectionLabel) *Focus { 2531 return self.box.getFocus(); 2532 } 2533 }; 2534 2535 // a submit button, indented past its form's content. the web overlay POSTs 2536 // the button's form; terminal hosts detect presses through `buttonId`. 2537 pub const SubmitButton = struct { 2538 box: wgt.Box(Widget), 2539 2540 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2541 return initLabeled(allocator, "submit"); 2542 } 2543 2544 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2545 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2546 errdefer box.deinit(allocator); 2547 2548 { 2549 var gap = try wgt.Text(Widget).init(allocator, " "); 2550 errdefer gap.deinit(allocator); 2551 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2552 } 2553 2554 { 2555 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2556 errdefer button.deinit(allocator); 2557 button.getFocus().focusable = true; 2558 // the renderer distinguishes plain clickables from buttons that 2559 // should POST to a server route by this kind. 2560 button.getFocus().kind = .{ .custom = "submit" }; 2561 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2562 } 2563 2564 box.getFocus().child_id = box.children.keys()[1]; 2565 return .{ .box = box }; 2566 } 2567 2568 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2569 self.box.deinit(allocator); 2570 } 2571 2572 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2573 try self.box.build(allocator, constraint, root_focus); 2574 } 2575 2576 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2577 _ = self; 2578 _ = allocator; 2579 _ = key; 2580 _ = root_focus; 2581 } 2582 2583 pub fn clearGrid(self: *SubmitButton) void { 2584 self.box.clearGrid(); 2585 } 2586 2587 pub fn getGrid(self: SubmitButton) ?Grid { 2588 return self.box.getGrid(); 2589 } 2590 2591 pub fn getFocus(self: *SubmitButton) *Focus { 2592 return self.box.getFocus(); 2593 } 2594 2595 // the button's focus id, for click hit-testing 2596 pub fn buttonId(self: *const SubmitButton) usize { 2597 return self.box.children.keys()[1]; 2598 } 2599 }; 2600 2601 pub const Center = struct { 2602 focus: *Focus, 2603 grid: ?Grid, 2604 child: *Widget, 2605 2606 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2607 const child = try allocator.create(Widget); 2608 errdefer allocator.destroy(child); 2609 child.* = child_widget; 2610 return .{ 2611 .focus = try Focus.create(allocator, .container), 2612 .grid = null, 2613 .child = child, 2614 }; 2615 } 2616 2617 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2618 self.focus.destroy(allocator); 2619 if (self.grid) |*grid| { 2620 grid.deinit(); 2621 self.grid = null; 2622 } 2623 self.child.deinit(allocator); 2624 allocator.destroy(self.child); 2625 } 2626 2627 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2628 self.clearGrid(); 2629 self.getFocus().clear(); 2630 2631 // build the child without forcing it to fill min_size; it sizes 2632 // itself to its content within the available max. 2633 try self.child.build(allocator, .{ 2634 .min_size = .{ .width = null, .height = null }, 2635 .max_size = constraint.max_size, 2636 }, root_focus); 2637 2638 const child_grid = self.child.getGrid() orelse return; 2639 2640 // prefer max when given; otherwise grow to max(min, child) so that 2641 // when only a min is set (e.g. the wasm path passes viewport rows 2642 // as min_height with no max), we can still vertically center while 2643 // letting taller content extend past the min. 2644 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; 2645 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; 2646 if (width == 0 or height == 0) return; 2647 2648 const offset_x: usize = (width -| child_grid.size.width) / 2; 2649 const offset_y: usize = (height -| child_grid.size.height) / 2; 2650 2651 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2652 errdefer grid.deinit(); 2653 try grid.drawGrid(child_grid, offset_x, offset_y); 2654 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2655 self.getFocus().child_id = self.child.getFocus().id; 2656 2657 self.grid = grid; 2658 } 2659 2660 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2661 try self.child.input(allocator, key, root_focus); 2662 } 2663 2664 pub fn clearGrid(self: *Center) void { 2665 if (self.grid) |*grid| { 2666 grid.deinit(); 2667 self.grid = null; 2668 } 2669 self.child.clearGrid(); 2670 } 2671 2672 pub fn getGrid(self: Center) ?Grid { 2673 return self.grid; 2674 } 2675 2676 pub fn getFocus(self: *Center) *Focus { 2677 return self.focus; 2678 } 2679 }; 2680 2681 // renders truecolor ANSI art 2682 pub const AnsiArt = struct { 2683 focus: *Focus, 2684 grid: ?Grid, 2685 content: []const u8, 2686 2687 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2688 return .{ 2689 .focus = try Focus.create(allocator, .container), 2690 .grid = null, 2691 .content = content, 2692 }; 2693 } 2694 2695 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2696 self.focus.destroy(allocator); 2697 if (self.grid) |*grid| { 2698 grid.deinit(); 2699 self.grid = null; 2700 } 2701 } 2702 2703 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2704 _ = root_focus; 2705 self.clearGrid(); 2706 2707 // parse into rows of cells, then copy into a rectangular grid 2708 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2709 defer { 2710 for (rows.items) |*row| row.deinit(allocator); 2711 rows.deinit(allocator); 2712 } 2713 var row: std.ArrayList(Grid.Cell) = .empty; 2714 errdefer row.deinit(allocator); 2715 2716 var style: Grid.Style = .{}; 2717 var width: usize = 0; 2718 const content = self.content; 2719 var i: usize = 0; 2720 while (i < content.len) { 2721 const byte = content[i]; 2722 if (byte == '\n') { 2723 width = @max(width, row.items.len); 2724 try rows.append(allocator, row); 2725 row = .empty; 2726 i += 1; 2727 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2728 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2729 var j = i + 2; 2730 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2731 if (j >= content.len) { 2732 i = content.len; // malformed trailing escape; stop 2733 } else { 2734 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2735 i = j + 1; 2736 } 2737 } else { 2738 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2739 const end = @min(content.len, i + len); 2740 const rune = content[i..end]; 2741 const transparent = end - i == 1 and rune[0] == ' ' and 2742 style.fg == null and style.bg == null and !style.inverted; 2743 try row.append(allocator, .{ 2744 .rune = if (transparent) null else rune, 2745 .style = style, 2746 }); 2747 i = end; 2748 } 2749 } 2750 // a trailing row with content but no closing newline 2751 if (row.items.len > 0) { 2752 width = @max(width, row.items.len); 2753 try rows.append(allocator, row); 2754 } else { 2755 row.deinit(allocator); 2756 } 2757 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2758 2759 const height = rows.items.len; 2760 if (width == 0 or height == 0) return; 2761 2762 const clamped_w = @min(width, constraint.max_size.width orelse width); 2763 const clamped_h = @min(height, constraint.max_size.height orelse height); 2764 2765 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2766 errdefer grid.deinit(); 2767 for (rows.items[0..clamped_h], 0..) |r, y| { 2768 const n = @min(r.items.len, clamped_w); 2769 for (r.items[0..n], 0..) |cell, x| { 2770 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2771 } 2772 } 2773 self.grid = grid; 2774 } 2775 2776 pub fn clearGrid(self: *AnsiArt) void { 2777 if (self.grid) |*grid| { 2778 grid.deinit(); 2779 self.grid = null; 2780 } 2781 } 2782 2783 pub fn getGrid(self: AnsiArt) ?Grid { 2784 return self.grid; 2785 } 2786 2787 pub fn getFocus(self: *AnsiArt) *Focus { 2788 return self.focus; 2789 } 2790 2791 fn applySgr(style: *Grid.Style, params: []const u8) void { 2792 var nums: [16]u32 = undefined; 2793 var n: usize = 0; 2794 var it = std.mem.splitScalar(u8, params, ';'); 2795 while (it.next()) |tok| { 2796 if (n >= nums.len) break; 2797 // an empty parameter (e.g. bare "\x1b[m") means 0 2798 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2799 n += 1; 2800 } 2801 if (n == 0) { 2802 style.* = .{}; 2803 return; 2804 } 2805 var i: usize = 0; 2806 while (i < n) : (i += 1) { 2807 switch (nums[i]) { 2808 0 => style.* = .{}, 2809 7 => style.inverted = true, 2810 27 => style.inverted = false, 2811 39 => style.fg = null, 2812 49 => style.bg = null, 2813 38, 48 => { 2814 // truecolor form: 38;2;r;g;b — anything else is ignored 2815 if (i + 4 < n and nums[i + 1] == 2) { 2816 const c = Grid.Color{ 2817 .r = @truncate(nums[i + 2]), 2818 .g = @truncate(nums[i + 3]), 2819 .b = @truncate(nums[i + 4]), 2820 }; 2821 if (nums[i] == 38) style.fg = c else style.bg = c; 2822 i += 4; 2823 } 2824 }, 2825 else => {}, 2826 } 2827 } 2828 } 2829 }; 2830 2831 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2832 pub const AnsiBackground = struct { 2833 grid: ?Grid, 2834 child: *Widget, 2835 art: AnsiArt, 2836 session: *Session, 2837 2838 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2839 var cw = child_widget; 2840 const child = allocator.create(Widget) catch |e| { 2841 cw.deinit(allocator); 2842 return e; 2843 }; 2844 child.* = cw; 2845 errdefer { 2846 child.deinit(allocator); 2847 allocator.destroy(child); 2848 } 2849 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2850 } 2851 2852 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2853 if (self.grid) |*grid| grid.deinit(); 2854 self.art.deinit(allocator); 2855 self.child.deinit(allocator); 2856 allocator.destroy(self.child); 2857 } 2858 2859 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2860 self.clearGrid(); 2861 2862 // make the wrapped page fill the whole available area 2863 try self.child.build(allocator, .{ 2864 .min_size = .{ 2865 .width = constraint.max_size.width orelse constraint.min_size.width, 2866 .height = constraint.max_size.height orelse constraint.min_size.height, 2867 }, 2868 .max_size = constraint.max_size, 2869 }, root_focus); 2870 2871 if (self.session.data.enable_ansi) { 2872 if (self.child.getGrid()) |fg| { 2873 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2874 } 2875 } 2876 } 2877 2878 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2879 try self.child.input(allocator, key, root_focus); 2880 } 2881 2882 pub fn clearGrid(self: *AnsiBackground) void { 2883 if (self.grid) |*grid| { 2884 grid.deinit(); 2885 self.grid = null; 2886 } 2887 self.child.clearGrid(); 2888 self.art.clearGrid(); 2889 } 2890 2891 pub fn getGrid(self: AnsiBackground) ?Grid { 2892 return self.grid orelse self.child.getGrid(); 2893 } 2894 2895 pub fn getFocus(self: *AnsiBackground) *Focus { 2896 return self.child.getFocus(); 2897 } 2898 2899 // which top corner the art hugs. 2900 const ArtAnchor = enum { top_left, top_right }; 2901 2902 // a foreground cell counts as blank if it's empty or a bare space without styling 2903 fn cellIsBlank(cell: Grid.Cell) bool { 2904 if (cell.rune) |rune| { 2905 return std.mem.eql(u8, rune, " ") and 2906 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2907 } 2908 return true; 2909 } 2910 2911 // the art is dimmed to this fraction of its brightness so foreground text 2912 // stays legible over it 2913 const art_brightness = 45; // percent 2914 2915 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2916 const c = color orelse return null; 2917 return .{ 2918 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2919 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2920 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2921 }; 2922 } 2923 2924 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2925 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2926 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2927 // snap such channels clear of that range so the backdrop doesn't blink or 2928 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2929 // actually render truecolor. 2930 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2931 const c = color orelse return null; 2932 const snap = struct { 2933 fn f(v: u8) u8 { 2934 return if (v >= 1 and v <= 9) 10 else v; 2935 } 2936 }.f; 2937 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2938 } 2939 2940 // composites ANSI art behind a foreground grid 2941 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2942 art.clearGrid(); 2943 try art.build(allocator, .{ 2944 .min_size = .{ .width = null, .height = null }, 2945 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2946 }, root_focus); 2947 2948 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2949 errdefer out.deinit(); 2950 2951 if (art.getGrid()) |art_grid| { 2952 const anchor_x = switch (anchor) { 2953 .top_left => 0, 2954 .top_right => foreground.size.width -| art_grid.size.width, 2955 }; 2956 for (0..art_grid.size.height) |y| { 2957 for (0..art_grid.size.width) |x| { 2958 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2959 if (src.rune == null) continue; 2960 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2961 const dst = &out.cells.items[idx]; 2962 // composite the dimmed art behind every cell so it shows 2963 // through the whole UI: a blank cell becomes the art, while a 2964 // cell with a glyph keeps the glyph but takes the dimmed art as 2965 // its background (replacing whatever background it had, which 2966 // would otherwise obscure the art). 2967 if (cellIsBlank(dst.*)) { 2968 dst.* = src; 2969 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2970 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2971 } else { 2972 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2973 dst.style.bg = background_maybe; 2974 // change the color of text overlaying ANSI art to ensure 2975 // it is readable. don't do this in the web UI, since we 2976 // have direct control of the theme there (also, inner 2977 // scroll panes allow text to move without a re-render 2978 // by the TUI, which would break this). also don't do this 2979 // if the text is explicitly styled. 2980 if (is_terminal and dst.style.fg == null) { 2981 if (background_maybe) |background| { 2982 // use near-black and near-white, which is easier on the eyes 2983 // while retaining high contrast 2984 const luminance = (@as(u32, background.r) * 299 + 2985 @as(u32, background.g) * 587 + 2986 @as(u32, background.b) * 114) / 1000; 2987 dst.style.fg = if (luminance >= 128) 2988 .{ .r = 16, .g = 16, .b = 16 } 2989 else 2990 .{ .r = 240, .g = 240, .b = 240 }; 2991 } 2992 } 2993 } 2994 } 2995 } 2996 } 2997 return out; 2998 } 2999 }; 3000