╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
817 const prefix = if (loaded) "ai:" else "a:"; 818 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 819 } 820 821 // the url `line` value for a content window: its first line's 1-based number, 822 // except the first window, which serializes as unset (0). 823 fn lineNumber(window_start: usize) usize { 824 return if (window_start == 0) 0 else window_start + 1; 825 } 826 827 // `dir`/`name`, or just `name` at the root. 828 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 829 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 830 } 831 832 // `dir` with its last segment removed ("" when `dir` has a single segment). 833 fn parentDir(dir: []const u8) []const u8 { 834 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 835 return dir[0..slash]; 836 } 837 838 // the "<ref_or_oid> <value>" banner shown above the listing. 839 pub const Header = struct { 840 content: []const u8, 841 842 // `value` arrives url-encoded, so decode it for display. 843 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 844 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 845 return .{ 846 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 847 }; 848 } 849 850 pub const View = struct { 851 box: wgt.Box(ui.Widget), 852 data: *const Header, 853 854 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 855 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 856 errdefer box.deinit(allocator); 857 858 if (!session.data.is_local) { 859 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 860 var clone_url = value; 861 errdefer clone_url.deinit(allocator); 862 const min_width = clone_url.minWidth(); 863 box.getFocus().child_id = clone_url.getFocus().id; 864 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 } }); 865 } 866 } 867 868 var spacer = try ui.widget.Spacer.init(allocator); 869 errdefer spacer.deinit(allocator); 870 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 871 872 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 873 errdefer text_box.deinit(allocator); 874 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 875 876 return .{ .box = box, .data = data }; 877 } 878 879 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 880 self.box.deinit(allocator); 881 } 882 883 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 884 self.clearGrid(); 885 try self.box.build(allocator, constraint, root_focus); 886 } 887 888 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 889 const clone_url = self.cloneUrl() orelse return; 890 try clone_url.input(allocator, key, root_focus); 891 } 892 893 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 894 const clone_url = self.cloneUrl() orelse return false; 895 root_focus.setFocus(clone_url.getFocus().id); 896 return true; 897 } 898 899 pub fn hasCloneUrl(self: *Header.View) bool { 900 return self.cloneUrl() != null; 901 } 902 903 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 904 for (self.box.children.values()) |*child| switch (child.widget) { 905 .copyable_text => |*copyable_text| return copyable_text, 906 else => {}, 907 }; 908 return null; 909 } 910 911 pub fn clearGrid(self: *Header.View) void { 912 self.box.clearGrid(); 913 } 914 915 pub fn getGrid(self: Header.View) ?Grid { 916 return self.box.getGrid(); 917 } 918 919 pub fn getFocus(self: *Header.View) *Focus { 920 return self.box.getFocus(); 921 } 922 }; 923 }; 924