╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2958 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2959 } 2960 2961 // an assignee as the detail view shows it: the stored email, and the name of 2962 // the user holding it on this instance when there is one 2963 pub const Assignee = struct { 2964 email: []const u8, 2965 name: ?[]const u8 = null, 2966 2967 pub fn display(self: Assignee) []const u8 { 2968 return self.name orelse self.email; 2969 } 2970 }; 2971 2972 // a display author, resolved against the admin db at read time 2973 pub const Author = union(enum) { 2974 unknown, 2975 email: []const u8, // an email no user matches 2976 user_name: []const u8, 2977 2978 // the commit author line's email, resolved like initFromEmail 2979 pub fn init( 2980 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2981 arena: *std.heap.ArenaAllocator, 2982 author_line: []const u8, 2983 ) !Author { 2984 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2985 } 2986 2987 // from a record's stored author email: it resolves to the user currently 2988 // holding it, else stays the email. the result is allocated in `arena`, 2989 // so the email may borrow transient memory. 2990 pub fn initFromEmail( 2991 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2992 arena: *std.heap.ArenaAllocator, 2993 author_email: ?[]const u8, 2994 ) !Author { 2995 const email = author_email orelse return .unknown; 2996 if (haxy_moment) |moment| { 2997 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2998 return .{ .user_name = user.name }; 2999 } 3000 } 3001 return .{ .email = try arena.allocator().dupe(u8, email) }; 3002 } 3003 }; 3004 3005 // the "a:" link to user `name`'s page. 3006 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 3007 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 3008 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 3009 return page_arena.allocator().print("a:{s}", .{url}); 3010 } 3011 3012 // a focusable " author " box showing `author`, linking to their user page 3013 // when it is a known user 3014 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3015 const text = switch (author) { 3016 .unknown => "", 3017 .email, .user_name => |t| t, 3018 }; 3019 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3020 errdefer tb.deinit(allocator); 3021 tb.getFocus().mode = .all; 3022 switch (author) { 3023 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3024 else => {}, 3025 } 3026 return tb; 3027 } 3028 3029 // native-TUI navigation 3030 pub const Nav = struct { 3031 root: Widget, 3032 // backs the current page (its Page data and the page-scoped strings its 3033 // widgets build). owned here, swapped on each navigation. session.page_arena 3034 // tracks whichever of these is current so widgets allocate into it. 3035 arena: *std.heap.ArenaAllocator, 3036 // the login the current root was built for 3037 user_id: ?[evt.event_id_size]u8, 3038 history: std.ArrayList(Entry), 3039 3040 // each retained page keeps its own arena, freed when the page leaves the 3041 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3042 // long-lived session from accumulating every page it ever visited. 3043 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3044 3045 // cap on retained back-history so the chain can't grow memory without bound 3046 const max_history: usize = 16; 3047 3048 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3049 session.back = .unavailable; 3050 const arena = try allocator.create(std.heap.ArenaAllocator); 3051 arena.* = std.heap.ArenaAllocator.init(allocator); 3052 errdefer { 3053 arena.deinit(); 3054 allocator.destroy(arena); 3055 } 3056 3057 return .{ 3058 .root = try initCurrentRoot(allocator, arena, session), 3059 .arena = arena, 3060 .user_id = session.userId(), 3061 .history = .empty, 3062 }; 3063 } 3064 3065 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3066 self.root.deinit(allocator); 3067 freeArena(allocator, self.arena); 3068 for (self.history.items) |*entry| { 3069 entry.root.deinit(allocator); 3070 freeArena(allocator, entry.arena); 3071 } 3072 self.history.deinit(allocator); 3073 } 3074 3075 // rebuild the current page in place from a current moment 3076 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3077 if (session.haxy_moment == null and session.local == null) return; 3078 const arena = try allocator.create(std.heap.ArenaAllocator); 3079 arena.* = std.heap.ArenaAllocator.init(allocator); 3080 errdefer freeArena(allocator, arena); 3081 3082 const new_root = try initCurrentRoot(allocator, arena, session); 3083 3084 self.root.deinit(allocator); 3085 freeArena(allocator, self.arena); 3086 self.root = new_root; 3087 self.arena = arena; 3088 self.user_id = session.userId(); 3089 } 3090 3091 // build the current page's root in `arena`, or the not-found page's when 3092 // the route names nothing 3093 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3094 session.page_arena = arena; 3095 const page = try arena.allocator().create(Page); 3096 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3097 error.NotFound => blk: { 3098 session.data.current_page = .not_found; 3099 break :blk .not_found; 3100 }, 3101 else => |e| return e, 3102 }; 3103 return initRoot(allocator, page, session); 3104 } 3105 3106 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3107 arena.deinit(); 3108 allocator.destroy(arena); 3109 } 3110 3111 // reconcile the displayed root with the session's navigation state. a 3112 // forward nav (current_page moved to a different parent page) pushes the 3113 // current root and builds the new page in a fresh arena; a back request 3114 // frees the current page and restores the previous one. when escape is 3115 // pressed with no history left we switch to the quit tab instead of quitting. 3116 // returns whether the root was replaced. 3117 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3118 // refresh: rebuild the current page in place from a current moment 3119 if (session.refresh_requested) { 3120 session.refresh_requested = false; 3121 try self.rebuild(allocator, session); 3122 return true; 3123 } 3124 3125 if (session.back == .requested) { 3126 if (self.history.pop()) |entry| { 3127 session.back = if (self.history.items.len == 0) .unavailable else .available; 3128 self.root.deinit(allocator); 3129 freeArena(allocator, self.arena); 3130 self.root = entry.root; 3131 self.arena = entry.arena; 3132 self.user_id = entry.user_id; 3133 session.page_arena = entry.arena; 3134 session.data.current_page = entry.route; 3135 // a page built under another login is rebuilt rather than restored 3136 if (!std.meta.eql(entry.user_id, session.userId())) { 3137 try self.rebuild(allocator, session); 3138 return true; 3139 } 3140 chooseAnsiArtForNavigation(session); 3141 return true; 3142 } 3143 session.back = .unavailable; 3144 // nothing to go back to; switch to the quit confirmation 3145 const root_focus = self.root.getFocus(); 3146 var iter = root_focus.children.iterator(); 3147 while (iter.next()) |entry| { 3148 switch (entry.value_ptr.focus.kind) { 3149 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3150 root_focus.setFocus(entry.key_ptr.*); 3151 // a Stack only builds its selected child, so the quit 3152 // button isn't in the focus tree until a build runs with 3153 // the quit tab selected. build once, then send a 3154 // synthetic arrow_down to drop focus from the tab onto 3155 // the button so it's ready to confirm. 3156 try self.root.build(allocator, .{ 3157 .min_size = .{ .width = null, .height = 40 }, 3158 .max_size = .{ .width = 80, .height = null }, 3159 }, root_focus); 3160 try self.root.input(allocator, .arrow_down, root_focus); 3161 break; 3162 }, 3163 else => {}, 3164 } 3165 } 3166 return false; 3167 } 3168 3169 // forward navigation: navigate() set next_page to the page to move to (a 3170 // cross-page link or tab change crossing pages) 3171 if (session.next_page) |route| { 3172 session.next_page = null; 3173 if (session.haxy_moment == null and session.local == null) return false; 3174 const previous_route = session.data.current_page; 3175 // the page we navigated to becomes the current page 3176 session.data.current_page = route; 3177 session.back = .available; 3178 3179 const arena = try allocator.create(std.heap.ArenaAllocator); 3180 arena.* = std.heap.ArenaAllocator.init(allocator); 3181 errdefer freeArena(allocator, arena); 3182 3183 const new_root = try initCurrentRoot(allocator, arena, session); 3184 3185 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3186 // drop the oldest entry (freeing its widget tree and arena) once over cap 3187 if (self.history.items.len > max_history) { 3188 var oldest = self.history.orderedRemove(0); 3189 oldest.root.deinit(allocator); 3190 freeArena(allocator, oldest.arena); 3191 } 3192 self.root = new_root; 3193 self.arena = arena; 3194 self.user_id = session.userId(); 3195 return true; 3196 } 3197 return false; 3198 } 3199 }; 3200 3201 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3202 const page_widget: Widget = switch (page.*) { 3203 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3204 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3205 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3206 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3207 .not_found => .{ .not_found = try .init(allocator, session) }, 3208 }; 3209 3210 chooseAnsiArtForNavigation(session); 3211 3212 // on the TUI/SSH, the page sits above a one-row footer showing its web 3213 // url, when there's a web UI to point at 3214 var root = if (session.is_terminal) blk: { 3215 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3216 errdefer box.deinit(allocator); 3217 const bg_id = bg_blk: { 3218 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3219 errdefer background.deinit(allocator); 3220 const id = background.getFocus().id; 3221 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3222 break :bg_blk id; 3223 }; 3224 if (session.web_port != null) { 3225 var footer = try widget.Footer.init(allocator, session); 3226 errdefer footer.deinit(allocator); 3227 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3228 } 3229 box.getFocus().child_id = bg_id; 3230 break :blk Widget{ .box = box }; 3231 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3232 errdefer root.deinit(allocator); 3233 3234 // input-owning views build their TextInputs in init, so reset their 3235 // registrations here 3236 session.text_inputs.clearRetainingCapacity(); 3237 3238 try root.build(allocator, .{ 3239 .min_size = .{ .width = null, .height = 40 }, 3240 .max_size = .{ .width = 100, .height = null }, 3241 }, root.getFocus()); 3242 3243 return root; 3244 } 3245 3246 // native and server-side sessions choose from the embedded collection. WASM 3247 // keeps the art serialized by the server instead. 3248 fn chooseAnsiArtForNavigation(session: *Session) void { 3249 if (comptime ansi_art.art.len == 0) return; 3250 if (session.data.current_page == .not_found) { 3251 session.data.ansi_art = ansi_art.not_found; 3252 return; 3253 } 3254 const io = session.io orelse return; 3255 var random: usize = undefined; 3256 io.random(std.mem.asBytes(&random)); 3257 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3258 } 3259