╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
755 return null; 756 } 757 758 // the first item of the row below (when `downward`) or above, or null at 759 // the flow's edge. items are laid out in order, so y is nondecreasing. 760 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 761 const rects = self.rects.items; 762 if (index >= rects.len) return null; 763 const cur_y = rects[index].y; 764 if (downward) { 765 for (rects[index + 1 ..], index + 1..) |r, i| { 766 if (r.y > cur_y) return i; 767 } 768 return null; 769 } 770 // walk the earlier rows; the last one visited is the row above. 771 var prev_first: ?usize = null; 772 var i: usize = 0; 773 while (i < rects.len and rects[i].y < cur_y) { 774 prev_first = i; 775 const y = rects[i].y; 776 while (i < rects.len and rects[i].y == y) i += 1; 777 } 778 return prev_first; 779 } 780 }; 781 782 // a word-wrapped paragraph of styled runs. each line a link touches is a 783 // focusable piece, and a focused link draws all its pieces inverted. 784 pub const RichText = struct { 785 focus: *Focus, 786 grid: ?Grid, 787 // backs the decoded content and the link targets 788 arena: std.heap.ArenaAllocator, 789 runes: []const u21, 790 // each rune's span 791 span_of: []const usize, 792 styles: []const Grid.Style, 793 // each span's link index, null outside a link 794 span_links: []const ?usize, 795 links: []const []const u8, 796 // each link's piece focuses in line order, kept across builds so a link 797 // keeps its ids when the text rewraps 798 link_focuses: []std.ArrayList(*Focus), 799 lines: std.ArrayList(wgt.Line), 800 // scratch for each build's link pieces 801 pieces: std.ArrayList(Piece), 802 803 pub const Span = struct { 804 text: []const u8, 805 style: Grid.Style = .{}, 806 // the focus kind a click or enter follows; empty for plain text 807 link: []const u8 = "", 808 }; 809 810 const Piece = struct { 811 link: usize, 812 rect: layout.URect, 813 }; 814 815 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 816 var arena = std.heap.ArenaAllocator.init(allocator); 817 errdefer arena.deinit(); 818 const aa = arena.allocator(); 819 820 var runes: std.ArrayList(u21) = .empty; 821 var span_of: std.ArrayList(usize) = .empty; 822 const styles = try aa.alloc(Grid.Style, spans.len); 823 const span_links = try aa.alloc(?usize, spans.len); 824 var links: std.ArrayList([]const u8) = .empty; 825 for (spans, 0..) |span, i| { 826 styles[i] = span.style; 827 span_links[i] = null; 828 if (span.link.len > 0) { 829 // adjacent spans with the same target are one link 830 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 831 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 832 span_links[i] = links.items.len - 1; 833 } 834 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 835 while (utf8.nextCodepoint()) |codepoint| { 836 try runes.append(aa, codepoint); 837 try span_of.append(aa, i); 838 } 839 } 840 841 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 842 @memset(link_focuses, .empty); 843 844 return .{ 845 .focus = try Focus.create(allocator, .container), 846 .grid = null, 847 .arena = arena, 848 .runes = runes.items, 849 .span_of = span_of.items, 850 .styles = styles, 851 .span_links = span_links, 852 .links = links.items, 853 .link_focuses = link_focuses, 854 .lines = .empty, 855 .pieces = .empty, 856 }; 857 } 858 859 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 860 self.focus.destroy(allocator); 861 self.clearGrid(); 862 for (self.link_focuses) |*focuses| { 863 for (focuses.items) |focus| focus.destroy(allocator); 864 focuses.deinit(allocator); 865 } 866 self.pieces.deinit(allocator); 867 self.lines.deinit(allocator); 868 self.arena.deinit(); 869 } 870 871 // the url a terminal can open for a link: raw links to absolute urls 872 fn terminalLink(link: []const u8) ?[]const u8 { 873 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 874 const url = link[ui.raw_link_prefix.len..]; 875 return if (std.mem.startsWith(u8, url, "/")) null else url; 876 } 877 878 // the link one of whose pieces has focus id `id` 879 fn linkOf(self: *const RichText, id: ?usize) ?usize { 880 for (self.link_focuses, 0..) |focuses, l| { 881 for (focuses.items) |focus| if (focus.id == id) return l; 882 } 883 return null; 884 } 885 886 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 887 self.clearGrid(); 888 self.focus.clear(); 889 const max_width = constraint.max_size.width; 890 if (max_width == 0 or constraint.max_size.height == 0) return; 891 const focused_link = self.linkOf(root_focus.grandchild_id); 892 893 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 894 // trailing spaces are dropped so a wrapped link doesn't highlight them 895 for (self.lines.items) |*line| { 896 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 897 line.end -= 1; 898 line.width -= 1; 899 } 900 } 901 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 902 var width: usize = 1; 903 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 904 if (max_width) |w| width = @min(width, w); 905 906 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 907 errdefer grid.deinit(); 908 self.pieces.clearRetainingCapacity(); 909 for (self.lines.items[0..visible], 0..) |line, y| { 910 var x: usize = 0; 911 for (self.runes[line.start..line.end], line.start..) |rune, i| { 912 const rune_width = xitui.width.cellWidth(rune); 913 if (x + rune_width > width) break; 914 const link = self.span_links[self.span_of[i]]; 915 const cell = try grid.cell(x, y); 916 cell.style = self.styles[self.span_of[i]]; 917 if (link) |l| { 918 cell.link = terminalLink(self.links[l]); 919 if (l == focused_link) cell.style.inverted = true; 920 // extend this line's piece of the link, or start one 921 const items = self.pieces.items; 922 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 923 items[items.len - 1].rect.size.width += rune_width; 924 } else { 925 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 926 } 927 } 928 try grid.setRune(x, y, rune); 929 x += rune_width; 930 } 931 } 932 933 // a link's pieces are consecutive, since its runes are 934 var piece_index: usize = 0; 935 for (self.pieces.items, 0..) |piece, i| { 936 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 937 const focuses = &self.link_focuses[piece.link]; 938 if (piece_index == focuses.items.len) { 939 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 940 errdefer focus.destroy(allocator); 941 focus.mode = .all; 942 try focuses.append(allocator, focus); 943 } 944 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 945 } 946 947 // focus rests on a link's first piece, which stays while the link shows 948 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 949 const first = self.link_focuses[l].items[0].id; 950 if (self.focus.children.contains(first)) { 951 root_focus.grandchild_id = first; 952 self.focus.child_id = first; 953 } 954 }; 955 956 self.grid = grid; 957 } 958 959 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 960 _ = self; 961 _ = allocator; 962 _ = key; 963 _ = root_focus; 964 } 965 966 pub fn clearGrid(self: *RichText) void { 967 if (self.grid) |*grid| { 968 grid.deinit(); 969 self.grid = null; 970 } 971 } 972 973 pub fn getGrid(self: RichText) ?Grid { 974 return self.grid; 975 } 976 977 pub fn getFocus(self: *RichText) *Focus { 978 return self.focus; 979 } 980 }; 981 982 // an invisible widget that fills the horizontal space granted by its parent. 983 // used inside Box(horiz) with a min_size so the box reserves space for the 984 // children that follow, pushing them to the right. 985 pub const Spacer = struct { 986 focus: *Focus, 987 grid: ?Grid, 988 989 pub fn init(allocator: std.mem.Allocator) !Spacer { 990 return .{ 991 .focus = try Focus.create(allocator, .container), 992 .grid = null, 993 }; 994 } 995 996 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 997 self.focus.destroy(allocator); 998 if (self.grid) |*grid| { 999 grid.deinit(); 1000 self.grid = null; 1001 } 1002 } 1003 1004 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1005 _ = root_focus; 1006 self.clearGrid(); 1007 const width = constraint.max_size.width orelse return; 1008 if (width == 0) return; 1009 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1010 } 1011 1012 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1013 _ = self; 1014 _ = allocator; 1015 _ = key; 1016 _ = root_focus; 1017 } 1018 1019 pub fn clearGrid(self: *Spacer) void { 1020 if (self.grid) |*grid| { 1021 grid.deinit(); 1022 self.grid = null; 1023 } 1024 } 1025 1026 pub fn getGrid(self: Spacer) ?Grid { 1027 return self.grid; 1028 } 1029 1030 pub fn getFocus(self: *Spacer) *Focus { 1031 return self.focus; 1032 } 1033 }; 1034 1035 // a sub-header's search box: a labeled text input whose enter the owning view 1036 // turns into a navigation. shared by the files, commits and thread tabs. 1037 pub const SearchBox = struct { 1038 text_input: wgt.TextInput, 1039 session: *ui.Session, 1040 1041 // the room the box asks its row for 1042 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 1043 1044 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 1045 var text_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = label }, .name = name, .round_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 1046 errdefer text_input.deinit(allocator); 1047 text_input.getFocus().mode = .all; 1048 if (text_value) |value| try text_input.setContent(allocator, value); 1049 return .{ .text_input = text_input, .session = session }; 1050 } 1051 1052 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 1053 self.text_input.deinit(allocator); 1054 } 1055 1056 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1057 self.text_input.options.bottom_label.text = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 1058 try self.text_input.build(allocator, constraint, root_focus); 1059 if (!self.session.is_terminal) { 1060 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 1061 } 1062 } 1063 1064 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1065 try self.text_input.input(allocator, key, root_focus); 1066 } 1067 1068 // the typed text; the caller owns it 1069 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 1070 return self.text_input.text(allocator); 1071 } 1072 1073 // whether the cursor sits past the last character, where right-arrow leaves 1074 // the box rather than moving within it 1075 pub fn atEnd(self: *SearchBox) bool { 1076 return self.text_input.cursor == self.text_input.content.items.len; 1077 } 1078 1079 pub fn clearGrid(self: *SearchBox) void { 1080 self.text_input.clearGrid(); 1081 } 1082 1083 pub fn getGrid(self: SearchBox) ?Grid { 1084 return self.text_input.getGrid(); 1085 } 1086 1087 pub fn getFocus(self: *SearchBox) *Focus { 1088 return self.text_input.getFocus(); 1089 } 1090 }; 1091 1092 // a wrapping row of bordered options posted as one form field. the selection 1093 // is the focused option, so a click selects on the web too. 1094 pub const Radio = struct { 1095 // holds the options' flow, framed under the label when there is one 1096 box: wgt.Box(Widget), 1097 name: []const u8, 1098 values: []const []const u8, 1099 1100 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8, label: ?[]const u8) !Radio { 1101 var box = try wgt.Box(Widget).init(allocator, .{ 1102 .border = if (label != null) .single else null, 1103 .round_corners = true, 1104 .direction = .vert, 1105 .top_label = .{ .text = label orelse "" }, 1106 }); 1107 errdefer box.deinit(allocator); 1108 1109 var flow = try WordFlow.init(allocator); 1110 errdefer flow.deinit(allocator); 1111 const pa = session.page_arena.allocator(); 1112 const items = try pa.alloc(WordFlow.Item, values.len); 1113 for (items, values) |*item, value| item.* = .{ .text = value, .link = try pa.print(ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 1114 try flow.setItems(allocator, items); 1115 for (flow.text_boxes.items, values) |*option, value| { 1116 if (std.mem.eql(u8, value, initial)) flow.focus.child_id = option.getFocus().id; 1117 } 1118 1119 box.getFocus().child_id = flow.getFocus().id; 1120 try box.children.put(allocator, flow.getFocus().id, .{ .widget = .{ .word_flow = flow }, .rect = null, .min_size = null }); 1121 return .{ .box = box, .name = name, .values = values }; 1122 } 1123 1124 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 1125 self.box.deinit(allocator); 1126 } 1127 1128 fn optionFlow(self: *Radio) *WordFlow { 1129 return &self.box.children.values()[0].widget.word_flow; 1130 } 1131 1132 // the focused option's value 1133 pub fn selected(self: *Radio) []const u8 { 1134 const options = self.optionFlow(); 1135 return self.values[options.indexOfFocusId(options.focus.child_id orelse unreachable) orelse unreachable]; 1136 } 1137 1138 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1139 self.clearGrid(); 1140 const options = self.optionFlow(); 1141 const selected_id = options.focus.child_id; 1142 for (options.text_boxes.items) |*option| { 1143 option.options.border = if (option.getFocus().id == selected_id) .single else .hidden; 1144 } 1145 try self.box.build(allocator, constraint, root_focus); 1146 } 1147 1148 // left and right step through the options in order, however they wrap 1149 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1150 _ = allocator; 1151 const options = self.optionFlow(); 1152 const index = options.indexOfFocusId(options.focus.child_id orelse return) orelse return; 1153 const option_boxes = options.text_boxes.items; 1154 switch (key) { 1155 .arrow_left => if (index > 0) root_focus.setFocus(option_boxes[index - 1].getFocus().id), 1156 .arrow_right => if (index + 1 < option_boxes.len) root_focus.setFocus(option_boxes[index + 1].getFocus().id), 1157 else => {}, 1158 } 1159 } 1160 1161 pub fn clearGrid(self: *Radio) void { 1162 self.box.clearGrid(); 1163 } 1164 1165 pub fn getGrid(self: Radio) ?Grid { 1166 return self.box.getGrid(); 1167 } 1168 1169 pub fn getFocus(self: *Radio) *Focus { 1170 return self.box.getFocus(); 1171 } 1172 }; 1173 1174 // a tab's sub-header: an optional search box on the left, then a spacer, then 1175 // the clone url on the right. the owning view turns a submitted query into a 1176 // route, so route building stays with the tab that knows its own url. 1177 pub const SearchHeader = struct { 1178 box: wgt.Box(Widget), 1179 session: *ui.Session, 1180 1181 pub fn init( 1182 allocator: std.mem.Allocator, 1183 session: *ui.Session, 1184 handle: ui.RepoHandle, 1185 label: []const u8, 1186 name: []const u8, 1187 text: ?[]const u8, 1188 show_input: bool, 1189 ) !SearchHeader { 1190 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1191 errdefer box.deinit(allocator); 1192 1193 // the search box only appears where an index can serve it. 1194 if (show_input) { 1195 var search_box = try SearchBox.init(allocator, session, label, name, text); 1196 errdefer search_box.deinit(allocator); 1197 box.getFocus().child_id = search_box.getFocus().id; 1198 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1199 } 1200 1201 // the spacer pushes the clone url to the right edge. 1202 { 1203 var spacer = try Spacer.init(allocator); 1204 errdefer spacer.deinit(allocator); 1205 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1206 } 1207 1208 if (session.data.host_kind == .server) { 1209 if (try CopyableText.initClone(allocator, session, handle)) |value| { 1210 var clone_url = value; 1211 errdefer clone_url.deinit(allocator); 1212 const min_width = clone_url.minWidth(); 1213 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1214 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1215 } 1216 } 1217 1218 return .{ .box = box, .session = session }; 1219 } 1220 1221 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1222 self.box.deinit(allocator); 1223 } 1224 1225 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1226 self.clearGrid(); 1227 try self.box.build(allocator, constraint, root_focus); 1228 } 1229 1230 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1231 const current = self.box.getFocus().child_id orelse return; 1232 if (self.searchBox()) |search_box| { 1233 if (current == search_box.getFocus().id) { 1234 // tab, and right-arrow past the last character, step to the 1235 // clone url beside the box 1236 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1237 if (self.cloneUrl()) |clone_url| { 1238 root_focus.setFocus(clone_url.getFocus().id); 1239 return; 1240 } 1241 } 1242 return search_box.input(allocator, key, root_focus); 1243 } 1244 } 1245 const clone_url = self.cloneUrl() orelse return; 1246 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1247 if (self.searchBox()) |search_box| { 1248 root_focus.setFocus(search_box.getFocus().id); 1249 return; 1250 } 1251 } 1252 try clone_url.input(allocator, key, root_focus); 1253 } 1254 1255 // the typed query when enter should submit it: the box exists and holds 1256 // focus. the caller owns the returned text. 1257 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1258 const search_box = self.searchBox() orelse return null; 1259 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1260 return try search_box.text(allocator); 1261 } 1262 1263 // enter the header, preferring the search box. 1264 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1265 if (self.searchBox()) |search_box| { 1266 root_focus.setFocus(search_box.getFocus().id); 1267 return true; 1268 } 1269 const clone_url = self.cloneUrl() orelse return false; 1270 root_focus.setFocus(clone_url.getFocus().id); 1271 return true; 1272 } 1273 1274 pub fn hasFocusable(self: *SearchHeader) bool { 1275 return self.cloneUrl() != null or self.searchBox() != null; 1276 } 1277 1278 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1279 for (self.box.children.values()) |*child| switch (child.widget) { 1280 .copyable_text => |*copyable_text| return copyable_text, 1281 else => {}, 1282 }; 1283 return null; 1284 } 1285 1286 fn searchBox(self: *SearchHeader) ?*SearchBox { 1287 for (self.box.children.values()) |*child| switch (child.widget) { 1288 .search_box => |*search_box| return search_box, 1289 else => {}, 1290 }; 1291 return null; 1292 } 1293 1294 pub fn clearGrid(self: *SearchHeader) void { 1295 self.box.clearGrid(); 1296 } 1297 1298 pub fn getGrid(self: SearchHeader) ?Grid { 1299 return self.box.getGrid(); 1300 } 1301 1302 pub fn getFocus(self: *SearchHeader) *Focus { 1303 return self.box.getFocus(); 1304 } 1305 }; 1306 1307 // a one-row, non-focusable status bar showing the current page's url 1308 pub const Footer = struct { 1309 focus: *Focus, 1310 grid: ?Grid, 1311 session: *ui.Session, 1312 arena: std.heap.ArenaAllocator, 1313 url: []const u8 = "", 1314 url_width: usize = 0, 1315 1316 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1317 return .{ 1318 .focus = try Focus.create(allocator, .container), 1319 .grid = null, 1320 .session = session, 1321 .arena = std.heap.ArenaAllocator.init(allocator), 1322 }; 1323 } 1324 1325 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1326 self.focus.destroy(allocator); 1327 if (self.grid) |*grid| grid.deinit(); 1328 self.arena.deinit(); 1329 } 1330 1331 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1332 _ = root_focus; 1333 self.clearGrid(); 1334 const width = constraint.max_size.width orelse return; 1335 if (width == 0) return; 1336 1337 _ = self.arena.reset(.retain_capacity); 1338 self.url = ""; 1339 self.url_width = 0; 1340 const aa = self.arena.allocator(); 1341 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1342 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1343 self.url = text; 1344 1345 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1346 errdefer grid.deinit(); 1347 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1348 var i: usize = 0; 1349 while (utf8.nextCodepoint()) |ch| { 1350 const rune_width = xitui.width.cellWidth(ch); 1351 if (i + rune_width > width) break; 1352 // a terminal hyperlink to the page 1353 (try grid.cell(i, 0)).link = text; 1354 try grid.setRune(i, 0, ch); 1355 i += rune_width; 1356 } 1357 self.url_width = i; 1358 self.grid = grid; 1359 } 1360 1361 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1362 _ = allocator; 1363 switch (key) { 1364 .mouse => |mouse| { 1365 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1366 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1367 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1368 }, 1369 else => {}, 1370 } 1371 } 1372 1373 pub fn clearGrid(self: *Footer) void { 1374 if (self.grid) |*grid| { 1375 grid.deinit(); 1376 self.grid = null; 1377 } 1378 } 1379 1380 pub fn getGrid(self: Footer) ?Grid { 1381 return self.grid; 1382 } 1383 1384 pub fn getFocus(self: *Footer) *Focus { 1385 return self.focus; 1386 } 1387 }; 1388 1389 // a single-child wrapper that builds the child at its natural size and 1390 // positions its grid in the middle of the area granted by the parent 1391 // a bordered label, indented past the content it precedes 1392 pub const SectionLabel = struct { 1393 box: wgt.Box(Widget), 1394 1395 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1396 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1397 errdefer box.deinit(allocator); 1398 1399 { 1400 var gap = try wgt.Text.init(allocator, " "); 1401 errdefer gap.deinit(allocator); 1402 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1403 } 1404 1405 { 1406 var tb = try wgt.TextBox.init(allocator, content, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1407 errdefer tb.deinit(allocator); 1408 tb.getFocus().mode = .all; 1409 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1410 } 1411 1412 box.getFocus().child_id = box.children.keys()[1]; 1413 return .{ .box = box }; 1414 } 1415 1416 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1417 self.box.deinit(allocator); 1418 } 1419 1420 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1421 try self.box.build(allocator, constraint, root_focus); 1422 } 1423 1424 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1425 _ = self; 1426 _ = allocator; 1427 _ = key; 1428 _ = root_focus; 1429 } 1430 1431 pub fn clearGrid(self: *SectionLabel) void { 1432 self.box.clearGrid(); 1433 } 1434 1435 pub fn getGrid(self: SectionLabel) ?Grid { 1436 return self.box.getGrid(); 1437 } 1438 1439 pub fn getFocus(self: *SectionLabel) *Focus { 1440 return self.box.getFocus(); 1441 } 1442 }; 1443 1444 // a submit button, indented past its form's content. the web form POSTs 1445 // the button's form; terminal hosts detect presses through `buttonId`. 1446 pub const SubmitButton = struct { 1447 box: wgt.Box(Widget), 1448 1449 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1450 return initLabeled(allocator, "submit"); 1451 } 1452 1453 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1454 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1455 errdefer box.deinit(allocator); 1456 1457 { 1458 var gap = try wgt.Text.init(allocator, " "); 1459 errdefer gap.deinit(allocator); 1460 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1461 } 1462 1463 { 1464 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1465 errdefer button.deinit(allocator); 1466 button.getFocus().mode = .all; 1467 // the renderer distinguishes plain clickables from buttons that 1468 // should POST to a server route by this kind. 1469 button.getFocus().kind = .{ .custom = "submit" }; 1470 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1471 } 1472 1473 box.getFocus().child_id = box.children.keys()[1]; 1474 return .{ .box = box }; 1475 } 1476 1477 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1478 self.box.deinit(allocator); 1479 } 1480 1481 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1482 const button = &self.box.children.values()[1]; 1483 try button.widget.text_box.setContent(allocator, label); 1484 button.min_size = .{ .width = label.len + 2, .height = null }; 1485 } 1486 1487 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1488 try self.box.build(allocator, constraint, root_focus); 1489 } 1490 1491 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1492 _ = self; 1493 _ = allocator; 1494 _ = key; 1495 _ = root_focus; 1496 } 1497 1498 pub fn clearGrid(self: *SubmitButton) void { 1499 self.box.clearGrid(); 1500 } 1501 1502 pub fn getGrid(self: SubmitButton) ?Grid { 1503 return self.box.getGrid(); 1504 } 1505 1506 pub fn getFocus(self: *SubmitButton) *Focus { 1507 return self.box.getFocus(); 1508 } 1509 1510 // the button's focus id, for click hit-testing 1511 pub fn buttonId(self: *const SubmitButton) usize { 1512 return self.box.children.keys()[1]; 1513 } 1514 }; 1515 1516 // keep `rect`, in a centered widget's space, in view within its scroll 1517 pub fn scrollToCenteredRect(scroll: *wgt.Scroll(Widget), rect: layout.IRect) void { 1518 const center = &scroll.child.center; 1519 const center_grid = center.getGrid() orelse return; 1520 const child_grid = center.child.getGrid() orelse return; 1521 const offset_y: isize = @intCast((center_grid.size.height -| child_grid.size.height) / 2); 1522 scroll.scrollToRect(.{ .x = rect.x, .y = rect.y + offset_y, .size = rect.size }); 1523 } 1524 1525 pub const Center = struct { 1526 focus: *Focus, 1527 grid: ?Grid, 1528 child: *Widget, 1529 1530 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1531 const child = try allocator.create(Widget); 1532 errdefer allocator.destroy(child); 1533 child.* = child_widget; 1534 return .{ 1535 .focus = try Focus.create(allocator, .container), 1536 .grid = null, 1537 .child = child, 1538 }; 1539 } 1540 1541 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1542 self.focus.destroy(allocator); 1543 if (self.grid) |*grid| { 1544 grid.deinit(); 1545 self.grid = null; 1546 } 1547 self.child.deinit(allocator); 1548 allocator.destroy(self.child); 1549 } 1550 1551 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1552 self.clearGrid(); 1553 self.getFocus().clear(); 1554 1555 // build the child without forcing it to fill min_size; it sizes 1556 // itself to its content within the available max. 1557 try self.child.build(allocator, .{ 1558 .min_size = .{ .width = null, .height = null }, 1559 .max_size = constraint.max_size, 1560 }, root_focus); 1561 1562 const child_grid = self.child.getGrid() orelse return; 1563 1564 // prefer max when given; otherwise grow to max(min, child) so that 1565 // when only a min is set (e.g. the wasm path passes viewport rows 1566 // as min_height with no max), we can still vertically center while 1567 // letting taller content extend past the min. 1568 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; 1569 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; 1570 if (width == 0 or height == 0) return; 1571 1572 const offset_x: usize = (width -| child_grid.size.width) / 2; 1573 const offset_y: usize = (height -| child_grid.size.height) / 2; 1574 1575 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1576 errdefer grid.deinit(); 1577 try grid.drawGrid(child_grid, offset_x, offset_y); 1578 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1579 self.getFocus().child_id = self.child.getFocus().id; 1580 1581 self.grid = grid; 1582 } 1583 1584 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1585 try self.child.input(allocator, key, root_focus); 1586 } 1587 1588 pub fn clearGrid(self: *Center) void { 1589 if (self.grid) |*grid| { 1590 grid.deinit(); 1591 self.grid = null; 1592 } 1593 self.child.clearGrid(); 1594 } 1595 1596 pub fn getGrid(self: Center) ?Grid { 1597 return self.grid; 1598 } 1599 1600 pub fn getFocus(self: *Center) *Focus { 1601 return self.focus; 1602 } 1603 }; 1604 1605 // renders truecolor ANSI art 1606 pub const AnsiArt = struct { 1607 focus: *Focus, 1608 grid: ?Grid, 1609 content: []const u8, 1610 1611 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1612 return .{ 1613 .focus = try Focus.create(allocator, .container), 1614 .grid = null, 1615 .content = content, 1616 }; 1617 } 1618 1619 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1620 self.focus.destroy(allocator); 1621 if (self.grid) |*grid| { 1622 grid.deinit(); 1623 self.grid = null; 1624 } 1625 } 1626 1627 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1628 _ = root_focus; 1629 self.clearGrid(); 1630 1631 // parse into rows of cells, then copy into a rectangular grid 1632 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1633 defer { 1634 for (rows.items) |*row| row.deinit(allocator); 1635 rows.deinit(allocator); 1636 } 1637 var row: std.ArrayList(Grid.Cell) = .empty; 1638 errdefer row.deinit(allocator); 1639 1640 var style: Grid.Style = .{}; 1641 var width: usize = 0; 1642 const content = self.content; 1643 var i: usize = 0; 1644 while (i < content.len) { 1645 const byte = content[i]; 1646 if (byte == '\n') { 1647 width = @max(width, row.items.len); 1648 try rows.append(allocator, row); 1649 row = .empty; 1650 i += 1; 1651 } else if (byte == '\r') { 1652 // git may checkout the ansi art with crlf line endings on windows 1653 i += 1; 1654 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1655 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1656 var j = i + 2; 1657 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1658 if (j >= content.len) { 1659 i = content.len; // malformed trailing escape; stop 1660 } else { 1661 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1662 i = j + 1; 1663 } 1664 } else { 1665 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1666 const end = @min(content.len, i + len); 1667 const rune = content[i..end]; 1668 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1669 // a foreground color does not make a space visible. only an 1670 // effective background makes it opaque; foreground state often 1671 // remains active through the padding at the end of a row. 1672 const effective_background = if (style.inverted) style.fg else style.bg; 1673 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1674 try row.append(allocator, .{ 1675 .rune = if (transparent) null else codepoint, 1676 .style = style, 1677 }); 1678 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1679 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1680 } 1681 i = end; 1682 } 1683 } 1684 // a trailing row with content but no closing newline 1685 if (row.items.len > 0) { 1686 width = @max(width, row.items.len); 1687 try rows.append(allocator, row); 1688 } else { 1689 row.deinit(allocator); 1690 } 1691 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1692 1693 const height = rows.items.len; 1694 if (width == 0 or height == 0) return; 1695 1696 const clamped_w = @min(width, constraint.max_size.width orelse width); 1697 const clamped_h = @min(height, constraint.max_size.height orelse height); 1698 1699 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1700 errdefer grid.deinit(); 1701 for (rows.items[0..clamped_h], 0..) |r, y| { 1702 const n = @min(r.items.len, clamped_w); 1703 for (r.items[0..n], 0..) |cell, x| { 1704 if (cell.continuation) continue; 1705 (try grid.cell(x, y)).style = cell.style; 1706 try grid.setRune(x, y, cell.rune); 1707 } 1708 } 1709 self.grid = grid; 1710 } 1711 1712 pub fn clearGrid(self: *AnsiArt) void { 1713 if (self.grid) |*grid| { 1714 grid.deinit(); 1715 self.grid = null; 1716 } 1717 } 1718 1719 pub fn getGrid(self: AnsiArt) ?Grid { 1720 return self.grid; 1721 } 1722 1723 pub fn getFocus(self: *AnsiArt) *Focus { 1724 return self.focus; 1725 } 1726 1727 fn applySgr(style: *Grid.Style, params: []const u8) void { 1728 var nums: [16]u32 = undefined; 1729 var n: usize = 0; 1730 var it = std.mem.splitScalar(u8, params, ';'); 1731 while (it.next()) |tok| { 1732 if (n >= nums.len) break; 1733 // an empty parameter (e.g. bare "\x1b[m") means 0 1734 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1735 n += 1; 1736 } 1737 if (n == 0) { 1738 style.* = .{}; 1739 return; 1740 } 1741 var i: usize = 0; 1742 while (i < n) : (i += 1) { 1743 switch (nums[i]) { 1744 0 => style.* = .{}, 1745 7 => style.inverted = true, 1746 27 => style.inverted = false, 1747 39 => style.fg = null, 1748 49 => style.bg = null, 1749 38, 48 => { 1750 // truecolor form: 38;2;r;g;b — anything else is ignored 1751 if (i + 4 < n and nums[i + 1] == 2) { 1752 const c = Grid.Color{ .rgb = .{ 1753 .r = @truncate(nums[i + 2]), 1754 .g = @truncate(nums[i + 3]), 1755 .b = @truncate(nums[i + 4]), 1756 } }; 1757 if (nums[i] == 38) style.fg = c else style.bg = c; 1758 i += 4; 1759 } 1760 }, 1761 else => {}, 1762 } 1763 } 1764 } 1765 }; 1766 1767 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1768 pub const AnsiBackground = struct { 1769 grid: ?Grid, 1770 child: *Widget, 1771 art: AnsiArt, 1772 session: *ui.Session, 1773 1774 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1775 var cw = child_widget; 1776 const child = allocator.create(Widget) catch |e| { 1777 cw.deinit(allocator); 1778 return e; 1779 }; 1780 child.* = cw; 1781 errdefer { 1782 child.deinit(allocator); 1783 allocator.destroy(child); 1784 } 1785 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1786 } 1787 1788 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1789 if (self.grid) |*grid| grid.deinit(); 1790 self.art.deinit(allocator); 1791 self.child.deinit(allocator); 1792 allocator.destroy(self.child); 1793 } 1794 1795 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1796 self.clearGrid(); 1797 1798 // make the wrapped page fill the whole available area 1799 try self.child.build(allocator, .{ 1800 .min_size = .{ 1801 .width = constraint.max_size.width orelse constraint.min_size.width, 1802 .height = constraint.max_size.height orelse constraint.min_size.height, 1803 }, 1804 .max_size = constraint.max_size, 1805 }, root_focus); 1806 1807 const foreground = self.child.getGrid() orelse return; 1808 if (!self.session.color) return; 1809 // terminals show the art only once they report a background to fade it toward 1810 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1811 self.art.content = self.session.data.ansi_art; 1812 try self.art.build(allocator, .{ 1813 .min_size = .{ .width = null, .height = null }, 1814 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1815 }, root_focus); 1816 // the web fades the art with css opacity instead 1817 const art_backdrop = backdrop orelse return; 1818 if (self.art.grid) |*art_grid| { 1819 for (art_grid.cells) |*cell| { 1820 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1821 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1822 } 1823 self.grid = try artBehind(allocator, foreground, art_grid.*); 1824 } 1825 } 1826 1827 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1828 try self.child.input(allocator, key, root_focus); 1829 } 1830 1831 pub fn clearGrid(self: *AnsiBackground) void { 1832 if (self.grid) |*grid| { 1833 grid.deinit(); 1834 self.grid = null; 1835 } 1836 self.child.clearGrid(); 1837 self.art.clearGrid(); 1838 } 1839 1840 pub fn getGrid(self: AnsiBackground) ?Grid { 1841 return self.grid orelse self.child.getGrid(); 1842 } 1843 1844 pub fn getFocus(self: *AnsiBackground) *Focus { 1845 return self.child.getFocus(); 1846 } 1847 1848 // a foreground cell counts as blank if it's empty or a bare space without styling 1849 fn cellIsBlank(cell: Grid.Cell) bool { 1850 if (cell.continuation) return false; 1851 if (cell.rune) |rune| { 1852 return rune == ' ' and 1853 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1854 } 1855 return true; 1856 } 1857 1858 // the art is faded toward the background at this opacity so foreground 1859 // text stays legible over it 1860 pub const art_opacity = 25; // percent 1861 1862 // the art parser only emits truecolor, so a palette color never occurs 1863 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1864 const c = color orelse return null; 1865 return switch (c) { 1866 .rgb => |v| v, 1867 else => null, 1868 }; 1869 } 1870 1871 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1872 const v = artRgb(color) orelse return color; 1873 const blend = struct { 1874 fn f(c: u8, b: u8) u8 { 1875 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1876 } 1877 }.f; 1878 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1879 } 1880 1881 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1882 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1883 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1884 // snap such channels clear of that range so the backdrop doesn't blink or 1885 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1886 // actually render truecolor. 1887 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1888 const v = artRgb(color) orelse return color; 1889 const snap = struct { 1890 fn f(c: u8) u8 { 1891 return if (c >= 1 and c <= 9) 10 else c; 1892 } 1893 }.f; 1894 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1895 } 1896 1897 // composites ANSI art behind a foreground grid 1898 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1899 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1900 errdefer out.deinit(); 1901 1902 const anchor_x = foreground.size.width -| art_grid.size.width; 1903 for (0..art_grid.size.height) |y| { 1904 for (0..art_grid.size.width) |x| { 1905 const src = (try art_grid.cell(x, y)).*; 1906 if (src.rune == null) continue; 1907 const dst = try out.cell(anchor_x + x, y); 1908 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1909 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1910 // blank cells take the art; occupied cells take its background 1911 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1912 dst.style = src.style; 1913 dst.style.fg = sgrSafe(dst.style.fg); 1914 dst.style.bg = sgrSafe(dst.style.bg); 1915 try out.setRune(anchor_x + x, y, src.rune); 1916 } else { 1917 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1918 dst.style.bg = art_bg; 1919 if (next_dst) |next| next.style.bg = art_bg; 1920 } 1921 } 1922 } 1923 return out; 1924 } 1925 }; 1926 1927 pub const CopyableText = struct { 1928 pub const Choice = struct { 1929 selector: []const u8 = "", 1930 text: []const u8, 1931 copyable_text: ?[]const u8 = null, 1932 label: []const u8 = "", 1933 bottom_label: []const u8 = "", 1934 }; 1935 1936 box: wgt.Box(Widget), 1937 session: *ui.Session, 1938 choices: []Choice, 1939 selected: usize = 0, 1940 1941 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1942 if (choices.len == 0) return error.MissingCopyableText; 1943 1944 const owned_choices = try allocator.dupe(Choice, choices); 1945 errdefer allocator.free(owned_choices); 1946 1947 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1948 errdefer box.deinit(allocator); 1949 1950 if (choices.len > 1) for (choices) |choice| { 1951 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1952 errdefer selector_box.deinit(allocator); 1953 selector_box.getFocus().mode = .all; 1954 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 } }); 1955 }; 1956 1957 var text_input = try wgt.TextInput.init(allocator, .{ 1958 .border = .single, 1959 .round_corners = true, 1960 .top_label = .{ .text = choices[initial].label }, 1961 .bottom_label = .{ .text = choices[initial].bottom_label }, 1962 .read_only = true, 1963 .render_content = session.is_terminal, 1964 .visible_width = null, 1965 }); 1966 errdefer text_input.deinit(allocator); 1967 text_input.getFocus().mode = .all; 1968 try text_input.setContent(allocator, choices[initial].text); 1969 box.getFocus().child_id = text_input.getFocus().id; 1970 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1971 1972 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1973 } 1974 1975 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1976 const aa = session.page_arena.allocator(); 1977 var choices: [2]Choice = undefined; 1978 var count: usize = 0; 1979 var ssh_index: ?usize = null; 1980 switch (handle.location) { 1981 .repo => |identity| { 1982 if (session.data.git_http_port) |port| { 1983 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1984 choices[count] = .{ 1985 .selector = "http", 1986 .text = url, 1987 .copyable_text = try aa.print("git clone {s}", .{url}), 1988 .label = " clone ", 1989 }; 1990 count += 1; 1991 } 1992 if (session.data.git_ssh_port) |port| { 1993 ssh_index = count; 1994 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1995 choices[count] = .{ 1996 .selector = "ssh", 1997 .text = url, 1998 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1999 .label = " clone ", 2000 }; 2001 count += 1; 2002 } 2003 }, 2004 .fork => |f| if (session.data.git_ssh_port) |port| { 2005 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 2006 choices[count] = .{ 2007 .text = url, 2008 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2009 .label = " clone ", 2010 }; 2011 count += 1; 2012 }, 2013 } 2014 if (count == 0) return null; 2015 // a viewer who can push starts on the ssh url 2016 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2017 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2018 } 2019 2020 pub fn minWidth(self: *const CopyableText) usize { 2021 var width = maxWidth(self.choices) + 2; 2022 if (self.choices.len > 1) { 2023 for (self.choices) |choice| width += choice.selector.len + 2; 2024 } 2025 return width; 2026 } 2027 2028 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2029 self.box.deinit(allocator); 2030 allocator.free(self.choices); 2031 } 2032 2033 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2034 self.clearGrid(); 2035 2036 if (self.choices.len > 1) { 2037 if (root_focus.grandchild_id) |id| { 2038 for (0..self.choices.len) |i| { 2039 if (id != self.selector(i).getFocus().id) continue; 2040 if (i != self.selected) try self.select(allocator, i); 2041 root_focus.setFocus(self.textInput().getFocus().id); 2042 break; 2043 } 2044 } 2045 2046 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2047 for (0..self.choices.len) |i| { 2048 self.selector(i).options.border = if (i == self.selected) 2049 (if (focused) .double else .single) 2050 else 2051 .hidden; 2052 } 2053 } 2054 self.textInput().options.border = .single; 2055 self.textInput().cursor = 0; 2056 self.textInput().scroll_offset = 0; 2057 try self.box.build(allocator, constraint, root_focus); 2058 2059 if (!self.session.is_terminal) { 2060 const aa = self.session.arena.allocator(); 2061 const id = self.textInput().getFocus().id; 2062 try self.session.text_inputs.put(aa, id, self.textInput()); 2063 } 2064 } 2065 2066 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2067 switch (key) { 2068 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2069 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2070 .enter => if (self.session.is_terminal) self.show(), 2071 .mouse => |mouse| { 2072 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2073 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2074 try self.select(allocator, i); 2075 root_focus.setFocus(self.textInput().getFocus().id); 2076 return; 2077 }; 2078 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2079 }, 2080 else => {}, 2081 } 2082 } 2083 2084 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2085 self.selected = selected; 2086 const choice = self.choices[selected]; 2087 const text_input = self.textInput(); 2088 text_input.options.top_label.text = choice.label; 2089 text_input.options.bottom_label.text = choice.bottom_label; 2090 try text_input.setContent(allocator, choice.text); 2091 } 2092 2093 fn show(self: *CopyableText) void { 2094 const choice = self.choices[self.selected]; 2095 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2096 } 2097 2098 fn textInput(self: *CopyableText) *wgt.TextInput { 2099 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2100 } 2101 2102 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2103 return &self.box.children.values()[index].widget.text_box; 2104 } 2105 2106 fn maxWidth(choices: []const Choice) usize { 2107 var width: usize = 0; 2108 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2109 return width; 2110 } 2111 2112 pub fn clearGrid(self: *CopyableText) void { 2113 self.box.clearGrid(); 2114 } 2115 2116 pub fn getGrid(self: CopyableText) ?Grid { 2117 return self.box.getGrid(); 2118 } 2119 2120 pub fn getFocus(self: *CopyableText) *Focus { 2121 return self.box.getFocus(); 2122 } 2123 }; 2124