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