╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1996 1997 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1998 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1999 } 2000 2001 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 2002 if (!xit.ref.validateName(branch)) return false; 2003 return switch (self.repo_kind) { 2004 inline else => |repo_kind| blk: { 2005 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2006 defer any_repo.deinit(io, allocator); 2007 break :blk switch (any_repo) { 2008 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 2009 }; 2010 }, 2011 }; 2012 } 2013 2014 // whether the actor may change the event `id` names 2015 pub fn canModify( 2016 self: RepoSource, 2017 io: std.Io, 2018 allocator: std.mem.Allocator, 2019 actor: Actor, 2020 kind: evt.EventKind, 2021 id: *const [evt.event_id_size]u8, 2022 ) !bool { 2023 // a role that may change an event with no author needs no read 2024 if (actor.role.canModify(actor.author.email, null)) return true; 2025 2026 var arena = std.heap.ArenaAllocator.init(allocator); 2027 defer arena.deinit(); 2028 return switch (self.repo_kind) { 2029 inline else => |repo_kind| blk: { 2030 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2031 defer any_repo.deinit(io, allocator); 2032 break :blk switch (any_repo) { 2033 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 2034 }; 2035 }, 2036 }; 2037 } 2038 }; 2039 2040 // who a write acts as and the role they hold in its repo 2041 pub const Actor = struct { 2042 author: evt.CommitAuthor, 2043 // local mode has no accounts, so it has no user id 2044 user_id: ?[evt.event_id_size]u8 = null, 2045 role: evt.Repo.Role, 2046 // the repo owner's id, which local mode lacks 2047 repo_user_id: ?[evt.event_id_size]u8 = null, 2048 }; 2049 2050 pub const Authorization = union(enum) { 2051 actor: Actor, 2052 login_required, 2053 repo_not_found, 2054 forbidden, 2055 }; 2056 2057 // the account `user_id` names, or null once it has been removed. a user id is 2058 // checked here wherever one enters, so the role lookups can trust theirs. 2059 pub fn activeUser( 2060 moment: evt.AdminDB.HashMap(.read_only), 2061 arena: *std.heap.ArenaAllocator, 2062 user_id: [evt.event_id_size]u8, 2063 ) !?evt.User.Record { 2064 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 2065 return if (user.removed) null else user; 2066 } 2067 2068 // whether the user may write to the repo `identity` names with at least 2069 // `min_role` 2070 pub fn authorizeUser( 2071 io: std.Io, 2072 moment: evt.AdminDB.HashMap(.read_only), 2073 arena: *std.heap.ArenaAllocator, 2074 users_dir: []const u8, 2075 user_id: [evt.event_id_size]u8, 2076 identity: []const u8, 2077 min_role: evt.Repo.Role, 2078 ) !Authorization { 2079 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 2080 2081 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return .repo_not_found; 2082 const repo = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, moment, users_dir, owner_repo.owner, owner_repo.name)) orelse return .repo_not_found; 2083 const role = evt.Repo.roleOf(repo.repo, user_id); 2084 // a repo the user can't read doesn't exist to them 2085 if (role == .none) return .repo_not_found; 2086 if (!role.atLeast(min_role)) return .forbidden; 2087 return .{ .actor = .{ 2088 .author = .{ .name = user.event.name, .email = user.event.email }, 2089 .user_id = user_id, 2090 .role = role, 2091 .repo_user_id = repo.repo.event.user_id[0..evt.event_id_size].*, 2092 } }; 2093 } 2094 2095 // events created in local mode have no account behind them, so the commit 2096 // author comes from the repo's config, which the global config feeds into. 2097 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 2098 2099 pub fn localAuthor( 2100 src: RepoSource, 2101 io: std.Io, 2102 allocator: std.mem.Allocator, 2103 arena: *std.heap.ArenaAllocator, 2104 ) !evt.CommitAuthor { 2105 switch (src.repo_kind) { 2106 inline else => |repo_kind| { 2107 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 2108 defer any_repo.deinit(io, allocator); 2109 2110 switch (any_repo) { 2111 inline else => |*repo| { 2112 var config = try repo.listConfig(io, allocator); 2113 defer config.deinit(); 2114 2115 const user = config.sections.get("user") orelse return local_author_fallback; 2116 const name = user.get("name") orelse return local_author_fallback; 2117 const email = user.get("email") orelse return local_author_fallback; 2118 return .{ 2119 .name = try arena.allocator().dupe(u8, name), 2120 .email = try arena.allocator().dupe(u8, email), 2121 }; 2122 }, 2123 } 2124 }, 2125 } 2126 } 2127 2128 // per-connection mutable state. each SSH session / web session / local TUI 2129 // run gets its own. 2130 pub const Session = struct { 2131 data: Data = .{}, 2132 // session-lifetime allocations: login/user_id, persisted prefs, queued 2133 // actions. lives as long as the connection. 2134 arena: *std.heap.ArenaAllocator, 2135 // current page's allocations: page data and the page-scoped strings widgets 2136 // build (form actions, links). owned by Nav, swapped on each navigation, so 2137 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 2138 // where a render is one-shot, this points at the same arena as `arena`. 2139 page_arena: *std.heap.ArenaAllocator, 2140 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 2141 // filesystem io and the path to <server>/repos, for opening on-disk repos 2142 // during page construction. both null on wasm, which has no filesystem and 2143 // rebuilds pages from the serialized snapshot rather than from disk. 2144 io: ?std.Io = null, 2145 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 2146 users_dir: ?[]const u8 = null, 2147 // the single on-disk repo this session views, when running in local mode 2148 // (haxy invoked with no arguments inside a repo). null on the server paths, 2149 // which resolve repos from the admin db instead. 2150 local: ?RepoSource = null, 2151 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 2152 // focus id -> the live TextInput, refreshed each frame by the views that own 2153 // inputs. web/wasm form handling looks widgets up here by focus id. 2154 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 2155 back: enum { unavailable, available, requested } = .unavailable, 2156 refresh_requested: bool = false, // set by input (ctrl+r) 2157 // a requested forward navigation. set this (via navigate) to move to a new 2158 // page; Nav.sync builds it and then copies it into current_page, clearing 2159 // this back to null. setting current_page directly only updates the url and 2160 // does not navigate. 2161 next_page: ?RoutablePage = null, 2162 is_terminal: bool = false, // true on remote SSH and local TUI 2163 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 2164 // url. null on the web itself (no footer there). 2165 web_port: ?u16 = null, 2166 // a host operation requested by a widget 2167 host_request: ?HostRequest = null, 2168 quit_requested: bool = false, 2169 2170 pub const HostRequest = union(enum) { 2171 show_copyable_text: []const u8, 2172 sync_events, 2173 undo: RoutablePage.RepoUndoRoute, 2174 }; 2175 2176 pub const FormFeedback = union(enum) { 2177 pub const LoginFailure = enum { unknown_user, wrong_password }; 2178 pub const ThreadFailure = enum { required_title }; 2179 pub const PatchFailure = enum { 2180 required_title, 2181 invalid_target_branch, 2182 invalid_source_branch, 2183 same_branch, 2184 unrelated_branches, 2185 2186 pub fn fromError(err: anyerror) ?PatchFailure { 2187 return switch (err) { 2188 error.InvalidTargetBranch => .invalid_target_branch, 2189 error.InvalidSourceBranch => .invalid_source_branch, 2190 error.SameBranch => .same_branch, 2191 error.UnrelatedBranches => .unrelated_branches, 2192 else => null, 2193 }; 2194 } 2195 }; 2196 2197 pub const RepoFailure = enum { 2198 required_name, 2199 invalid_name, 2200 name_taken, 2201 2202 pub fn fromError(err: anyerror) ?RepoFailure { 2203 return switch (err) { 2204 error.NameEmpty => .required_name, 2205 error.NameTooLong, error.InvalidName => .invalid_name, 2206 error.NameTaken => .name_taken, 2207 else => null, 2208 }; 2209 } 2210 }; 2211 2212 login: struct { 2213 failure: LoginFailure, 2214 username: []const u8, 2215 }, 2216 repo: struct { 2217 failure: RepoFailure, 2218 fields: struct { 2219 name: []const u8, 2220 description: []const u8, 2221 hash: hash.HashKind, 2222 access: evt.Repo.Access, 2223 }, 2224 }, 2225 issue: struct { 2226 failure: ThreadFailure, 2227 fields: ?struct { 2228 title: []const u8, 2229 labels: []const u8, 2230 description: []const u8, 2231 } = null, 2232 }, 2233 patch: struct { 2234 failure: PatchFailure, 2235 fields: ?struct { 2236 title: []const u8, 2237 labels: []const u8, 2238 description: []const u8, 2239 target_branch: []const u8, 2240 source_branch: []const u8 = "", 2241 source_kind: Repo.Patches.Source.Kind = .fork, 2242 } = null, 2243 }, 2244 discussion: struct { 2245 failure: ThreadFailure, 2246 fields: ?struct { 2247 title: []const u8, 2248 labels: []const u8, 2249 description: []const u8, 2250 } = null, 2251 }, 2252 }; 2253 2254 const Self = @This(); 2255 2256 // serializable data sent down to web client 2257 pub const Data = struct { 2258 user_id: ?[]const u8 = null, 2259 // the logged-in user's name, for the views that show it 2260 user_name: ?[]const u8 = null, 2261 // a failed form retained long enough to render it for correction 2262 form_feedback: ?FormFeedback = null, 2263 // a transient local event-sync error 2264 sync_failure: ?[]const u8 = null, 2265 // a failed terminal undo retained on the current tab 2266 undo_failure: ?[]const u8 = null, 2267 current_page: RoutablePage = .default, 2268 // whether to render the ANSI art backdrop 2269 enable_ansi: bool = true, 2270 // the selected ANSI art. the server serializes one image for WASM so 2271 // the browser does not need the whole build-generated collection. 2272 ansi_art: []const u8 = "", 2273 // the bound git-service ports. absent in local mode, where remote urls 2274 // are not shown. 2275 git_http_port: ?u16 = null, 2276 git_ssh_port: ?u16 = null, 2277 git_ssh_prefix: []const u8 = "", 2278 // whether this session views a local repo or the server. unlike `local` 2279 // (the host-side filesystem source) this travels in the snapshot, so 2280 // the wasm side also parses elided link urls and hides the multi-user 2281 // chrome. 2282 host_kind: evt.HostKind = .server, 2283 }; 2284 2285 // a user-initiated state change. widgets enqueue these on the session during 2286 // input instead of mutating state or touching the DB themselves; the host 2287 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 2288 // side effects out of the widget tree and gives every render path one place to 2289 // turn a UI action into a state change + event. 2290 pub const Action = union(enum) { 2291 toggle_ansi, 2292 }; 2293 2294 pub fn init( 2295 arena: *std.heap.ArenaAllocator, 2296 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2297 data: Data, 2298 ) !Self { 2299 var session = Self{ 2300 .data = data, 2301 .arena = arena, 2302 // until a host swaps in a page-scoped arena (Nav does this), page 2303 // allocations land in the session arena. on the web path that's the 2304 // intended behavior, since the whole arena is per-request. 2305 .page_arena = arena, 2306 .admin_repo = repo, 2307 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2308 }; 2309 try session.loadUser(); 2310 return session; 2311 } 2312 2313 // load the logged-in user's name and persisted preferences from the db 2314 pub fn loadUser(self: *Self) !void { 2315 const user_id = self.userId() orelse return; 2316 const moment = self.haxy_moment orelse return; 2317 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2318 self.data.user_name = user.event.name; 2319 self.data.enable_ansi = user.event.enable_ansi; 2320 } 2321 2322 // forget the logged-in user, on a logout or once their account is removed 2323 pub fn logOut(self: *Self) void { 2324 self.data.user_id = null; 2325 self.data.user_name = null; 2326 } 2327 2328 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2329 const bytes = self.data.user_id orelse return null; 2330 if (bytes.len != evt.event_id_size) return null; 2331 var id: [evt.event_id_size]u8 = undefined; 2332 @memcpy(&id, bytes); 2333 return id; 2334 } 2335 2336 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2337 const feedback = self.data.form_feedback orelse return null; 2338 if (std.meta.activeTag(feedback) != tag) return null; 2339 return @field(feedback, @tagName(tag)); 2340 } 2341 2342 pub fn clearFormFeedback(self: *Self) void { 2343 self.data.form_feedback = null; 2344 } 2345 2346 // the actor for a write to the repo `identity` names, or null when the 2347 // session may not write there with at least `min_role`. local mode always 2348 // writes. 2349 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role) !?Actor { 2350 if (self.data.host_kind == .local) { 2351 const author = blk: { 2352 const src = self.local orelse break :blk local_author_fallback; 2353 const io = self.io orelse break :blk local_author_fallback; 2354 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2355 }; 2356 return .{ .author = author, .role = .owner }; 2357 } 2358 const user_id = self.userId() orelse return null; 2359 // the session's moment predates this input, so read the roles afresh 2360 const admin_repo = self.admin_repo orelse return null; 2361 const io = self.io orelse return null; 2362 const users_dir = self.users_dir orelse return null; 2363 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2364 return switch (try authorizeUser(io, moment, self.page_arena, users_dir, user_id, identity, min_role)) { 2365 .actor => |actor| actor, 2366 .login_required, .repo_not_found, .forbidden => null, 2367 }; 2368 } 2369 2370 // where this session's event writes go. a server session always has a users dir 2371 pub fn eventHost(self: *const Self) evt.Host { 2372 return switch (self.data.host_kind) { 2373 .local => .local, 2374 .server => .{ .server = .{ .users_dir = self.users_dir orelse unreachable } }, 2375 }; 2376 } 2377 2378 // queue an action for the host to drain this frame. 2379 pub fn push(self: *Self, action: Action) !void { 2380 try self.pending.append(self.arena.allocator(), action); 2381 } 2382 2383 // apply a single action's in-memory effect only (no persistence). 2384 fn apply(self: *Self, action: Action) void { 2385 switch (action) { 2386 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 2387 } 2388 } 2389 2390 // drain the session's queued actions by applying them to the session. 2391 // used on the wasm path, which has no repo to persist to. 2392 pub fn applyPending(self: *Self) void { 2393 for (self.pending.items) |action| self.apply(action); 2394 self.pending.clearRetainingCapacity(); 2395 } 2396 2397 // drain the session's queued actions by applying them to the session and 2398 // writing them to the db 2399 pub fn applyAndWritePending( 2400 self: *Self, 2401 io: std.Io, 2402 allocator: std.mem.Allocator, 2403 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2404 ) !void { 2405 defer self.pending.clearRetainingCapacity(); 2406 for (self.pending.items) |action| { 2407 self.apply(action); 2408 switch (action) { 2409 .toggle_ansi => if (self.data.user_id) |user_id| { 2410 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, self.users_dir orelse unreachable, user_id); 2411 }, 2412 } 2413 } 2414 } 2415 2416 // reload the moment from the admin repo and re-read user preferences 2417 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2418 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2419 self.haxy_moment = moment; 2420 2421 const user_id = self.userId() orelse return; 2422 var arena = std.heap.ArenaAllocator.init(allocator); 2423 defer arena.deinit(); 2424 const user = (try activeUser(moment, &arena, user_id)) orelse return self.logOut(); 2425 self.data.enable_ansi = user.event.enable_ansi; 2426 } 2427 2428 // request a forward navigation to `route`; the host consumes next_page 2429 // (Nav.sync on the terminal, the wasm tick on the web). 2430 pub fn navigate(self: *Session, route: RoutablePage) !void { 2431 self.clearFormFeedback(); 2432 self.next_page = route; 2433 } 2434 }; 2435 2436 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2437 var nav = try Nav.init(allocator, session); 2438 defer nav.deinit(allocator); 2439 2440 var terminal = try term.Terminal.init(io, allocator); 2441 var terminal_live = true; 2442 defer if (terminal_live) terminal.deinit(io); 2443 2444 // set term as active so it will be properly cooked 2445 // when a panic/segfault happens 2446 term.setActive(&terminal); 2447 defer term.setActive(null); 2448 2449 while (!terminal.shouldQuit()) { 2450 const grid_changed = try terminal.render(&nav.root); 2451 2452 // process any inputs. 2453 // 2454 // if the grid didn't change, then first do a blocking 2455 // read, so the thread will sleep until further input. 2456 // after that, all remaining reads are non-blocking so 2457 // we can process the rest of the queued inputs. 2458 // 2459 // if the grid *did* change, then only do non-blocking 2460 // reads. we do not want to sleep the thread because 2461 // there may be an animation that requires more looping. 2462 var blocking = !grid_changed; 2463 while (try terminal.readKey(io, blocking)) |key| { 2464 blocking = false; 2465 try inputKey(allocator, &nav.root, key, session); 2466 } 2467 2468 if (session.host_request) |request| { 2469 session.host_request = null; 2470 switch (request) { 2471 .show_copyable_text => |copyable_text| { 2472 term.setActive(null); 2473 terminal.deinit(io); 2474 terminal_live = false; 2475 showCopyableText(io, copyable_text) catch |show_err| { 2476 terminal = try term.Terminal.init(io, allocator); 2477 terminal_live = true; 2478 term.setActive(&terminal); 2479 return show_err; 2480 }; 2481 terminal = try term.Terminal.init(io, allocator); 2482 terminal_live = true; 2483 term.setActive(&terminal); 2484 }, 2485 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2486 .sync_events => { 2487 try nav.root.build(allocator, .{ 2488 .min_size = .{ .width = null, .height = null }, 2489 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2490 }, nav.root.getFocus()); 2491 _ = try terminal.render(&nav.root); 2492 try Repo.Events.performSync(allocator, session); 2493 }, 2494 } 2495 } 2496 2497 // persist queued actions when there's a repo; local mode has none, 2498 // so just apply in-memory 2499 if (repo_maybe) |repo| { 2500 try session.applyAndWritePending(io, allocator, repo); 2501 // reload so the next navigation builds its page from the current moment 2502 try session.reloadMoment(allocator, repo); 2503 } else { 2504 session.applyPending(); 2505 } 2506 2507 // reconcile navigation: forward to a new page, or back on escape. 2508 try nav.sync(allocator, session); 2509 2510 // the quit button (on the quit tab) asks the host to tear down. 2511 if (session.quit_requested) terminal.requestQuit(); 2512 2513 try nav.root.build(allocator, .{ 2514 .min_size = .{ .width = null, .height = null }, 2515 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2516 }, nav.root.getFocus()); 2517 } 2518 } 2519 2520 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2521 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2522 null 2523 else 2524 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2525 defer if (tty) |file| file.close(io); 2526 2527 const input = if (tty) |file| file else std.Io.File.stdin(); 2528 const output = if (tty) |file| file else std.Io.File.stdout(); 2529 2530 var write_buf: [1024]u8 = undefined; 2531 var writer = output.writer(io, &write_buf); 2532 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2533 try writer.interface.flush(); 2534 2535 var read_buf: [1]u8 = undefined; 2536 var reader = input.reader(io, &read_buf); 2537 while (true) { 2538 const byte = try reader.interface.takeByte(); 2539 if (byte == '\r' or byte == '\n') return; 2540 } 2541 } 2542 2543 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2544 const root_focus = root.getFocus(); 2545 const focused_before = root_focus.grandchild_id; 2546 var activate_in_page_link = false; 2547 switch (key) { 2548 // request a navigation pop; the host's Nav.sync goes back a page, or 2549 // quits when there's no history left. 2550 .escape => { 2551 session.back = .requested; 2552 return; 2553 }, 2554 .ctrl => |letter| switch (letter) { 2555 'r' => { 2556 session.refresh_requested = true; 2557 return; 2558 }, 2559 else => try root.input(allocator, key, root_focus), 2560 }, 2561 .enter => { 2562 if (root_focus.grandchild_id) |gid| { 2563 // follow a cross-page link 2564 if (crossPageLink(root_focus, gid, session.data)) |route| { 2565 return session.navigate(route); 2566 } 2567 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2568 } 2569 activate_in_page_link = true; 2570 try root.input(allocator, key, root_focus); 2571 }, 2572 .mouse => |mouse| { 2573 if (mouse.action == .press and mouse.action.press == .left) { 2574 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2575 const focus_id = hit.id; 2576 if (hit.kind == .background) { 2577 root_focus.setFocus(focus_id); 2578 return; 2579 } 2580 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2581 if (session.back == .available) session.back = .requested; 2582 return; 2583 } 2584 // follow a cross-page link 2585 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2586 return session.navigate(route); 2587 } 2588 if (rawLink(root_focus, focus_id)) |url| { 2589 if (!mouse.ctrl) try requestRawLink(session, url); 2590 return; 2591 } 2592 root_focus.setFocus(focus_id); 2593 activate_in_page_link = true; 2594 } 2595 // forward the press into the widget tree so buttons (and any 2596 // future click-aware widgets) can react. widgets that don't 2597 // care about presses ignore it. 2598 try root.input(allocator, key, root_focus); 2599 } else { 2600 try root.input(allocator, key, root_focus); 2601 } 2602 }, 2603 else => try root.input(allocator, key, root_focus), 2604 } 2605 2606 const focused_after = root_focus.grandchild_id; 2607 if (!activate_in_page_link and focused_before == focused_after) return; 2608 const focus_id = focused_after orelse return; 2609 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2610 } 2611 2612 // a link to bytes the server serves directly rather than to a page route, so 2613 // it never resolves to a RoutablePage 2614 pub const raw_link_prefix = "ax:"; 2615 2616 // a route already loaded into the current page; focusing it changes the url 2617 // without rebuilding the page 2618 pub const in_page_link_prefix = "ai:"; 2619 2620 // an in-page tab's base route, or the exact current route when it is selected 2621 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2622 const target = if (selected) session.data.current_page else route; 2623 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2624 } 2625 2626 // a button the web renderer covers with a file picker that posts the chosen 2627 // file to the url after the prefix 2628 pub const file_input_prefix = "file:"; 2629 2630 // a submit button that overrides its enclosing form's action 2631 pub const submit_action_prefix = "submit:"; 2632 2633 // one option of a radio field, as `radio:<name>=<value>` 2634 pub const radio_prefix = "radio:"; 2635 2636 // the url an `ax:` link points at, or null for anything else. a host that 2637 // suppresses the anchor's own navigation needs this to follow the link. 2638 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2639 const child = root_focus.children.get(focus_id) orelse return null; 2640 const custom = switch (child.focus.kind) { 2641 .custom => |c| c, 2642 else => return null, 2643 }; 2644 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2645 return custom[raw_link_prefix.len..]; 2646 } 2647 2648 fn requestRawLink(session: *Session, url: []const u8) !void { 2649 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2650 } 2651 2652 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2653 // an absolute url is already complete 2654 if (!std.mem.startsWith(u8, path, "/")) return path; 2655 return if (session.web_port) |port| 2656 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2657 else 2658 path; 2659 } 2660 2661 // if the focus target at focus_id is an `a:` link that should navigate (a 2662 // different parent page, or a different files directory within the repo page), 2663 // return its route; otherwise null. lets a host turn a click / enter on such a 2664 // link into a navigation rather than a focus change. 2665 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2666 const current = data.current_page; 2667 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2668 // a link to a different parent page always navigates; within a page, a 2669 // files-directory / commits-page / list-window change navigates while tab 2670 // links stay in-page. 2671 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2672 return null; 2673 } 2674 2675 // the route carried by an `ai:` focus target, or null for any other target 2676 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2677 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2678 } 2679 2680 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2681 const child = root_focus.children.get(focus_id) orelse return null; 2682 const custom = switch (child.focus.kind) { 2683 .custom => |c| c, 2684 else => return null, 2685 }; 2686 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2687 const path = custom[prefix.len..]; 2688 // local sessions build (and parse) links with the repo identity elided 2689 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2690 } 2691 2692 // a display author, resolved against the admin db at read time 2693 pub const Author = union(enum) { 2694 unknown, 2695 email: []const u8, // an email no user matches 2696 user_name: []const u8, 2697 2698 // the commit author line's email, resolved like initFromEmail 2699 pub fn init( 2700 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2701 arena: *std.heap.ArenaAllocator, 2702 author_line: []const u8, 2703 ) !Author { 2704 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2705 } 2706 2707 // from a record's stored author email: it resolves to the user currently 2708 // holding it, else stays the email. the result is allocated in `arena`, 2709 // so the email may borrow transient memory. 2710 pub fn initFromEmail( 2711 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2712 arena: *std.heap.ArenaAllocator, 2713 author_email: ?[]const u8, 2714 ) !Author { 2715 const email = author_email orelse return .unknown; 2716 if (haxy_moment) |moment| { 2717 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2718 return .{ .user_name = user.name }; 2719 } 2720 } 2721 return .{ .email = try arena.allocator().dupe(u8, email) }; 2722 } 2723 }; 2724 2725 // the "a:" link to user `name`'s page. 2726 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2727 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2728 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2729 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2730 } 2731 2732 // a focusable " author " box showing `author`, linking to their user page 2733 // when it is a known user 2734 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2735 const text = switch (author) { 2736 .unknown => "", 2737 .email, .user_name => |t| t, 2738 }; 2739 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2740 errdefer tb.deinit(allocator); 2741 tb.getFocus().mode = .all; 2742 switch (author) { 2743 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2744 else => {}, 2745 } 2746 return tb; 2747 } 2748 2749 // native-TUI navigation 2750 pub const Nav = struct { 2751 root: Widget, 2752 // backs the current page (its Page data and the page-scoped strings its 2753 // widgets build). owned here, swapped on each navigation. session.page_arena 2754 // tracks whichever of these is current so widgets allocate into it. 2755 arena: *std.heap.ArenaAllocator, 2756 // the login the current root was built for 2757 user_id: ?[evt.event_id_size]u8, 2758 history: std.ArrayList(Entry), 2759 2760 // each retained page keeps its own arena, freed when the page leaves the 2761 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2762 // long-lived session from accumulating every page it ever visited. 2763 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2764 2765 // cap on retained back-history so the chain can't grow memory without bound 2766 const max_history: usize = 16; 2767 2768 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2769 session.back = .unavailable; 2770 const arena = try allocator.create(std.heap.ArenaAllocator); 2771 arena.* = std.heap.ArenaAllocator.init(allocator); 2772 errdefer { 2773 arena.deinit(); 2774 allocator.destroy(arena); 2775 } 2776 2777 return .{ 2778 .root = try initCurrentRoot(allocator, arena, session), 2779 .arena = arena, 2780 .user_id = session.userId(), 2781 .history = .empty, 2782 }; 2783 } 2784 2785 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2786 self.root.deinit(allocator); 2787 freeArena(allocator, self.arena); 2788 for (self.history.items) |*entry| { 2789 entry.root.deinit(allocator); 2790 freeArena(allocator, entry.arena); 2791 } 2792 self.history.deinit(allocator); 2793 } 2794 2795 // rebuild the current page in place from a current moment 2796 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2797 if (session.haxy_moment == null and session.local == null) return; 2798 const arena = try allocator.create(std.heap.ArenaAllocator); 2799 arena.* = std.heap.ArenaAllocator.init(allocator); 2800 errdefer freeArena(allocator, arena); 2801 2802 const new_root = try initCurrentRoot(allocator, arena, session); 2803 2804 self.root.deinit(allocator); 2805 freeArena(allocator, self.arena); 2806 self.root = new_root; 2807 self.arena = arena; 2808 self.user_id = session.userId(); 2809 } 2810 2811 // build the current page's root in `arena`, or the not-found page's when 2812 // the route names nothing 2813 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2814 session.page_arena = arena; 2815 const page = try arena.allocator().create(Page); 2816 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2817 error.NotFound => blk: { 2818 session.data.current_page = .not_found; 2819 break :blk .not_found; 2820 }, 2821 else => |e| return e, 2822 }; 2823 return initRoot(allocator, page, session); 2824 } 2825 2826 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2827 arena.deinit(); 2828 allocator.destroy(arena); 2829 } 2830 2831 // reconcile the displayed root with the session's navigation state. a 2832 // forward nav (current_page moved to a different parent page) pushes the 2833 // current root and builds the new page in a fresh arena; a back request 2834 // frees the current page and restores the previous one. when escape is 2835 // pressed with no history left we switch to the quit tab instead of quitting. 2836 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2837 // refresh: rebuild the current page in place from a current moment 2838 if (session.refresh_requested) { 2839 session.refresh_requested = false; 2840 try self.rebuild(allocator, session); 2841 return; 2842 } 2843 2844 if (session.back == .requested) { 2845 if (self.history.pop()) |entry| { 2846 session.back = if (self.history.items.len == 0) .unavailable else .available; 2847 self.root.deinit(allocator); 2848 freeArena(allocator, self.arena); 2849 self.root = entry.root; 2850 self.arena = entry.arena; 2851 self.user_id = entry.user_id; 2852 session.page_arena = entry.arena; 2853 session.data.current_page = entry.route; 2854 // a page built under another login is rebuilt rather than restored 2855 if (!std.meta.eql(entry.user_id, session.userId())) { 2856 try self.rebuild(allocator, session); 2857 return; 2858 } 2859 chooseAnsiArtForNavigation(session); 2860 return; 2861 } 2862 session.back = .unavailable; 2863 // nothing to go back to; switch to the quit confirmation 2864 const root_focus = self.root.getFocus(); 2865 var iter = root_focus.children.iterator(); 2866 while (iter.next()) |entry| { 2867 switch (entry.value_ptr.focus.kind) { 2868 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2869 root_focus.setFocus(entry.key_ptr.*); 2870 // a Stack only builds its selected child, so the quit 2871 // button isn't in the focus tree until a build runs with 2872 // the quit tab selected. build once, then send a 2873 // synthetic arrow_down to drop focus from the tab onto 2874 // the button so it's ready to confirm. 2875 try self.root.build(allocator, .{ 2876 .min_size = .{ .width = null, .height = 40 }, 2877 .max_size = .{ .width = 80, .height = null }, 2878 }, root_focus); 2879 try self.root.input(allocator, .arrow_down, root_focus); 2880 break; 2881 }, 2882 else => {}, 2883 } 2884 } 2885 return; 2886 } 2887 2888 // forward navigation: navigate() set next_page to the page to move to (a 2889 // cross-page link or tab change crossing pages) 2890 if (session.next_page) |route| { 2891 session.next_page = null; 2892 if (session.haxy_moment == null and session.local == null) return; 2893 const previous_route = session.data.current_page; 2894 // the page we navigated to becomes the current page 2895 session.data.current_page = route; 2896 session.back = .available; 2897 2898 const arena = try allocator.create(std.heap.ArenaAllocator); 2899 arena.* = std.heap.ArenaAllocator.init(allocator); 2900 errdefer freeArena(allocator, arena); 2901 2902 const new_root = try initCurrentRoot(allocator, arena, session); 2903 2904 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2905 // drop the oldest entry (freeing its widget tree and arena) once over cap 2906 if (self.history.items.len > max_history) { 2907 var oldest = self.history.orderedRemove(0); 2908 oldest.root.deinit(allocator); 2909 freeArena(allocator, oldest.arena); 2910 } 2911 self.root = new_root; 2912 self.arena = arena; 2913 self.user_id = session.userId(); 2914 } 2915 } 2916 }; 2917 2918 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2919 const page_widget: Widget = switch (page.*) { 2920 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2921 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2922 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2923 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2924 .not_found => .{ .not_found = try .init(allocator, session) }, 2925 }; 2926 2927 chooseAnsiArtForNavigation(session); 2928 2929 // on the TUI/SSH, the page sits above a one-row footer showing its web 2930 // url, when there's a web UI to point at 2931 var root = if (session.is_terminal) blk: { 2932 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2933 errdefer box.deinit(allocator); 2934 const bg_id = bg_blk: { 2935 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2936 errdefer background.deinit(allocator); 2937 const id = background.getFocus().id; 2938 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2939 break :bg_blk id; 2940 }; 2941 if (session.web_port != null) { 2942 var footer = try widget.Footer.init(allocator, session); 2943 errdefer footer.deinit(allocator); 2944 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2945 } 2946 box.getFocus().child_id = bg_id; 2947 break :blk Widget{ .box = box }; 2948 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2949 errdefer root.deinit(allocator); 2950 2951 // input-owning views build their TextInputs in init, so reset their 2952 // registrations here 2953 session.text_inputs.clearRetainingCapacity(); 2954 2955 try root.build(allocator, .{ 2956 .min_size = .{ .width = null, .height = 40 }, 2957 .max_size = .{ .width = 100, .height = null }, 2958 }, root.getFocus()); 2959 2960 return root; 2961 } 2962 2963 // native and server-side sessions choose from the embedded collection. WASM 2964 // keeps the art serialized by the server instead. 2965 fn chooseAnsiArtForNavigation(session: *Session) void { 2966 if (comptime ansi_art.art.len == 0) return; 2967 if (session.data.current_page == .not_found) { 2968 session.data.ansi_art = ansi_art.not_found; 2969 return; 2970 } 2971 const io = session.io orelse return; 2972 var random: usize = undefined; 2973 io.random(std.mem.asBytes(&random)); 2974 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 2975 } 2976