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