╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
236 // /auth, or /files/<dir> for a directory in the files tab. the `repo` 237 // variant stores "username/reponame" (root) or 238 // "username/reponame/files/<dir>"; settings/auth store just the pair. 239 if (std.mem.startsWith(u8, path, repo_segment)) { 240 const rest = path[repo_segment.len..]; 241 const user_slash = std.mem.indexOfScalar(u8, rest, '/') orelse return null; 242 const after_user = rest[user_slash + 1 ..]; 243 const repo_slash = std.mem.indexOfScalar(u8, after_user, '/'); 244 const repo_name = if (repo_slash) |s| after_user[0..s] else after_user; 245 // reject an empty username or reponame 246 if (user_slash == 0 or repo_name.len == 0) return null; 247 const pair = rest[0 .. user_slash + 1 + repo_name.len]; // "username/reponame" 248 if (repo_slash) |s| { 249 const sub = after_user[s + 1 ..]; 250 if (std.mem.eql(u8, sub, "refs")) return .{ .repo_refs = .{ 251 .name = Array(repo_route_max_len).from(pair) orelse return null, 252 .kind = refsKind(query), 253 .after = after, 254 } }; 255 if (std.mem.eql(u8, sub, "settings")) return .{ .repo_settings = Array(repo_route_max_len).from(pair) orelse return null }; 256 if (std.mem.eql(u8, sub, "auth")) return .{ .repo_auth = Array(repo_route_max_len).from(pair) orelse return null }; 257 if (std.mem.eql(u8, sub, files_seg)) return repoFilesRoute(pair, ""); // trailing /files == root 258 if (std.mem.startsWith(u8, sub, files_seg ++ "/")) return repoFilesRoute(pair, sub[files_seg.len + 1 ..]); 259 if (std.mem.eql(u8, sub, commits_seg)) return repoCommitsRoute(pair, "", after); // trailing /commits == page 1 260 if (std.mem.startsWith(u8, sub, commits_seg ++ "/")) return repoCommitsRoute(pair, sub[commits_seg.len + 1 ..], after); 261 return null; // unknown sub-path 262 } 263 return repoFilesRoute(pair, ""); 264 } 265 return null; 266 } 267 268 // the refs column a "kind=" query param names; absent or unrecognized reads 269 // as the branches column. 270 fn refsKind(query: ?[]const u8) RefKind { 271 const qs = query orelse return .branch; 272 var it = std.mem.splitScalar(u8, qs, '&'); 273 while (it.next()) |pair| { 274 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 275 if (std.mem.eql(u8, pair[0..eq], "kind") and std.mem.eql(u8, pair[eq + 1 ..], "tag")) return .tag; 276 } 277 return .branch; 278 } 279 280 // read a "key=<uint>" value from a raw "a=1&b=2" query string. 281 fn uintParam(query: ?[]const u8, key: []const u8) ?usize { 282 const qs = query orelse return null; 283 var it = std.mem.splitScalar(u8, qs, '&'); 284 while (it.next()) |pair| { 285 const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; 286 if (std.mem.eql(u8, pair[0..eq], key)) 287 return std.fmt.parseInt(usize, pair[eq + 1 ..], 10) catch null; 288 } 289 return null; 290 } 291 292 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 293 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 294 return switch (a) { 295 .home_users => |a_after| a_after == b.home_users, 296 .home_repos => |a_after| a_after == b.home_repos, 297 .user => |a_name| std.mem.eql(u8, a_name.slice(), b.user.slice()), 298 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 299 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 300 .repo => |a_name| std.mem.eql(u8, a_name.slice(), b.repo.slice()), 301 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and a_c.after == b.repo_commits.after, 302 .repo_refs => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_refs.name.slice()) and a_r.kind == b.repo_refs.kind and a_r.after == b.repo_refs.after, 303 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 304 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 305 else => true, 306 }; 307 } 308 309 // the content a repo route renders: which repo, plus the files directory and 310 // commits page. tab switches keep this fixed (the page already holds every 311 // tab's data); a files-directory or commits-page change alters it. 312 const RepoContent = struct { identity: []const u8, files_dir: []const u8, commits_start: []const u8, commits_after: usize, refs_kind: RefKind, refs_after: usize }; 313 314 fn repoContent(r: RoutablePage) ?RepoContent { 315 const name = switch (r) { 316 .repo, .repo_settings, .repo_auth => |n| n.slice(), 317 .repo_commits => |c| c.name.slice(), 318 .repo_refs => |r2| r2.name.slice(), 319 else => return null, 320 }; 321 const rf = RepoFiles.parse(name) orelse return null; 322 // a 0 offset means no refs pagination, so canonicalize the column to 323 // branch then — otherwise the base /refs route would look like a content 324 // change relative to the same page mirrored with kind=tag. 325 const refs_after = switch (r) { 326 .repo_refs => |r2| r2.after, 327 else => 0, 328 }; 329 return .{ 330 .identity = rf.identity, 331 .files_dir = if (std.meta.activeTag(r) == .repo) rf.dir else "", 332 .commits_start = if (std.meta.activeTag(r) == .repo_commits) repoCommitsStart(name) else "", 333 .commits_after = switch (r) { 334 .repo_commits => |c| c.after, 335 else => 0, 336 }, 337 .refs_kind = switch (r) { 338 .repo_refs => |r2| if (refs_after == 0) .branch else r2.kind, 339 else => .branch, 340 }, 341 .refs_after = refs_after, 342 }; 343 } 344 345 // true when `a` and `b` are both repo routes that render different content 346 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 347 const ca = repoContent(a) orelse return false; 348 const cb = repoContent(b) orelse return false; 349 return !std.mem.eql(u8, ca.identity, cb.identity) or 350 !std.mem.eql(u8, ca.files_dir, cb.files_dir) or 351 !std.mem.eql(u8, ca.commits_start, cb.commits_start) or 352 ca.commits_after != cb.commits_after or 353 ca.refs_kind != cb.refs_kind or 354 ca.refs_after != cb.refs_after; 355 } 356 357 // true when `a` and `b` are the same home list tab paginated to a different 358 // window. switching between the users/repos tabs is in-page (the home page 359 // holds both lists), so only a changed `after` on the same tab navigates. 360 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 361 return switch (a) { 362 .home_users => |aa| switch (b) { 363 .home_users => |bb| aa != bb, 364 else => false, 365 }, 366 .home_repos => |aa| switch (b) { 367 .home_repos => |bb| aa != bb, 368 else => false, 369 }, 370 else => false, 371 }; 372 } 373 374 // serialize as { "kind": <tag>, "name"?: <name>, "after"?: <n> }. the default 375 // union(enum) codec doesn't round-trip here: Stringify emits a void 376 // variant as a bare tag string, but the parser expects an object — so the 377 // server->wasm Snapshot JSON would fail to parse. 378 pub fn jsonStringify(self: RoutablePage, jw: anytype) !void { 379 try jw.beginObject(); 380 try jw.objectField("kind"); 381 try jw.write(@tagName(self)); 382 switch (self) { 383 .user, .user_settings, .user_auth => |name| { 384 try jw.objectField("name"); 385 try jw.write(name.slice()); 386 }, 387 .repo, .repo_settings, .repo_auth => |name| { 388 try jw.objectField("name"); 389 try jw.write(name.slice()); 390 }, 391 .repo_commits => |c| { 392 try jw.objectField("name"); 393 try jw.write(c.name.slice()); 394 try jw.objectField("after"); 395 try jw.write(c.after); 396 }, 397 .repo_refs => |r| { 398 try jw.objectField("name"); 399 try jw.write(r.name.slice()); 400 // "kind" already holds the union tag, so the paginated column 401 // travels under a distinct field. 402 try jw.objectField("ref_kind"); 403 try jw.write(@tagName(r.kind)); 404 try jw.objectField("after"); 405 try jw.write(r.after); 406 }, 407 .home_users, .home_repos => |after| { 408 try jw.objectField("after"); 409 try jw.write(after); 410 }, 411 else => {}, 412 } 413 try jw.endObject(); 414 } 415 416 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !RoutablePage { 417 const Helper = struct { kind: std.meta.Tag(RoutablePage), name: ?[]const u8 = null, after: usize = 0, ref_kind: ?[]const u8 = null }; 418 const helper = try std.json.innerParse(Helper, allocator, source, options); 419 const parseName = struct { 420 // errors must stay within std.json's ParseError set; ValueTooLong 421 // is its member for an over-long field. 422 fn f(comptime max_len: usize, maybe: ?[]const u8) !Array(max_len) { 423 return Array(max_len).from(maybe orelse return error.MissingField) orelse error.ValueTooLong; 424 } 425 }.f; 426 return switch (helper.kind) { 427 .home_users => .{ .home_users = helper.after }, 428 .home_repos => .{ .home_repos = helper.after }, 429 .home_settings => .home_settings, 430 .home_auth => .home_auth, 431 .user => .{ .user = try parseName(evt.User.name_max_len, helper.name) }, 432 .user_settings => .{ .user_settings = try parseName(evt.User.name_max_len, helper.name) }, 433 .user_auth => .{ .user_auth = try parseName(evt.User.name_max_len, helper.name) }, 434 .repo => .{ .repo = try parseName(repo_route_max_len, helper.name) }, 435 .repo_commits => .{ .repo_commits = .{ .name = try parseName(repo_route_max_len, helper.name), .after = helper.after } }, 436 .repo_refs => .{ .repo_refs = .{ 437 .name = try parseName(repo_route_max_len, helper.name), 438 .kind = if (helper.ref_kind) |k| (if (std.mem.eql(u8, k, "tag")) .tag else .branch) else .branch, 439 .after = helper.after, 440 } }, 441 .repo_settings => .{ .repo_settings = try parseName(repo_route_max_len, helper.name) }, 442 .repo_auth => .{ .repo_auth = try parseName(repo_route_max_len, helper.name) }, 443 }; 444 } 445 }; 446 447 // per-connection mutable state. each SSH session / web session / local TUI 448 // run gets its own. 449 pub const Session = struct { 450 data: Data = .{}, 451 // session-lifetime allocations: login/user_id, persisted prefs, queued 452 // actions. lives as long as the connection. 453 arena: *std.heap.ArenaAllocator, 454 // current page's allocations: page data and the page-scoped strings widgets 455 // build (form actions, links). owned by Nav, swapped on each navigation, so 456 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 457 // where a render is one-shot, this points at the same arena as `arena`. 458 page_arena: *std.heap.ArenaAllocator, 459 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 460 // filesystem io and the path to <server>/repos, for opening on-disk repos 461 // during page construction. both null on wasm, which has no filesystem and 462 // rebuilds pages from the serialized snapshot rather than from disk. 463 io: ?std.Io = null, 464 repos_dir: ?[]const u8 = null, 465 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 466 // focus id -> the live TextInput, refreshed each frame by the views that own 467 // inputs. web/wasm form handling looks widgets up here by focus id. 468 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput(Widget)) = .empty, 469 nav_back: bool = false, // set by input (escape) to request the native TUI pop a page; see Nav 470 // a requested forward navigation. set this (via navigate) to move to a new 471 // page; Nav.sync builds it and then copies it into current_page, clearing 472 // this back to null. setting current_page directly only updates the url and 473 // does not navigate. 474 next_page: ?RoutablePage = null, 475 is_terminal: bool = false, // true on remote SSH and local TUI 476 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 477 // url. null on the web itself (no footer there). 478 web_port: ?u16 = null, 479 quit_requested: bool = false, 480 481 const Self = @This(); 482 483 // serializable data sent down to web client 484 pub const Data = struct { 485 user_id: ?[]const u8 = null, 486 // a transient outcome to surface from the last /login POST attempt 487 login_failure: ?Home.Auth.Login.Failure = null, 488 current_page: RoutablePage = .default, 489 // whether to render the ANSI art backdrop 490 enable_ansi: bool = true, 491 }; 492 493 // a user-initiated state change. widgets enqueue these on the session during 494 // input instead of mutating state or touching the DB themselves; the host 495 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 496 // side effects out of the widget tree and gives every render path one place to 497 // turn a UI action into a state change + event. 498 pub const Action = union(enum) { 499 toggle_ansi, 500 }; 501 502 pub fn init( 503 arena: *std.heap.ArenaAllocator, 504 repo: *rp.Repo(.xit, evt.admin_repo_opts), 505 data: Data, 506 ) !Self { 507 var session = Self{ 508 .data = data, 509 .arena = arena, 510 // until a host swaps in a page-scoped arena (Nav does this), page 511 // allocations land in the session arena. on the web path that's the 512 // intended behavior, since the whole arena is per-request. 513 .page_arena = arena, 514 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 515 }; 516 try session.loadUserPrefs(); 517 return session; 518 } 519 520 // load the logged-in user's persisted preferences from the db 521 pub fn loadUserPrefs(self: *Self) !void { 522 const user_id = self.data.user_id orelse return; 523 const moment = self.haxy_moment orelse return; 524 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 525 self.data.enable_ansi = user.enable_ansi; 526 } 527 } 528 529 // queue an action for the host to drain this frame. 530 pub fn push(self: *Self, action: Action) !void { 531 try self.pending.append(self.arena.allocator(), action); 532 } 533 534 // apply a single action's in-memory effect only (no persistence). 535 fn apply(self: *Self, action: Action) void { 536 switch (action) { 537 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 538 } 539 } 540 541 // drain the session's queued actions by applying them to the session. 542 // used on the wasm path, which has no repo to persist to. 543 pub fn applyPending(self: *Self) void { 544 for (self.pending.items) |action| self.apply(action); 545 self.pending.clearRetainingCapacity(); 546 } 547 548 // drain the session's queued actions by applying them to the session and 549 // writing them to the db 550 pub fn applyAndWritePending( 551 self: *Self, 552 io: std.Io, 553 allocator: std.mem.Allocator, 554 repo: *rp.Repo(.xit, evt.admin_repo_opts), 555 ) !void { 556 defer self.pending.clearRetainingCapacity(); 557 for (self.pending.items) |action| { 558 self.apply(action); 559 switch (action) { 560 .toggle_ansi => if (self.data.user_id) |user_id| { 561 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 562 }, 563 } 564 } 565 } 566 567 // request a forward navigation to `route`; the host consumes next_page 568 // (Nav.sync on the terminal, the wasm tick on the web). 569 pub fn navigate(self: *Session, route: RoutablePage) !void { 570 self.next_page = route; 571 } 572 }; 573 574 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session) !void { 575 var nav = try Nav.init(allocator, session); 576 defer nav.deinit(allocator); 577 578 var terminal = try term.Terminal.init(io, allocator); 579 defer terminal.deinit(io); 580 581 // set term as active so it will be properly cooked 582 // when a panic/segfault happens 583 term.setActive(&terminal); 584 defer term.setActive(null); 585 586 var last_size = layout.Size{ .width = 0, .height = 0 }; 587 var last_grid = try Grid.init(allocator, last_size); 588 defer last_grid.deinit(); 589 590 while (!terminal.shouldQuit()) { 591 const grid_changed = try terminal.render(&nav.root, &last_grid, &last_size); 592 593 // process any inputs. 594 // 595 // if the grid didn't change, then first do a blocking 596 // read, so the thread will sleep until further input. 597 // after that, all remaining reads are non-blocking so 598 // we can process the rest of the queued inputs. 599 // 600 // if the grid *did* change, then only do non-blocking 601 // reads. we do not want to sleep the thread because 602 // there may be an animation that requires more looping. 603 var blocking = !grid_changed; 604 while (try terminal.readKey(io, blocking)) |key| { 605 blocking = false; 606 try inputKey(allocator, &nav.root, key, session); 607 } 608 609 // local run has no repo to persist to, so just apply in-memory 610 session.applyPending(); 611 612 // reconcile navigation: forward to a new page, or back on escape. 613 try nav.sync(allocator, session); 614 615 // the quit button (on the quit tab) asks the host to tear down. 616 if (session.quit_requested) terminal.requestQuit(); 617 618 try nav.root.build(allocator, .{ 619 .min_size = .{ .width = null, .height = null }, 620 .max_size = .{ .width = last_size.width, .height = last_size.height }, 621 }, nav.root.getFocus()); 622 } 623 } 624 625 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: inp.Key, session: *Session) !void { 626 switch (key) { 627 // request a navigation pop; the host's Nav.sync goes back a page, or 628 // quits when there's no history left. 629 .escape => session.nav_back = true, 630 .enter => { 631 const root_focus = root.getFocus(); 632 if (root_focus.grandchild_id) |gid| { 633 // follow a cross-page link 634 if (crossPageLink(root_focus, gid, session.data.current_page)) |route| { 635 return session.navigate(route); 636 } 637 } 638 try root.input(allocator, key, root_focus); 639 }, 640 .mouse => |mouse| { 641 if (mouse.action == .press and mouse.action.press == .left) { 642 const root_focus = root.getFocus(); 643 var clicked: ?usize = null; 644 var iter = root_focus.children.iterator(); 645 while (iter.next()) |entry| { 646 const child = entry.value_ptr.*; 647 if (!child.focus.focusable) continue; 648 const r = child.rect; 649 if (mouse.x >= r.x and mouse.y >= r.y and 650 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 651 { 652 clicked = entry.key_ptr.*; 653 break; 654 } 655 } 656 if (clicked) |focus_id| { 657 // follow a cross-page link 658 if (crossPageLink(root_focus, focus_id, session.data.current_page)) |route| { 659 return session.navigate(route); 660 } 661 try root_focus.setFocus(focus_id); 662 } 663 // forward the press into the widget tree so buttons (and any 664 // future click-aware widgets) can react. widgets that don't 665 // care about presses ignore it. 666 try root.input(allocator, key, root.getFocus()); 667 } else { 668 try root.input(allocator, key, root.getFocus()); 669 } 670 }, 671 else => try root.input(allocator, key, root.getFocus()), 672 } 673 } 674 675 // if the focus target at focus_id is an `a:` link that should navigate (a 676 // different parent page, or a different files directory within the repo page), 677 // return its route; otherwise null. lets a host turn a click / enter on such a 678 // link into a navigation rather than a focus change. 679 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, current: RoutablePage) ?RoutablePage { 680 const child = root_focus.children.get(focus_id) orelse return null; 681 const custom = switch (child.focus.kind) { 682 .custom => |c| c, 683 else => return null, 684 }; 685 const a_prefix = "a:"; 686 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 687 const url = custom[a_prefix.len..]; 688 const q = std.mem.indexOfScalar(u8, url, '?'); 689 const route = RoutablePage.fromUrl(if (q) |i| url[0..i] else url, if (q) |i| url[i + 1 ..] else null) orelse return null; 690 // a link to a different parent page always navigates; within a page, a 691 // files-directory / commits-page / list-window change navigates while tab 692 // links stay in-page (header-handled). 693 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current)) return route; 694 return null; 695 } 696 697 // native-TUI navigation 698 pub const Nav = struct { 699 root: Widget, 700 route: RoutablePage, 701 // backs the current page (its Page data and the page-scoped strings its 702 // widgets build). owned here, swapped on each navigation. session.page_arena 703 // tracks whichever of these is current so widgets allocate into it. 704 arena: *std.heap.ArenaAllocator, 705 history: std.ArrayList(Entry), 706 707 // each retained page keeps its own arena, freed when the page leaves the 708 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 709 // long-lived session from accumulating every page it ever visited. 710 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 711 712 // cap on retained back-history so the chain can't grow memory without bound 713 const max_history: usize = 16; 714 715 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 716 const arena = try allocator.create(std.heap.ArenaAllocator); 717 arena.* = std.heap.ArenaAllocator.init(allocator); 718 errdefer { 719 arena.deinit(); 720 allocator.destroy(arena); 721 } 722 723 session.page_arena = arena; 724 const route = session.data.current_page; 725 726 const page = try arena.allocator().create(Page); 727 page.* = try Page.init(arena, session, route); 728 return .{ 729 .root = try initRoot(allocator, page, session), 730 .route = route, 731 .arena = arena, 732 .history = .empty, 733 }; 734 } 735 736 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 737 self.root.deinit(allocator); 738 freeArena(allocator, self.arena); 739 for (self.history.items) |*entry| { 740 entry.root.deinit(allocator); 741 freeArena(allocator, entry.arena); 742 } 743 self.history.deinit(allocator); 744 } 745 746 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 747 arena.deinit(); 748 allocator.destroy(arena); 749 } 750 751 // reconcile the displayed root with the session's navigation state. a 752 // forward nav (current_page moved to a different parent page) pushes the 753 // current root and builds the new page in a fresh arena; a back request 754 // frees the current page and restores the previous one. when escape is 755 // pressed with no history left we switch to the quit tab instead of quitting. 756 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 757 if (session.nav_back) { 758 session.nav_back = false; 759 if (self.history.pop()) |entry| { 760 self.root.deinit(allocator); 761 freeArena(allocator, self.arena); 762 self.root = entry.root; 763 self.route = entry.route; 764 self.arena = entry.arena; 765 session.page_arena = entry.arena; 766 session.data.current_page = entry.route; 767 return; 768 } 769 // nothing to go back to; switch to the quit confirmation 770 const root_focus = self.root.getFocus(); 771 var iter = root_focus.children.iterator(); 772 while (iter.next()) |entry| { 773 switch (entry.value_ptr.focus.kind) { 774 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 775 try root_focus.setFocus(entry.key_ptr.*); 776 // a Stack only builds its selected child, so the quit 777 // button isn't in the focus tree until a build runs with 778 // the quit tab selected. build once, then send a 779 // synthetic arrow_down to drop focus from the tab onto 780 // the button so it's ready to confirm. 781 try self.root.build(allocator, .{ 782 .min_size = .{ .width = null, .height = 40 }, 783 .max_size = .{ .width = 80, .height = null }, 784 }, root_focus); 785 try self.root.input(allocator, .arrow_down, root_focus); 786 break; 787 }, 788 else => {}, 789 } 790 } 791 return; 792 } 793 794 // forward navigation: navigate() set next_page to the page to move to (a 795 // cross-page link or tab change crossing pages) 796 if (session.next_page) |route| { 797 session.next_page = null; 798 if (session.haxy_moment == null) return; 799 // the page we navigated to becomes the current page 800 session.data.current_page = route; 801 802 const arena = try allocator.create(std.heap.ArenaAllocator); 803 arena.* = std.heap.ArenaAllocator.init(allocator); 804 errdefer freeArena(allocator, arena); 805 806 session.page_arena = arena; 807 808 const page = try arena.allocator().create(Page); 809 page.* = try Page.init(arena, session, route); 810 const new_root = try initRoot(allocator, page, session); 811 812 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 813 // drop the oldest entry (freeing its widget tree and arena) once over cap 814 if (self.history.items.len > max_history) { 815 var oldest = self.history.orderedRemove(0); 816 oldest.root.deinit(allocator); 817 freeArena(allocator, oldest.arena); 818 } 819 self.root = new_root; 820 self.route = route; 821 self.arena = arena; 822 } 823 } 824 }; 825 826 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 827 const page_widget: Widget = switch (page.*) { 828 .home => |*p| .{ .home = try .init(allocator, p, session) }, 829 .user => |*p| .{ .user = try .init(allocator, p, session) }, 830 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 831 }; 832 833 const demon_art = @embedFile("embed/demon.ans"); 834 835 // on the TUI/SSH, the page sits above a one-row footer showing the url 836 var root = if (session.is_terminal) blk: { 837 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 838 errdefer box.deinit(allocator); 839 const bg_id = bg_blk: { 840 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 841 errdefer background.deinit(allocator); 842 const id = background.getFocus().id; 843 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 844 break :bg_blk id; 845 }; 846 { 847 var footer = try Footer.init(allocator, session); 848 errdefer footer.deinit(allocator); 849 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 850 } 851 box.getFocus().child_id = bg_id; 852 break :blk Widget{ .box = box }; 853 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 854 errdefer root.deinit(allocator); 855 856 // input-owning views build their TextInputs in init — so reset the 857 // focus-id -> *TextInput map here 858 session.text_inputs.clearRetainingCapacity(); 859 860 try root.build(allocator, .{ 861 .min_size = .{ .width = null, .height = 40 }, 862 .max_size = .{ .width = 80, .height = null }, 863 }, root.getFocus()); 864 if (root.getFocus().child_id) |child_id| { 865 try root.getFocus().setFocus(child_id); 866 } 867 868 return root; 869 } 870 871 pub const Widget = union(enum) { 872 text: wgt.Text(Widget), 873 box: wgt.Box(Widget), 874 text_box: wgt.TextBox(Widget), 875 text_input: wgt.TextInput(Widget), 876 scroll: wgt.Scroll(Widget), 877 stack: wgt.Stack(Widget), 878 flow_box: FlowBox, 879 flow_box_scroll: FlowBox.Scroll, 880 spacer: Spacer, 881 center: Center, 882 ansi_art: AnsiArt, 883 background: AnsiBackground, 884 home: Home.View, 885 user: User.View, 886 repo: Repo.View, 887 quit: Quit.View, 888 title: Title.View, 889 home_header: Home.Header.View, 890 user_header: User.Header.View, 891 repo_header: Repo.Header.View, 892 repo_files: Repo.Files.View, 893 repo_commits: Repo.Commits.View, 894 repo_refs: Repo.Refs.View, 895 home_users: Home.Users.View, 896 home_repos: Home.Repos.View, 897 auth_tab: Home.Header.AuthTab.View, 898 home_settings: Home.Settings.View, 899 home_auth: Home.Auth.View, 900 footer: Footer, 901 902 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 903 switch (self.*) { 904 inline else => |*case| case.deinit(allocator), 905 } 906 } 907 908 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 909 switch (self.*) { 910 inline else => |*case| try case.build(allocator, constraint, root_focus), 911 } 912 } 913 914 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) anyerror!void { 915 switch (self.*) { 916 inline else => |*case| try case.input(allocator, key, root_focus), 917 } 918 } 919 920 pub fn clearGrid(self: *Widget) void { 921 switch (self.*) { 922 inline else => |*case| case.clearGrid(), 923 } 924 } 925 926 pub fn getGrid(self: Widget) ?Grid { 927 switch (self) { 928 inline else => |*case| return case.getGrid(), 929 } 930 } 931 932 pub fn getFocus(self: *Widget) *Focus { 933 switch (self.*) { 934 inline else => |*case| return case.getFocus(), 935 } 936 } 937 }; 938 939 pub const FlowBox = struct { 940 focus: *Focus, 941 grid: ?Grid, 942 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 943 // backs the per-item strings the text boxes borrow: each box's `content` 944 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 945 // wholesale on each setItems so there's no per-string ownership to track. 946 arena: std.heap.ArenaAllocator, 947 // column count from the last build — FlowBox.Scroll.input uses it so arrow 948 // up/down can step by a row's worth of items. 949 last_cols: usize, 950 options: Options, 951 952 const border_rows: usize = 2; 953 954 pub const Options = struct { 955 cell_width: usize = 40, 956 cell_height: usize = 3, 957 }; 958 959 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 960 return .{ 961 .focus = try Focus.create(allocator, .container), 962 .grid = null, 963 .text_boxes = .empty, 964 .arena = std.heap.ArenaAllocator.init(allocator), 965 .last_cols = 1, 966 .options = options, 967 }; 968 } 969 970 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 971 self.focus.destroy(allocator); 972 if (self.grid) |*grid| { 973 grid.deinit(); 974 self.grid = null; 975 } 976 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 977 self.text_boxes.deinit(allocator); 978 self.arena.deinit(); 979 } 980 981 // one flow item: its display text plus an optional link. a non-empty `link` 982 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 983 // the web renderer turns into an anchor. 984 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 985 986 // both the item text and the link are copied into the arena, so the caller's 987 // slices needn't outlive this call. 988 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 989 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 990 self.text_boxes.clearAndFree(allocator); 991 992 // the old text boxes (and their borrowed content/links) are gone now, so 993 // the arena that backed those strings can be reclaimed. 994 _ = self.arena.reset(.retain_capacity); 995 const aa = self.arena.allocator(); 996 997 self.focus.clear(); 998 self.focus.child_id = null; 999 1000 for (items) |item| { 1001 const line = try aa.dupe(u8, item.text); 1002 1003 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1004 errdefer text_box.deinit(allocator); 1005 text_box.getFocus().focusable = true; 1006 1007 if (item.link.len > 0) { 1008 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1009 } 1010 1011 try self.text_boxes.append(allocator, text_box); 1012 } 1013 1014 if (self.text_boxes.items.len > 0) { 1015 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1016 } 1017 } 1018 1019 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1020 self.clearGrid(); 1021 self.focus.clear(); 1022 1023 const cell_width = self.options.cell_width; 1024 const max_width = constraint.max_size.width orelse cell_width; 1025 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1026 self.last_cols = cols; 1027 1028 const count = self.text_boxes.items.len; 1029 if (count == 0) return; 1030 const slot_height = self.options.cell_height + border_rows; 1031 if (slot_height == 0) return; 1032 1033 // build at the slot size so every text box fits a single grid cell 1034 for (self.text_boxes.items) |*tb| { 1035 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1036 try tb.build(allocator, .{ 1037 .min_size = .{ .width = cell_width, .height = null }, 1038 .max_size = .{ .width = cell_width, .height = slot_height }, 1039 }, root_focus); 1040 } 1041 1042 const rows = (count + cols - 1) / cols; 1043 const content_height = rows * slot_height; 1044 const total_width = cols * cell_width; 1045 if (total_width == 0 or content_height == 0) return; 1046 1047 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1048 errdefer grid.deinit(); 1049 1050 for (self.text_boxes.items, 0..) |*tb, i| { 1051 const tb_grid = tb.getGrid() orelse continue; 1052 const col = i % cols; 1053 const row = i / cols; 1054 const cell_x = col * cell_width; 1055 const cell_y = row * slot_height; 1056 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1057 try grid.drawGrid(tb_grid, cell_x, cell_y); 1058 } 1059 1060 self.grid = grid; 1061 } 1062 1063 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1064 _ = self; 1065 _ = allocator; 1066 _ = key; 1067 _ = root_focus; 1068 } 1069 1070 pub fn clearGrid(self: *FlowBox) void { 1071 if (self.grid) |*grid| { 1072 grid.deinit(); 1073 self.grid = null; 1074 } 1075 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1076 } 1077 1078 pub fn getGrid(self: FlowBox) ?Grid { 1079 return self.grid; 1080 } 1081 1082 pub fn getFocus(self: *FlowBox) *Focus { 1083 return self.focus; 1084 } 1085 1086 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1087 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1088 const slot_height = self.options.cell_height + border_rows; 1089 const col = index % self.last_cols; 1090 const row = index / self.last_cols; 1091 return .{ 1092 .x = @intCast(col * self.options.cell_width), 1093 .y = @intCast(row * slot_height), 1094 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1095 }; 1096 } 1097 1098 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1099 for (self.text_boxes.items, 0..) |tb, i| { 1100 if (tb.box.focus.id == focus_id) return i; 1101 } 1102 return null; 1103 } 1104 1105 pub const Scroll = struct { 1106 scroll: wgt.Scroll(Widget), 1107 1108 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1109 var layout_inner = try FlowBox.init(allocator, options); 1110 errdefer layout_inner.deinit(allocator); 1111 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1112 errdefer scroll.deinit(allocator); 1113 return .{ .scroll = scroll }; 1114 } 1115 1116 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1117 self.scroll.deinit(allocator); 1118 } 1119 1120 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1121 self.scroll.x = 0; 1122 self.scroll.y = 0; 1123 try self.inner().setItems(allocator, items); 1124 } 1125 1126 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1127 try self.scroll.build(allocator, constraint, root_focus); 1128 } 1129 1130 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1131 _ = allocator; 1132 const in = self.inner(); 1133 const child_id = in.focus.child_id orelse return; 1134 const current_index = in.indexOfFocusId(child_id) orelse return; 1135 const count = in.text_boxes.items.len; 1136 if (count == 0) return; 1137 const cols = in.last_cols; 1138 const slot_height = in.options.cell_height + FlowBox.border_rows; 1139 1140 var index = current_index; 1141 switch (key) { 1142 .arrow_up => index -|= cols, 1143 .arrow_down => if (index + cols < count) { 1144 index += cols; 1145 }, 1146 .arrow_left => index -|= 1, 1147 .arrow_right => if (index + 1 < count) { 1148 index += 1; 1149 }, 1150 .home => index = 0, 1151 .end => index = count - 1, 1152 .page_up => { 1153 if (self.scroll.grid) |grid| if (slot_height > 0) { 1154 const rows_per_page = grid.size.height / slot_height; 1155 index -|= rows_per_page * cols; 1156 }; 1157 }, 1158 .page_down => { 1159 if (self.scroll.grid) |grid| if (slot_height > 0) { 1160 const rows_per_page = grid.size.height / slot_height; 1161 index = @min(index + rows_per_page * cols, count - 1); 1162 }; 1163 }, 1164 // scroll wheel moves the focused cell by a full row so the 1165 // viewport (via scrollToRect) follows in row-sized steps, 1166 // matching how a scroll wheel feels in a grid view. 1167 .mouse => |mouse| switch (mouse.action) { 1168 .scroll => |dir| switch (dir) { 1169 .up => index -|= cols, 1170 .down => if (index + cols < count) { 1171 index += cols; 1172 }, 1173 }, 1174 else => {}, 1175 }, 1176 else => {}, 1177 } 1178 1179 if (index != current_index) { 1180 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1181 if (in.cellRect(index)) |rect| { 1182 self.scroll.scrollToRect(rect); 1183 } 1184 } 1185 } 1186 1187 pub fn clearGrid(self: *Scroll) void { 1188 self.scroll.clearGrid(); 1189 } 1190 1191 pub fn getGrid(self: Scroll) ?Grid { 1192 return self.scroll.getGrid(); 1193 } 1194 1195 pub fn getFocus(self: *Scroll) *Focus { 1196 return self.scroll.getFocus(); 1197 } 1198 1199 pub fn getSelectedIndex(self: Scroll) ?usize { 1200 const in = self.scroll.child.flow_box; 1201 const child_id = in.focus.child_id orelse return null; 1202 return in.indexOfFocusId(child_id); 1203 } 1204 1205 fn inner(self: *Scroll) *FlowBox { 1206 return &self.scroll.child.flow_box; 1207 } 1208 }; 1209 }; 1210 1211 // an invisible widget that fills the horizontal space granted by its parent. 1212 // used inside Box(horiz) with a min_size so the box reserves space for the 1213 // children that follow, pushing them to the right. 1214 pub const Spacer = struct { 1215 focus: *Focus, 1216 grid: ?Grid, 1217 1218 pub fn init(allocator: std.mem.Allocator) !Spacer { 1219 return .{ 1220 .focus = try Focus.create(allocator, .container), 1221 .grid = null, 1222 }; 1223 } 1224 1225 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1226 self.focus.destroy(allocator); 1227 if (self.grid) |*grid| { 1228 grid.deinit(); 1229 self.grid = null; 1230 } 1231 } 1232 1233 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1234 _ = root_focus; 1235 self.clearGrid(); 1236 const width = constraint.max_size.width orelse return; 1237 if (width == 0) return; 1238 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1239 } 1240 1241 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1242 _ = self; 1243 _ = allocator; 1244 _ = key; 1245 _ = root_focus; 1246 } 1247 1248 pub fn clearGrid(self: *Spacer) void { 1249 if (self.grid) |*grid| { 1250 grid.deinit(); 1251 self.grid = null; 1252 } 1253 } 1254 1255 pub fn getGrid(self: Spacer) ?Grid { 1256 return self.grid; 1257 } 1258 1259 pub fn getFocus(self: *Spacer) *Focus { 1260 return self.focus; 1261 } 1262 }; 1263 1264 // a one-row, non-focusable status bar showing the current page's url 1265 pub const Footer = struct { 1266 focus: *Focus, 1267 grid: ?Grid, 1268 session: *Session, 1269 arena: std.heap.ArenaAllocator, 1270 1271 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1272 return .{ 1273 .focus = try Focus.create(allocator, .container), 1274 .grid = null, 1275 .session = session, 1276 .arena = std.heap.ArenaAllocator.init(allocator), 1277 }; 1278 } 1279 1280 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1281 self.focus.destroy(allocator); 1282 if (self.grid) |*grid| grid.deinit(); 1283 self.arena.deinit(); 1284 } 1285 1286 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1287 _ = root_focus; 1288 self.clearGrid(); 1289 const width = constraint.max_size.width orelse return; 1290 if (width == 0) return; 1291 1292 _ = self.arena.reset(.retain_capacity); 1293 const aa = self.arena.allocator(); 1294 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1295 const text = if (self.session.web_port) |port| 1296 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1297 else 1298 path; 1299 1300 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1301 errdefer grid.deinit(); 1302 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1303 var i: usize = 0; 1304 while (utf8.nextCodepointSlice()) |ch| { 1305 if (i == width) break; 1306 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1307 i += 1; 1308 } 1309 self.grid = grid; 1310 } 1311 1312 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1313 _ = self; 1314 _ = allocator; 1315 _ = key; 1316 _ = root_focus; 1317 } 1318 1319 pub fn clearGrid(self: *Footer) void { 1320 if (self.grid) |*grid| { 1321 grid.deinit(); 1322 self.grid = null; 1323 } 1324 } 1325 1326 pub fn getGrid(self: Footer) ?Grid { 1327 return self.grid; 1328 } 1329 1330 pub fn getFocus(self: *Footer) *Focus { 1331 return self.focus; 1332 } 1333 }; 1334 1335 // a single-child wrapper that builds the child at its natural size and 1336 // positions its grid in the middle of the area granted by the parent 1337 pub const Center = struct { 1338 focus: *Focus, 1339 grid: ?Grid, 1340 child: *Widget, 1341 direction: Direction, 1342 1343 pub const Direction = enum { both, horiz, vert }; 1344 1345 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1346 const child = try allocator.create(Widget); 1347 errdefer allocator.destroy(child); 1348 child.* = child_widget; 1349 return .{ 1350 .focus = try Focus.create(allocator, .container), 1351 .grid = null, 1352 .child = child, 1353 .direction = direction, 1354 }; 1355 } 1356 1357 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1358 self.focus.destroy(allocator); 1359 if (self.grid) |*grid| { 1360 grid.deinit(); 1361 self.grid = null; 1362 } 1363 self.child.deinit(allocator); 1364 allocator.destroy(self.child); 1365 } 1366 1367 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1368 self.clearGrid(); 1369 self.getFocus().clear(); 1370 1371 // build the child without forcing it to fill min_size; it sizes 1372 // itself to its content within the available max. 1373 try self.child.build(allocator, .{ 1374 .min_size = .{ .width = null, .height = null }, 1375 .max_size = constraint.max_size, 1376 }, root_focus); 1377 1378 const child_grid = self.child.getGrid() orelse return; 1379 1380 // prefer max when given; otherwise grow to max(min, child) so that 1381 // when only a min is set (e.g. the wasm path passes viewport rows 1382 // as min_height with no max), we can still vertically center while 1383 // letting taller content extend past the min. 1384 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1385 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1386 if (width == 0 or height == 0) return; 1387 1388 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1389 (width -| child_grid.size.width) / 2 1390 else 1391 0; 1392 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1393 (height -| child_grid.size.height) / 2 1394 else 1395 0; 1396 1397 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1398 errdefer grid.deinit(); 1399 try grid.drawGrid(child_grid, offset_x, offset_y); 1400 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1401 self.getFocus().child_id = self.child.getFocus().id; 1402 1403 self.grid = grid; 1404 } 1405 1406 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1407 try self.child.input(allocator, key, root_focus); 1408 } 1409 1410 pub fn clearGrid(self: *Center) void { 1411 if (self.grid) |*grid| { 1412 grid.deinit(); 1413 self.grid = null; 1414 } 1415 self.child.clearGrid(); 1416 } 1417 1418 pub fn getGrid(self: Center) ?Grid { 1419 return self.grid; 1420 } 1421 1422 pub fn getFocus(self: *Center) *Focus { 1423 return self.focus; 1424 } 1425 }; 1426 1427 // renders truecolor ANSI art 1428 pub const AnsiArt = struct { 1429 focus: *Focus, 1430 grid: ?Grid, 1431 content: []const u8, 1432 1433 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1434 return .{ 1435 .focus = try Focus.create(allocator, .container), 1436 .grid = null, 1437 .content = content, 1438 }; 1439 } 1440 1441 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1442 self.focus.destroy(allocator); 1443 if (self.grid) |*grid| { 1444 grid.deinit(); 1445 self.grid = null; 1446 } 1447 } 1448 1449 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1450 _ = root_focus; 1451 self.clearGrid(); 1452 1453 // parse into rows of cells, then copy into a rectangular grid 1454 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1455 defer { 1456 for (rows.items) |*row| row.deinit(allocator); 1457 rows.deinit(allocator); 1458 } 1459 var row: std.ArrayList(Grid.Cell) = .empty; 1460 errdefer row.deinit(allocator); 1461 1462 var style: Grid.Style = .{}; 1463 var width: usize = 0; 1464 const content = self.content; 1465 var i: usize = 0; 1466 while (i < content.len) { 1467 const byte = content[i]; 1468 if (byte == '\n') { 1469 width = @max(width, row.items.len); 1470 try rows.append(allocator, row); 1471 row = .empty; 1472 i += 1; 1473 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1474 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1475 var j = i + 2; 1476 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1477 if (j >= content.len) { 1478 i = content.len; // malformed trailing escape; stop 1479 } else { 1480 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1481 i = j + 1; 1482 } 1483 } else { 1484 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1485 const end = @min(content.len, i + len); 1486 const rune = content[i..end]; 1487 const transparent = end - i == 1 and rune[0] == ' ' and 1488 style.fg == null and style.bg == null and !style.inverted; 1489 try row.append(allocator, .{ 1490 .rune = if (transparent) null else rune, 1491 .style = style, 1492 }); 1493 i = end; 1494 } 1495 } 1496 // a trailing row with content but no closing newline 1497 if (row.items.len > 0) { 1498 width = @max(width, row.items.len); 1499 try rows.append(allocator, row); 1500 } else { 1501 row.deinit(allocator); 1502 } 1503 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1504 1505 const height = rows.items.len; 1506 if (width == 0 or height == 0) return; 1507 1508 const clamped_w = @min(width, constraint.max_size.width orelse width); 1509 const clamped_h = @min(height, constraint.max_size.height orelse height); 1510 1511 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1512 errdefer grid.deinit(); 1513 for (rows.items[0..clamped_h], 0..) |r, y| { 1514 const n = @min(r.items.len, clamped_w); 1515 for (r.items[0..n], 0..) |cell, x| { 1516 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1517 } 1518 } 1519 self.grid = grid; 1520 } 1521 1522 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1523 _ = self; 1524 _ = allocator; 1525 _ = key; 1526 _ = root_focus; 1527 } 1528 1529 pub fn clearGrid(self: *AnsiArt) void { 1530 if (self.grid) |*grid| { 1531 grid.deinit(); 1532 self.grid = null; 1533 } 1534 } 1535 1536 pub fn getGrid(self: AnsiArt) ?Grid { 1537 return self.grid; 1538 } 1539 1540 pub fn getFocus(self: *AnsiArt) *Focus { 1541 return self.focus; 1542 } 1543 1544 fn applySgr(style: *Grid.Style, params: []const u8) void { 1545 var nums: [16]u32 = undefined; 1546 var n: usize = 0; 1547 var it = std.mem.splitScalar(u8, params, ';'); 1548 while (it.next()) |tok| { 1549 if (n >= nums.len) break; 1550 // an empty parameter (e.g. bare "\x1b[m") means 0 1551 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1552 n += 1; 1553 } 1554 if (n == 0) { 1555 style.* = .{}; 1556 return; 1557 } 1558 var i: usize = 0; 1559 while (i < n) : (i += 1) { 1560 switch (nums[i]) { 1561 0 => style.* = .{}, 1562 7 => style.inverted = true, 1563 27 => style.inverted = false, 1564 39 => style.fg = null, 1565 49 => style.bg = null, 1566 38, 48 => { 1567 // truecolor form: 38;2;r;g;b — anything else is ignored 1568 if (i + 4 < n and nums[i + 1] == 2) { 1569 const c = Grid.Color{ 1570 .r = @truncate(nums[i + 2]), 1571 .g = @truncate(nums[i + 3]), 1572 .b = @truncate(nums[i + 4]), 1573 }; 1574 if (nums[i] == 38) style.fg = c else style.bg = c; 1575 i += 4; 1576 } 1577 }, 1578 else => {}, 1579 } 1580 } 1581 } 1582 }; 1583 1584 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1585 pub const AnsiBackground = struct { 1586 grid: ?Grid, 1587 child: *Widget, 1588 art: AnsiArt, 1589 session: *Session, 1590 1591 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1592 var cw = child_widget; 1593 const child = allocator.create(Widget) catch |e| { 1594 cw.deinit(allocator); 1595 return e; 1596 }; 1597 child.* = cw; 1598 errdefer { 1599 child.deinit(allocator); 1600 allocator.destroy(child); 1601 } 1602 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1603 } 1604 1605 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1606 if (self.grid) |*grid| grid.deinit(); 1607 self.art.deinit(allocator); 1608 self.child.deinit(allocator); 1609 allocator.destroy(self.child); 1610 } 1611 1612 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1613 self.clearGrid(); 1614 1615 // make the wrapped page fill the whole available area 1616 try self.child.build(allocator, .{ 1617 .min_size = .{ 1618 .width = constraint.max_size.width orelse constraint.min_size.width, 1619 .height = constraint.max_size.height orelse constraint.min_size.height, 1620 }, 1621 .max_size = constraint.max_size, 1622 }, root_focus); 1623 1624 if (self.session.data.enable_ansi) { 1625 if (self.child.getGrid()) |fg| { 1626 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1627 } 1628 } 1629 } 1630 1631 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1632 try self.child.input(allocator, key, root_focus); 1633 } 1634 1635 pub fn clearGrid(self: *AnsiBackground) void { 1636 if (self.grid) |*grid| { 1637 grid.deinit(); 1638 self.grid = null; 1639 } 1640 self.child.clearGrid(); 1641 self.art.clearGrid(); 1642 } 1643 1644 pub fn getGrid(self: AnsiBackground) ?Grid { 1645 return self.grid orelse self.child.getGrid(); 1646 } 1647 1648 pub fn getFocus(self: *AnsiBackground) *Focus { 1649 return self.child.getFocus(); 1650 } 1651 1652 // which top corner the art hugs. 1653 const ArtAnchor = enum { top_left, top_right }; 1654 1655 // a foreground cell counts as blank if it's empty or a bare space without styling 1656 fn cellIsBlank(cell: Grid.Cell) bool { 1657 if (cell.rune) |rune| { 1658 return std.mem.eql(u8, rune, " ") and 1659 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1660 } 1661 return true; 1662 } 1663 1664 // the art is dimmed to this fraction of its brightness so foreground text 1665 // stays legible over it 1666 const art_brightness = 45; // percent 1667 1668 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1669 const c = color orelse return null; 1670 return .{ 1671 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1672 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1673 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1674 }; 1675 } 1676 1677 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1678 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1679 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1680 // snap such channels clear of that range so the backdrop doesn't blink or 1681 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1682 // actually render truecolor. 1683 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1684 const c = color orelse return null; 1685 const snap = struct { 1686 fn f(v: u8) u8 { 1687 return if (v >= 1 and v <= 9) 10 else v; 1688 } 1689 }.f; 1690 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1691 } 1692 1693 // composites ANSI art behind a foreground grid 1694 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1695 art.clearGrid(); 1696 try art.build(allocator, .{ 1697 .min_size = .{ .width = null, .height = null }, 1698 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1699 }, root_focus); 1700 1701 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1702 errdefer out.deinit(); 1703 1704 if (art.getGrid()) |art_grid| { 1705 const anchor_x = switch (anchor) { 1706 .top_left => 0, 1707 .top_right => foreground.size.width -| art_grid.size.width, 1708 }; 1709 for (0..art_grid.size.height) |y| { 1710 for (0..art_grid.size.width) |x| { 1711 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1712 if (src.rune == null) continue; 1713 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1714 const dst = &out.cells.items[idx]; 1715 // composite the dimmed art behind every cell so it shows 1716 // through the whole UI: a blank cell becomes the art, while a 1717 // cell with a glyph keeps the glyph but takes the dimmed art as 1718 // its background (replacing whatever background it had, which 1719 // would otherwise obscure the art). 1720 if (cellIsBlank(dst.*)) { 1721 dst.* = src; 1722 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1723 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1724 } else { 1725 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1726 } 1727 } 1728 } 1729 } 1730 return out; 1731 } 1732 }; 1733