╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1315 if (std.mem.startsWith(u8, tail, comment_seg)) { 1316 _ = segments.next(); 1317 const comment_id = tail[comment_seg.len..]; 1318 if (comment_id.len == 0) return null; 1319 const word = params.scanTail(&segments) catch return null; 1320 if (word) |last| { 1321 if (!params.only(&.{})) return null; 1322 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1323 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1324 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1325 return null; 1326 } 1327 if (!params.only(&.{.start})) return null; 1328 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1329 } 1330 } 1331 const word = params.scanTail(&segments) catch return null; 1332 if (word) |tail| { 1333 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1334 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1335 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1336 if (std.mem.eql(u8, tail, "merge")) return if (params.only(&.{})) repoPatchMergeRoute(pair, patch_id) else null; 1337 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1338 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1339 if (std.mem.eql(u8, tail, "resolve")) { 1340 if (!params.only(&.{.theirs})) return null; 1341 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1342 } 1343 return null; 1344 } 1345 if (!params.only(&.{.start})) return null; 1346 return repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null); 1347 } 1348 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1349 const discussion_id = tab[discuss_seg.len..]; 1350 if (discussion_id.len == 0) return null; 1351 if (segments.peek()) |tail| { 1352 if (std.mem.startsWith(u8, tail, comment_seg)) { 1353 _ = segments.next(); 1354 const comment_id = tail[comment_seg.len..]; 1355 if (comment_id.len == 0) return null; 1356 const word = params.scanTail(&segments) catch return null; 1357 if (word) |last| { 1358 if (!params.only(&.{})) return null; 1359 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1360 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1361 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1362 return null; 1363 } 1364 if (!params.only(&.{.start})) return null; 1365 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1366 } 1367 } 1368 const word = params.scanTail(&segments) catch return null; 1369 if (word) |tail| { 1370 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1371 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1372 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1373 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1374 return null; 1375 } 1376 if (!params.only(&.{.start})) return null; 1377 return repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null); 1378 } 1379 if (std.mem.eql(u8, tab, "issues")) { 1380 // the word is a list view; a filter with neither is never emitted. 1381 const word = params.scanTail(&segments) catch return null; 1382 const tag_value = params.values.get(.tag) orelse ""; 1383 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1384 // the conflicts list windows by an issue id, not a number, so it 1385 // reads the start param raw 1386 if (std.mem.eql(u8, w, "conflicts")) { 1387 if (!params.only(&.{.start})) return null; 1388 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1389 } 1390 if (!params.only(&.{.tag})) return null; 1391 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return repoIssuesRoute(pair, status, tag_value, ""); 1392 if (std.mem.eql(u8, w, "tags")) return repoThreadTagsRoute(.issue, pair, tag_value); 1393 if (std.mem.eql(u8, w, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1394 return null; 1395 } 1396 if (std.mem.eql(u8, tab, "patches")) { 1397 const word = params.scanTail(&segments) catch return null; 1398 const tag_value = params.values.get(.tag) orelse ""; 1399 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1400 if (std.mem.eql(u8, view, "conflicts")) { 1401 if (!params.only(&.{.start})) return null; 1402 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1403 } 1404 if (!params.only(&.{.tag})) return null; 1405 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return repoPatchesRoute(pair, status, tag_value, ""); 1406 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.patch, pair, tag_value); 1407 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1408 if (std.mem.eql(u8, view, "drafts")) return if (tag_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1409 return null; 1410 } 1411 if (std.mem.eql(u8, tab, "discussions")) { 1412 const word = params.scanTail(&segments) catch return null; 1413 const tag_value = params.values.get(.tag) orelse ""; 1414 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1415 if (!params.only(&.{.tag})) return null; 1416 if (std.mem.eql(u8, view, "recent")) return repoDiscussionsRoute(pair, tag_value, ""); 1417 if (std.mem.eql(u8, view, "tags")) return repoThreadTagsRoute(.discuss, pair, tag_value); 1418 if (std.mem.eql(u8, view, "new")) return if (tag_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1419 return null; 1420 } 1421 if (std.mem.eql(u8, tab, "events")) { 1422 const view = if (segments.next()) |word| 1423 std.meta.stringToEnum(EventsView, word) orelse return null 1424 else 1425 .active; 1426 return if (segments.next() == null) repoEventsRoute(pair, view, null, "") else null; 1427 } 1428 if (std.mem.startsWith(u8, tab, "event:")) { 1429 const event_id = tab["event:".len..]; 1430 const kind_segment = segments.next() orelse return null; 1431 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1432 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1433 if (event_id.len == 0 or segments.next() != null) return null; 1434 return repoEventsRoute(pair, .active, kind, event_id); 1435 } 1436 if (std.mem.eql(u8, tab, files_seg)) { 1437 params.scanPairs(&segments) catch return null; 1438 if (params.values.get(.patchrev)) |id| { 1439 if (!params.only(&.{ .line, .patchrev })) return null; 1440 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1441 } 1442 if (!params.only(&.{ .line, .branch, .tag, .object })) return null; 1443 const line = params.line() orelse return null; 1444 const dir = pathValue(segments.rest()) orelse return null; 1445 const ref = (params.ref() catch return null) orelse 1446 return if (dir.len == 0) repoFilesRoute(pair, null, "", "", line) else null; 1447 return repoFilesRoute(pair, ref.kind, ref.value, dir, line); 1448 } 1449 if (std.mem.eql(u8, tab, commits_seg)) { 1450 params.scanPairs(&segments) catch return null; 1451 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1452 const base_oid = params.values.get(.base) orelse ""; 1453 const start = params.start() orelse return null; 1454 const ref = (params.ref() catch return null) orelse { 1455 const file = pathValue(segments.rest()) orelse return null; 1456 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "", base_oid) else null; 1457 }; 1458 // the message page has no diff window or file filter 1459 if (std.mem.eql(u8, segments.rest(), message_seg)) 1460 return if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) else null; 1461 const file = pathValue(segments.rest()) orelse return null; 1462 return repoCommitsRoute(pair, ref.kind, ref.value, start, file, base_oid); 1463 } 1464 if (std.mem.eql(u8, tab, "diff")) { 1465 params.scanPairs(&segments) catch return null; 1466 if (params.values.get(.patchrev)) |id| { 1467 if (!params.only(&.{ .start, .patchrev })) return null; 1468 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1469 } 1470 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1471 const base_oid = params.values.get(.base) orelse return null; 1472 const start = params.start() orelse return null; 1473 const ref = params.ref() catch return null; 1474 const file = pathValue(segments.rest()) orelse return null; 1475 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1476 } 1477 return null; // unknown sub-path 1478 } 1479 1480 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1481 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1482 return switch (a) { 1483 .home_users => |a_start| a_start == b.home_users, 1484 .home_repos => |a_start| a_start == b.home_repos, 1485 .user_repos => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_repos.name.slice()) and a_u.start == b.user_repos.start, 1486 .user_forks => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_forks.name.slice()) and a_u.start == b.user_forks.start, 1487 .user_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.user_settings.slice()), 1488 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1489 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1490 a_f.ref_kind == b.repo_files.ref_kind and 1491 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1492 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1493 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1494 a_f.line == b.repo_files.line, 1495 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1496 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1497 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1498 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1499 switch (a_c.content) { 1500 .diff => |a_d| switch (b.repo_commits.content) { 1501 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1502 .message => false, 1503 }, 1504 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1505 }, 1506 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1507 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1508 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1509 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1510 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1511 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1512 .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 std.mem.eql(u8, a_r.from.slice(), b.repo_refs.from.slice()), 1513 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1514 std.mem.eql(u8, a_i.tag.slice(), b.repo_issues.tag.slice()) and 1515 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1516 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1517 a_i.view == b.repo_issues.view and 1518 a_i.comments_start == b.repo_issues.comments_start and 1519 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1520 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1521 std.mem.eql(u8, a_t.tag.slice(), b.repo_discussions.tag.slice()) and 1522 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1523 a_t.view == b.repo_discussions.view and 1524 a_t.comments_start == b.repo_discussions.comments_start and 1525 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1526 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1527 std.mem.eql(u8, a_p.tag.slice(), b.repo_patches.tag.slice()) and 1528 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1529 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1530 a_p.view == b.repo_patches.view and 1531 a_p.comments_start == b.repo_patches.comments_start and 1532 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1533 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1534 a_e.view == b.repo_events.view and 1535 a_e.kind == b.repo_events.kind and 1536 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1537 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1538 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1539 .fork_patch => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_patch.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_patch.id.slice()), 1540 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1541 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1542 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1543 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1544 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1545 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1546 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1547 a_f.line == b.fork_files.line, 1548 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1549 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1550 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1551 switch (a_c.content) { 1552 .diff => |a_d| switch (b.fork_commits.content) { 1553 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1554 .message => false, 1555 }, 1556 .message => std.meta.activeTag(b.fork_commits.content) == .message, 1557 }, 1558 .fork_settings => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_settings.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_settings.id.slice()), 1559 .fork_auth => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_auth.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_auth.id.slice()), 1560 else => true, 1561 }; 1562 } 1563 1564 // true when following a link from `b` to `a` (both repo routes) should reload 1565 // rather than stay in page 1566 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1567 if (a.parent() != .repo or b.parent() != .repo) return false; 1568 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1569 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1570 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1571 return !a.eql(b); 1572 } 1573 1574 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1575 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1576 } 1577 1578 // true when `a` and `b` are the same user paginated to a different repos 1579 // window. switching between a user's tabs is in-page, so 1580 // only a changed `start` on the repos list navigates. 1581 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1582 return switch (a) { 1583 .user_repos => |aa| switch (b) { 1584 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1585 else => false, 1586 }, 1587 .user_forks => |aa| switch (b) { 1588 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1589 else => false, 1590 }, 1591 else => false, 1592 }; 1593 } 1594 1595 // true when `a` and `b` are the same home list tab paginated to a different 1596 // window. switching between the users/repos tabs is in-page (the home page 1597 // holds both lists), so only a changed `after` on the same tab navigates. 1598 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1599 return switch (a) { 1600 .home_users => |aa| switch (b) { 1601 .home_users => |bb| aa != bb, 1602 else => false, 1603 }, 1604 .home_repos => |aa| switch (b) { 1605 .home_repos => |bb| aa != bb, 1606 else => false, 1607 }, 1608 else => false, 1609 }; 1610 } 1611 }; 1612 1613 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1614 fn isUnreserved(c: u8) bool { 1615 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1616 } 1617 1618 // percent-encode a ref name for use as a single url path segment. 1619 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1620 var out: std.Io.Writer.Allocating = .init(aa); 1621 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1622 return out.written(); 1623 } 1624 1625 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1626 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1627 return struct { 1628 pub const hex_len = hash.hexLen(repo_opts.hash); 1629 1630 // the concrete ref/oid (a null request resolves to the default branch) 1631 ref_or_oid: RoutablePage.RefOrOid, 1632 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1633 // the route layer — which splits on '/' — stores them encoded). duped 1634 // into `aa`. 1635 value: []const u8, 1636 // the commit oid it points at 1637 oid: [hex_len]u8, 1638 1639 const Self = @This(); 1640 1641 // resolve a requested ref/oid (null = the repo's default branch) to a 1642 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1643 // the url-encoded form (as it appears in the route). null when it doesn't 1644 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1645 pub fn init( 1646 repo: *rp.Repo(repo_kind, repo_opts), 1647 io: std.Io, 1648 aa: std.mem.Allocator, 1649 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1650 requested_value: []const u8, 1651 ) !?Self { 1652 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1653 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1654 var value: []const u8 = if (requested_ref_or_oid == null) 1655 requested_value 1656 else 1657 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1658 // no ref named: fall back to HEAD's branch (or its oid when detached). 1659 if (requested_ref_or_oid == null) { 1660 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1661 if (repo.head(io, &head_buf)) |head| switch (head) { 1662 .ref => |r| { 1663 ref_or_oid = .branch; 1664 value = r.name; 1665 }, 1666 .oid => |o| { 1667 ref_or_oid = .object; 1668 value = o; 1669 }, 1670 } else |_| {} 1671 } 1672 1673 var oid: [hex_len]u8 = undefined; 1674 switch (ref_or_oid) { 1675 .object => { 1676 if (value.len != hex_len) return null; 1677 @memcpy(&oid, value); 1678 }, 1679 .branch, .tag => { 1680 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1681 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1682 }, 1683 } 1684 // store url-encoded so the route layer can hold the value verbatim. 1685 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1686 } 1687 }; 1688 } 1689 1690 // where a repo page reads its on-disk repo from 1691 pub const RepoSource = struct { 1692 path: []const u8, 1693 repo_kind: rp.RepoKind, 1694 // the user's global git config, so local mode picks up their identity and 1695 // their signing and ssh settings. null on the server paths, which must not 1696 // read whoever runs the server. 1697 global_config_path: ?[]const u8 = null, 1698 1699 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1700 return .{ .path = self.path, .global_config_path = self.global_config_path }; 1701 } 1702 1703 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 1704 if (!xit.ref.validateName(branch)) return false; 1705 return switch (self.repo_kind) { 1706 inline else => |repo_kind| blk: { 1707 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1708 defer any_repo.deinit(io, allocator); 1709 break :blk switch (any_repo) { 1710 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 1711 }; 1712 }, 1713 }; 1714 } 1715 1716 // whether the actor may change the event `id` names 1717 pub fn canModify( 1718 self: RepoSource, 1719 io: std.Io, 1720 allocator: std.mem.Allocator, 1721 actor: Actor, 1722 kind: evt.EventKind, 1723 id: *const [evt.event_id_size]u8, 1724 ) !bool { 1725 // a role that may change an event with no author needs no read 1726 if (actor.role.canModify(actor.author.email, null)) return true; 1727 1728 var arena = std.heap.ArenaAllocator.init(allocator); 1729 defer arena.deinit(); 1730 return switch (self.repo_kind) { 1731 inline else => |repo_kind| blk: { 1732 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 1733 defer any_repo.deinit(io, allocator); 1734 break :blk switch (any_repo) { 1735 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 1736 }; 1737 }, 1738 }; 1739 } 1740 }; 1741 1742 // who a write acts as and the role they hold in its repo 1743 pub const Actor = struct { 1744 author: evt.CommitAuthor, 1745 // local mode has no accounts, so it has no user id 1746 user_id: ?[evt.event_id_size]u8 = null, 1747 role: evt.Repo.Role, 1748 }; 1749 1750 pub const Authorization = union(enum) { 1751 actor: Actor, 1752 login_required, 1753 repo_not_found, 1754 forbidden, 1755 }; 1756 1757 // the account `user_id` names, or null once it has been removed. a user id is 1758 // checked here wherever one enters, so the role lookups can trust theirs. 1759 pub fn activeUser( 1760 moment: evt.AdminDB.HashMap(.read_only), 1761 arena: *std.heap.ArenaAllocator, 1762 user_id: [evt.event_id_size]u8, 1763 ) !?evt.User.Record { 1764 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 1765 return if (user.removed) null else user; 1766 } 1767 1768 // the role the user holds in the repo `identity` names, or null when no repo 1769 // has that identity. a null user is logged out. 1770 pub fn repoRole( 1771 moment: evt.AdminDB.HashMap(.read_only), 1772 arena: *std.heap.ArenaAllocator, 1773 user_id: ?[evt.event_id_size]u8, 1774 identity: []const u8, 1775 ) !?evt.Repo.Role { 1776 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return null; 1777 const repo = (try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, owner_repo.owner, owner_repo.name)) orelse return null; 1778 return evt.Repo.roleOf(repo.repo, user_id); 1779 } 1780 1781 // whether the user may write to the repo `identity` names with at least 1782 // `min_role` 1783 pub fn authorizeUser( 1784 moment: evt.AdminDB.HashMap(.read_only), 1785 arena: *std.heap.ArenaAllocator, 1786 user_id: [evt.event_id_size]u8, 1787 identity: []const u8, 1788 min_role: evt.Repo.Role, 1789 ) !Authorization { 1790 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 1791 1792 const role = (try repoRole(moment, arena, user_id, identity)) orelse return .repo_not_found; 1793 // a repo the user can't read doesn't exist to them 1794 if (role == .none) return .repo_not_found; 1795 if (!role.atLeast(min_role)) return .forbidden; 1796 return .{ .actor = .{ 1797 .author = .{ .name = user.event.name, .email = user.event.email }, 1798 .user_id = user_id, 1799 .role = role, 1800 } }; 1801 } 1802 1803 // events created in local mode have no account behind them, so the commit 1804 // author comes from the repo's config, which the global config feeds into. 1805 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 1806 1807 pub fn localAuthor( 1808 src: RepoSource, 1809 io: std.Io, 1810 allocator: std.mem.Allocator, 1811 arena: *std.heap.ArenaAllocator, 1812 ) !evt.CommitAuthor { 1813 switch (src.repo_kind) { 1814 inline else => |repo_kind| { 1815 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 1816 defer any_repo.deinit(io, allocator); 1817 1818 switch (any_repo) { 1819 inline else => |*repo| { 1820 var config = try repo.listConfig(io, allocator); 1821 defer config.deinit(); 1822 1823 const user = config.sections.get("user") orelse return local_author_fallback; 1824 const name = user.get("name") orelse return local_author_fallback; 1825 const email = user.get("email") orelse return local_author_fallback; 1826 return .{ 1827 .name = try arena.allocator().dupe(u8, name), 1828 .email = try arena.allocator().dupe(u8, email), 1829 }; 1830 }, 1831 } 1832 }, 1833 } 1834 } 1835 1836 // per-connection mutable state. each SSH session / web session / local TUI 1837 // run gets its own. 1838 pub const Session = struct { 1839 data: Data = .{}, 1840 // session-lifetime allocations: login/user_id, persisted prefs, queued 1841 // actions. lives as long as the connection. 1842 arena: *std.heap.ArenaAllocator, 1843 // current page's allocations: page data and the page-scoped strings widgets 1844 // build (form actions, links). owned by Nav, swapped on each navigation, so 1845 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 1846 // where a render is one-shot, this points at the same arena as `arena`. 1847 page_arena: *std.heap.ArenaAllocator, 1848 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 1849 // filesystem io and the path to <server>/repos, for opening on-disk repos 1850 // during page construction. both null on wasm, which has no filesystem and 1851 // rebuilds pages from the serialized snapshot rather than from disk. 1852 io: ?std.Io = null, 1853 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 1854 repos_dir: ?[]const u8 = null, 1855 // the single on-disk repo this session views, when running in local mode 1856 // (haxy invoked with no arguments inside a repo). null on the server paths, 1857 // which resolve repos from the admin db instead. 1858 local: ?RepoSource = null, 1859 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 1860 // focus id -> the live TextInput, refreshed each frame by the views that own 1861 // inputs. web/wasm form handling looks widgets up here by focus id. 1862 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 1863 back: enum { unavailable, available, requested } = .unavailable, 1864 refresh_requested: bool = false, // set by input (ctrl+r) 1865 // a requested forward navigation. set this (via navigate) to move to a new 1866 // page; Nav.sync builds it and then copies it into current_page, clearing 1867 // this back to null. setting current_page directly only updates the url and 1868 // does not navigate. 1869 next_page: ?RoutablePage = null, 1870 is_terminal: bool = false, // true on remote SSH and local TUI 1871 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 1872 // url. null on the web itself (no footer there). 1873 web_port: ?u16 = null, 1874 // a host operation requested by a widget 1875 host_request: ?HostRequest = null, 1876 quit_requested: bool = false, 1877 1878 pub const HostRequest = union(enum) { 1879 show_copyable_text: []const u8, 1880 sync_events, 1881 }; 1882 1883 pub const FormFeedback = union(enum) { 1884 pub const LoginFailure = enum { unknown_user, wrong_password }; 1885 pub const ThreadFailure = enum { required_title }; 1886 pub const PatchFailure = enum { 1887 required_title, 1888 invalid_target_branch, 1889 invalid_source_branch, 1890 same_branch, 1891 unrelated_branches, 1892 1893 pub fn fromError(err: anyerror) ?PatchFailure { 1894 return switch (err) { 1895 error.InvalidTargetBranch => .invalid_target_branch, 1896 error.InvalidSourceBranch => .invalid_source_branch, 1897 error.SameBranch => .same_branch, 1898 error.UnrelatedBranches => .unrelated_branches, 1899 else => null, 1900 }; 1901 } 1902 }; 1903 1904 login: struct { 1905 failure: LoginFailure, 1906 username: []const u8, 1907 }, 1908 issue: struct { 1909 failure: ThreadFailure, 1910 fields: ?struct { 1911 title: []const u8, 1912 tags: []const u8, 1913 description: []const u8, 1914 } = null, 1915 }, 1916 patch: struct { 1917 failure: PatchFailure, 1918 fields: ?struct { 1919 title: []const u8, 1920 tags: []const u8, 1921 description: []const u8, 1922 target_branch: []const u8, 1923 source_branch: []const u8 = "", 1924 source_kind: Repo.Patches.Source.Kind = .fork, 1925 } = null, 1926 }, 1927 discussion: struct { 1928 failure: ThreadFailure, 1929 fields: ?struct { 1930 title: []const u8, 1931 tags: []const u8, 1932 description: []const u8, 1933 } = null, 1934 }, 1935 }; 1936 1937 const Self = @This(); 1938 1939 // serializable data sent down to web client 1940 pub const Data = struct { 1941 user_id: ?[]const u8 = null, 1942 // the logged-in user's name, for the views that show it 1943 user_name: ?[]const u8 = null, 1944 // a failed form retained long enough to render it for correction 1945 form_feedback: ?FormFeedback = null, 1946 // a transient local event-sync error 1947 sync_failure: ?[]const u8 = null, 1948 current_page: RoutablePage = .default, 1949 // whether to render the ANSI art backdrop 1950 enable_ansi: bool = true, 1951 // the selected ANSI art. the server serializes one image for WASM so 1952 // the browser does not need the whole build-generated collection. 1953 ansi_art: []const u8 = "", 1954 // the bound git-service ports. absent in local mode, where remote urls 1955 // are not shown. 1956 git_http_port: ?u16 = null, 1957 git_ssh_port: ?u16 = null, 1958 git_ssh_prefix: []const u8 = "", 1959 // whether this session views a local repo or the server. unlike `local` 1960 // (the host-side filesystem source) this travels in the snapshot, so 1961 // the wasm side also parses elided link urls and hides the multi-user 1962 // chrome. 1963 host_kind: evt.HostKind = .server, 1964 }; 1965 1966 // a user-initiated state change. widgets enqueue these on the session during 1967 // input instead of mutating state or touching the DB themselves; the host 1968 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1969 // side effects out of the widget tree and gives every render path one place to 1970 // turn a UI action into a state change + event. 1971 pub const Action = union(enum) { 1972 toggle_ansi, 1973 }; 1974 1975 pub fn init( 1976 arena: *std.heap.ArenaAllocator, 1977 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1978 data: Data, 1979 ) !Self { 1980 var session = Self{ 1981 .data = data, 1982 .arena = arena, 1983 // until a host swaps in a page-scoped arena (Nav does this), page 1984 // allocations land in the session arena. on the web path that's the 1985 // intended behavior, since the whole arena is per-request. 1986 .page_arena = arena, 1987 .admin_repo = repo, 1988 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1989 }; 1990 try session.loadUser(); 1991 return session; 1992 } 1993 1994 // load the logged-in user's name and persisted preferences from the db 1995 pub fn loadUser(self: *Self) !void { 1996 const user_id = self.userId() orelse return; 1997 const moment = self.haxy_moment orelse return; 1998 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 1999 self.data.user_name = user.event.name; 2000 self.data.enable_ansi = user.event.enable_ansi; 2001 } 2002 2003 // forget the logged-in user, on a logout or once their account is removed 2004 pub fn logOut(self: *Self) void { 2005 self.data.user_id = null; 2006 self.data.user_name = null; 2007 } 2008 2009 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2010 const bytes = self.data.user_id orelse return null; 2011 if (bytes.len != evt.event_id_size) return null; 2012 var id: [evt.event_id_size]u8 = undefined; 2013 @memcpy(&id, bytes); 2014 return id; 2015 } 2016 2017 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2018 const feedback = self.data.form_feedback orelse return null; 2019 if (std.meta.activeTag(feedback) != tag) return null; 2020 return @field(feedback, @tagName(tag)); 2021 } 2022 2023 pub fn clearFormFeedback(self: *Self) void { 2024 self.data.form_feedback = null; 2025 } 2026 2027 // the actor for a write to the repo `identity` names, or null when the 2028 // session may not write there with at least `min_role`. local mode always 2029 // writes. 2030 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role) !?Actor { 2031 if (self.data.host_kind == .local) { 2032 const author = blk: { 2033 const src = self.local orelse break :blk local_author_fallback; 2034 const io = self.io orelse break :blk local_author_fallback; 2035 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2036 }; 2037 return .{ .author = author, .role = .owner }; 2038 } 2039 const user_id = self.userId() orelse return null; 2040 // the session's moment predates this input, so read the roles afresh 2041 const admin_repo = self.admin_repo orelse return null; 2042 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2043 return switch (try authorizeUser(moment, self.page_arena, user_id, identity, min_role)) { 2044 .actor => |actor| actor, 2045 .login_required, .repo_not_found, .forbidden => null, 2046 }; 2047 } 2048 2049 // queue an action for the host to drain this frame. 2050 pub fn push(self: *Self, action: Action) !void { 2051 try self.pending.append(self.arena.allocator(), action); 2052 } 2053 2054 // apply a single action's in-memory effect only (no persistence). 2055 fn apply(self: *Self, action: Action) void { 2056 switch (action) { 2057 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 2058 } 2059 } 2060 2061 // drain the session's queued actions by applying them to the session. 2062 // used on the wasm path, which has no repo to persist to. 2063 pub fn applyPending(self: *Self) void { 2064 for (self.pending.items) |action| self.apply(action); 2065 self.pending.clearRetainingCapacity(); 2066 } 2067 2068 // drain the session's queued actions by applying them to the session and 2069 // writing them to the db 2070 pub fn applyAndWritePending( 2071 self: *Self, 2072 io: std.Io, 2073 allocator: std.mem.Allocator, 2074 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2075 ) !void { 2076 defer self.pending.clearRetainingCapacity(); 2077 for (self.pending.items) |action| { 2078 self.apply(action); 2079 switch (action) { 2080 .toggle_ansi => if (self.data.user_id) |user_id| { 2081 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 2082 }, 2083 } 2084 } 2085 } 2086 2087 // reload the moment from the admin repo and re-read user preferences 2088 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2089 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2090 self.haxy_moment = moment; 2091 2092 const user_id = self.userId() orelse return; 2093 var arena = std.heap.ArenaAllocator.init(allocator); 2094 defer arena.deinit(); 2095 const user = (try activeUser(moment, &arena, user_id)) orelse return self.logOut(); 2096 self.data.enable_ansi = user.event.enable_ansi; 2097 } 2098 2099 // request a forward navigation to `route`; the host consumes next_page 2100 // (Nav.sync on the terminal, the wasm tick on the web). 2101 pub fn navigate(self: *Session, route: RoutablePage) !void { 2102 self.clearFormFeedback(); 2103 self.next_page = route; 2104 } 2105 }; 2106 2107 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2108 var nav = try Nav.init(allocator, session); 2109 defer nav.deinit(allocator); 2110 2111 var terminal = try term.Terminal.init(io, allocator); 2112 var terminal_live = true; 2113 defer if (terminal_live) terminal.deinit(io); 2114 2115 // set term as active so it will be properly cooked 2116 // when a panic/segfault happens 2117 term.setActive(&terminal); 2118 defer term.setActive(null); 2119 2120 while (!terminal.shouldQuit()) { 2121 const grid_changed = try terminal.render(&nav.root); 2122 2123 // process any inputs. 2124 // 2125 // if the grid didn't change, then first do a blocking 2126 // read, so the thread will sleep until further input. 2127 // after that, all remaining reads are non-blocking so 2128 // we can process the rest of the queued inputs. 2129 // 2130 // if the grid *did* change, then only do non-blocking 2131 // reads. we do not want to sleep the thread because 2132 // there may be an animation that requires more looping. 2133 var blocking = !grid_changed; 2134 while (try terminal.readKey(io, blocking)) |key| { 2135 blocking = false; 2136 try inputKey(allocator, &nav.root, key, session); 2137 } 2138 2139 if (session.host_request) |request| { 2140 session.host_request = null; 2141 switch (request) { 2142 .show_copyable_text => |copyable_text| { 2143 term.setActive(null); 2144 terminal.deinit(io); 2145 terminal_live = false; 2146 showCopyableText(io, copyable_text) catch |show_err| { 2147 terminal = try term.Terminal.init(io, allocator); 2148 terminal_live = true; 2149 term.setActive(&terminal); 2150 return show_err; 2151 }; 2152 terminal = try term.Terminal.init(io, allocator); 2153 terminal_live = true; 2154 term.setActive(&terminal); 2155 }, 2156 .sync_events => { 2157 try nav.root.build(allocator, .{ 2158 .min_size = .{ .width = null, .height = null }, 2159 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2160 }, nav.root.getFocus()); 2161 _ = try terminal.render(&nav.root); 2162 try Repo.Events.performSync(allocator, session); 2163 }, 2164 } 2165 } 2166 2167 // persist queued actions when there's a repo; local mode has none, 2168 // so just apply in-memory 2169 if (repo_maybe) |repo| { 2170 try session.applyAndWritePending(io, allocator, repo); 2171 // reload so the next navigation builds its page from the current moment 2172 try session.reloadMoment(allocator, repo); 2173 } else { 2174 session.applyPending(); 2175 } 2176 2177 // reconcile navigation: forward to a new page, or back on escape. 2178 try nav.sync(allocator, session); 2179 2180 // the quit button (on the quit tab) asks the host to tear down. 2181 if (session.quit_requested) terminal.requestQuit(); 2182 2183 try nav.root.build(allocator, .{ 2184 .min_size = .{ .width = null, .height = null }, 2185 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2186 }, nav.root.getFocus()); 2187 } 2188 } 2189 2190 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2191 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2192 null 2193 else 2194 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2195 defer if (tty) |file| file.close(io); 2196 2197 const input = if (tty) |file| file else std.Io.File.stdin(); 2198 const output = if (tty) |file| file else std.Io.File.stdout(); 2199 2200 var write_buf: [1024]u8 = undefined; 2201 var writer = output.writer(io, &write_buf); 2202 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}); 2203 try writer.interface.flush(); 2204 2205 var read_buf: [1]u8 = undefined; 2206 var reader = input.reader(io, &read_buf); 2207 while (true) { 2208 const byte = try reader.interface.takeByte(); 2209 if (byte == '\r' or byte == '\n') return; 2210 } 2211 } 2212 2213 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2214 const root_focus = root.getFocus(); 2215 const focused_before = root_focus.grandchild_id; 2216 var activate_in_page_link = false; 2217 switch (key) { 2218 // request a navigation pop; the host's Nav.sync goes back a page, or 2219 // quits when there's no history left. 2220 .escape => { 2221 session.back = .requested; 2222 return; 2223 }, 2224 .ctrl => |letter| switch (letter) { 2225 'r' => { 2226 session.refresh_requested = true; 2227 return; 2228 }, 2229 else => try root.input(allocator, key, root_focus), 2230 }, 2231 .enter => { 2232 if (root_focus.grandchild_id) |gid| { 2233 // follow a cross-page link 2234 if (crossPageLink(root_focus, gid, session.data)) |route| { 2235 return session.navigate(route); 2236 } 2237 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2238 } 2239 activate_in_page_link = true; 2240 try root.input(allocator, key, root_focus); 2241 }, 2242 .mouse => |mouse| { 2243 if (mouse.action == .press and mouse.action.press == .left) { 2244 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2245 const focus_id = hit.id; 2246 if (hit.kind == .background) { 2247 root_focus.setFocus(focus_id); 2248 return; 2249 } 2250 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2251 if (session.back == .available) session.back = .requested; 2252 return; 2253 } 2254 // follow a cross-page link 2255 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2256 return session.navigate(route); 2257 } 2258 if (rawLink(root_focus, focus_id)) |url| { 2259 if (!mouse.ctrl) try requestRawLink(session, url); 2260 return; 2261 } 2262 root_focus.setFocus(focus_id); 2263 activate_in_page_link = true; 2264 } 2265 // forward the press into the widget tree so buttons (and any 2266 // future click-aware widgets) can react. widgets that don't 2267 // care about presses ignore it. 2268 try root.input(allocator, key, root_focus); 2269 } else { 2270 try root.input(allocator, key, root_focus); 2271 } 2272 }, 2273 else => try root.input(allocator, key, root_focus), 2274 } 2275 2276 const focused_after = root_focus.grandchild_id; 2277 if (!activate_in_page_link and focused_before == focused_after) return; 2278 const focus_id = focused_after orelse return; 2279 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2280 } 2281 2282 // a link to bytes the server serves directly rather than to a page route, so 2283 // it never resolves to a RoutablePage 2284 pub const raw_link_prefix = "ax:"; 2285 2286 // a route already loaded into the current page; focusing it changes the url 2287 // without rebuilding the page 2288 pub const in_page_link_prefix = "ai:"; 2289 2290 // an in-page tab's base route, or the exact current route when it is selected 2291 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2292 const target = if (selected) session.data.current_page else route; 2293 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2294 } 2295 2296 // a button the web renderer covers with a file picker that posts the chosen 2297 // file to the url after the prefix 2298 pub const file_input_prefix = "file:"; 2299 2300 // a submit button that overrides its enclosing form's action 2301 pub const submit_action_prefix = "submit:"; 2302 2303 // the url an `ax:` link points at, or null for anything else. a host that 2304 // suppresses the anchor's own navigation needs this to follow the link. 2305 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2306 const child = root_focus.children.get(focus_id) orelse return null; 2307 const custom = switch (child.focus.kind) { 2308 .custom => |c| c, 2309 else => return null, 2310 }; 2311 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2312 return custom[raw_link_prefix.len..]; 2313 } 2314 2315 fn requestRawLink(session: *Session, url: []const u8) !void { 2316 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2317 } 2318 2319 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2320 return if (session.web_port) |port| 2321 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2322 else 2323 path; 2324 } 2325 2326 // if the focus target at focus_id is an `a:` link that should navigate (a 2327 // different parent page, or a different files directory within the repo page), 2328 // return its route; otherwise null. lets a host turn a click / enter on such a 2329 // link into a navigation rather than a focus change. 2330 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2331 const current = data.current_page; 2332 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2333 // a link to a different parent page always navigates; within a page, a 2334 // files-directory / commits-page / list-window change navigates while tab 2335 // links stay in-page. 2336 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; 2337 return null; 2338 } 2339 2340 // the route carried by an `ai:` focus target, or null for any other target 2341 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2342 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2343 } 2344 2345 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2346 const child = root_focus.children.get(focus_id) orelse return null; 2347 const custom = switch (child.focus.kind) { 2348 .custom => |c| c, 2349 else => return null, 2350 }; 2351 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2352 const path = custom[prefix.len..]; 2353 // local sessions build (and parse) links with the repo identity elided 2354 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2355 } 2356 2357 // a display author, resolved against the admin db at read time 2358 pub const Author = union(enum) { 2359 unknown, 2360 email: []const u8, // an email no user matches 2361 user_name: []const u8, 2362 2363 // the commit author line's email, resolved like initFromEmail 2364 pub fn init( 2365 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2366 arena: *std.heap.ArenaAllocator, 2367 author_line: []const u8, 2368 ) !Author { 2369 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2370 } 2371 2372 // from a record's stored author email: it resolves to the user currently 2373 // holding it, else stays the email. the result is allocated in `arena`, 2374 // so the email may borrow transient memory. 2375 pub fn initFromEmail( 2376 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2377 arena: *std.heap.ArenaAllocator, 2378 author_email: ?[]const u8, 2379 ) !Author { 2380 const email = author_email orelse return .unknown; 2381 if (haxy_moment) |moment| { 2382 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2383 return .{ .user_name = user.event.name }; 2384 } 2385 } 2386 return .{ .email = try arena.allocator().dupe(u8, email) }; 2387 } 2388 }; 2389 2390 // the "a:" link to user `name`'s page. 2391 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2392 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2393 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2394 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2395 } 2396 2397 // a focusable " author " box showing `author`, linking to their user page 2398 // when it is a known user 2399 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2400 const text = switch (author) { 2401 .unknown => "", 2402 .email, .user_name => |t| t, 2403 }; 2404 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2405 errdefer tb.deinit(allocator); 2406 tb.getFocus().mode = .all; 2407 switch (author) { 2408 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2409 else => {}, 2410 } 2411 return tb; 2412 } 2413 2414 // native-TUI navigation 2415 pub const Nav = struct { 2416 root: Widget, 2417 // backs the current page (its Page data and the page-scoped strings its 2418 // widgets build). owned here, swapped on each navigation. session.page_arena 2419 // tracks whichever of these is current so widgets allocate into it. 2420 arena: *std.heap.ArenaAllocator, 2421 history: std.ArrayList(Entry), 2422 2423 // each retained page keeps its own arena, freed when the page leaves the 2424 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2425 // long-lived session from accumulating every page it ever visited. 2426 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2427 2428 // cap on retained back-history so the chain can't grow memory without bound 2429 const max_history: usize = 16; 2430 2431 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2432 session.back = .unavailable; 2433 const arena = try allocator.create(std.heap.ArenaAllocator); 2434 arena.* = std.heap.ArenaAllocator.init(allocator); 2435 errdefer { 2436 arena.deinit(); 2437 allocator.destroy(arena); 2438 } 2439 2440 session.page_arena = arena; 2441 const route = session.data.current_page; 2442 2443 const page = try arena.allocator().create(Page); 2444 page.* = try Page.init(arena, session, route); 2445 return .{ 2446 .root = try initRoot(allocator, page, session), 2447 .arena = arena, 2448 .history = .empty, 2449 }; 2450 } 2451 2452 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2453 self.root.deinit(allocator); 2454 freeArena(allocator, self.arena); 2455 for (self.history.items) |*entry| { 2456 entry.root.deinit(allocator); 2457 freeArena(allocator, entry.arena); 2458 } 2459 self.history.deinit(allocator); 2460 } 2461 2462 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2463 arena.deinit(); 2464 allocator.destroy(arena); 2465 } 2466 2467 // reconcile the displayed root with the session's navigation state. a 2468 // forward nav (current_page moved to a different parent page) pushes the 2469 // current root and builds the new page in a fresh arena; a back request 2470 // frees the current page and restores the previous one. when escape is 2471 // pressed with no history left we switch to the quit tab instead of quitting. 2472 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2473 // refresh: rebuild the current page in place from a current moment 2474 if (session.refresh_requested) { 2475 session.refresh_requested = false; 2476 if (session.haxy_moment != null or session.local != null) { 2477 const arena = try allocator.create(std.heap.ArenaAllocator); 2478 arena.* = std.heap.ArenaAllocator.init(allocator); 2479 errdefer freeArena(allocator, arena); 2480 2481 session.page_arena = arena; 2482 2483 const route = session.data.current_page; 2484 const page = try arena.allocator().create(Page); 2485 page.* = try Page.init(arena, session, route); 2486 const new_root = try initRoot(allocator, page, session); 2487 2488 self.root.deinit(allocator); 2489 freeArena(allocator, self.arena); 2490 self.root = new_root; 2491 self.arena = arena; 2492 } 2493 return; 2494 } 2495 2496 if (session.back == .requested) { 2497 if (self.history.pop()) |entry| { 2498 session.back = if (self.history.items.len == 0) .unavailable else .available; 2499 self.root.deinit(allocator); 2500 freeArena(allocator, self.arena); 2501 self.root = entry.root; 2502 self.arena = entry.arena; 2503 session.page_arena = entry.arena; 2504 session.data.current_page = entry.route; 2505 chooseAnsiArtForNavigation(session); 2506 return; 2507 } 2508 session.back = .unavailable; 2509 // nothing to go back to; switch to the quit confirmation 2510 const root_focus = self.root.getFocus(); 2511 var iter = root_focus.children.iterator(); 2512 while (iter.next()) |entry| { 2513 switch (entry.value_ptr.focus.kind) { 2514 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2515 root_focus.setFocus(entry.key_ptr.*); 2516 // a Stack only builds its selected child, so the quit 2517 // button isn't in the focus tree until a build runs with 2518 // the quit tab selected. build once, then send a 2519 // synthetic arrow_down to drop focus from the tab onto 2520 // the button so it's ready to confirm. 2521 try self.root.build(allocator, .{ 2522 .min_size = .{ .width = null, .height = 40 }, 2523 .max_size = .{ .width = 80, .height = null }, 2524 }, root_focus); 2525 try self.root.input(allocator, .arrow_down, root_focus); 2526 break; 2527 }, 2528 else => {}, 2529 } 2530 } 2531 return; 2532 } 2533 2534 // forward navigation: navigate() set next_page to the page to move to (a 2535 // cross-page link or tab change crossing pages) 2536 if (session.next_page) |route| { 2537 session.next_page = null; 2538 if (session.haxy_moment == null and session.local == null) return; 2539 const previous_route = session.data.current_page; 2540 // the page we navigated to becomes the current page 2541 session.data.current_page = route; 2542 session.back = .available; 2543 2544 const arena = try allocator.create(std.heap.ArenaAllocator); 2545 arena.* = std.heap.ArenaAllocator.init(allocator); 2546 errdefer freeArena(allocator, arena); 2547 2548 session.page_arena = arena; 2549 2550 const page = try arena.allocator().create(Page); 2551 page.* = try Page.init(arena, session, route); 2552 const new_root = try initRoot(allocator, page, session); 2553 2554 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2555 // drop the oldest entry (freeing its widget tree and arena) once over cap 2556 if (self.history.items.len > max_history) { 2557 var oldest = self.history.orderedRemove(0); 2558 oldest.root.deinit(allocator); 2559 freeArena(allocator, oldest.arena); 2560 } 2561 self.root = new_root; 2562 self.arena = arena; 2563 } 2564 } 2565 }; 2566 2567 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2568 const page_widget: Widget = switch (page.*) { 2569 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2570 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2571 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2572 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2573 }; 2574 2575 chooseAnsiArtForNavigation(session); 2576 2577 // on the TUI/SSH, the page sits above a one-row footer showing the url 2578 var root = if (session.is_terminal) blk: { 2579 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2580 errdefer box.deinit(allocator); 2581 const bg_id = bg_blk: { 2582 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2583 errdefer background.deinit(allocator); 2584 const id = background.getFocus().id; 2585 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2586 break :bg_blk id; 2587 }; 2588 { 2589 var footer = try widget.Footer.init(allocator, session); 2590 errdefer footer.deinit(allocator); 2591 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2592 } 2593 box.getFocus().child_id = bg_id; 2594 break :blk Widget{ .box = box }; 2595 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2596 errdefer root.deinit(allocator); 2597 2598 // input-owning views build their TextInputs in init, so reset their 2599 // registrations here 2600 session.text_inputs.clearRetainingCapacity(); 2601 2602 try root.build(allocator, .{ 2603 .min_size = .{ .width = null, .height = 40 }, 2604 .max_size = .{ .width = 100, .height = null }, 2605 }, root.getFocus()); 2606 2607 return root; 2608 } 2609 2610 // native and server-side sessions choose from the embedded collection. WASM 2611 // keeps the art serialized by the server instead. 2612 fn chooseAnsiArtForNavigation(session: *Session) void { 2613 if (comptime ansi_arts.len == 0) return; 2614 const io = session.io orelse return; 2615 var random: usize = undefined; 2616 io.random(std.mem.asBytes(&random)); 2617 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2618 } 2619