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