╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1213 // process any inputs. 1214 // 1215 // if the grid didn't change, then first do a blocking 1216 // read, so the thread will sleep until further input. 1217 // after that, all remaining reads are non-blocking so 1218 // we can process the rest of the queued inputs. 1219 // 1220 // if the grid *did* change, then only do non-blocking 1221 // reads. we do not want to sleep the thread because 1222 // there may be an animation that requires more looping. 1223 var blocking = !grid_changed; 1224 while (try terminal.readKey(io, blocking)) |key| { 1225 blocking = false; 1226 try inputKey(allocator, &nav.root, key, session); 1227 } 1228 1229 if (session.host_request) |request| { 1230 session.host_request = null; 1231 switch (request) { 1232 .show_copyable_text => |copyable_text| { 1233 term.setActive(null); 1234 terminal.deinit(io); 1235 terminal_live = false; 1236 showCopyableText(io, copyable_text) catch |show_err| { 1237 terminal = try term.Terminal.init(io, allocator); 1238 terminal_live = true; 1239 term.setActive(&terminal); 1240 return show_err; 1241 }; 1242 terminal = try term.Terminal.init(io, allocator); 1243 terminal_live = true; 1244 term.setActive(&terminal); 1245 last_size = .{ .width = 0, .height = 0 }; 1246 }, 1247 .sync_events => { 1248 try nav.root.build(allocator, .{ 1249 .min_size = .{ .width = null, .height = null }, 1250 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1251 }, nav.root.getFocus()); 1252 _ = try terminal.render(&nav.root, &last_grid, &last_size); 1253 try Repo.Events.performSync(allocator, session); 1254 }, 1255 } 1256 } 1257 1258 // persist queued actions when there's a repo; local mode has none, 1259 // so just apply in-memory 1260 if (repo_maybe) |repo| { 1261 try session.applyAndWritePending(io, allocator, repo); 1262 // reload so the next navigation builds its page from the current moment 1263 try session.reloadMoment(allocator, repo); 1264 } else { 1265 session.applyPending(); 1266 } 1267 1268 // reconcile navigation: forward to a new page, or back on escape. 1269 try nav.sync(allocator, session); 1270 1271 // the quit button (on the quit tab) asks the host to tear down. 1272 if (session.quit_requested) terminal.requestQuit(); 1273 1274 try nav.root.build(allocator, .{ 1275 .min_size = .{ .width = null, .height = null }, 1276 .max_size = .{ .width = last_size.width, .height = last_size.height }, 1277 }, nav.root.getFocus()); 1278 } 1279 } 1280 1281 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 1282 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 1283 null 1284 else 1285 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 1286 defer if (tty) |file| file.close(io); 1287 1288 const input = if (tty) |file| file else std.Io.File.stdin(); 1289 const output = if (tty) |file| file else std.Io.File.stdout(); 1290 1291 var write_buf: [1024]u8 = undefined; 1292 var writer = output.writer(io, &write_buf); 1293 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}); 1294 try writer.interface.flush(); 1295 1296 var read_buf: [1]u8 = undefined; 1297 var reader = input.reader(io, &read_buf); 1298 while (true) { 1299 const byte = try reader.interface.takeByte(); 1300 if (byte == '\r' or byte == '\n') return; 1301 } 1302 } 1303 1304 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1305 switch (key) { 1306 // request a navigation pop; the host's Nav.sync goes back a page, or 1307 // quits when there's no history left. 1308 .escape => session.nav_back = true, 1309 .ctrl => |letter| switch (letter) { 1310 'r' => session.refresh_requested = true, 1311 else => try root.input(allocator, key, root.getFocus()), 1312 }, 1313 .enter => { 1314 const root_focus = root.getFocus(); 1315 if (root_focus.grandchild_id) |gid| { 1316 // follow a cross-page link 1317 if (crossPageLink(root_focus, gid, session.data)) |route| { 1318 return session.navigate(route); 1319 } 1320 } 1321 try root.input(allocator, key, root_focus); 1322 }, 1323 .mouse => |mouse| { 1324 if (mouse.action == .press and mouse.action.press == .left) { 1325 const root_focus = root.getFocus(); 1326 var clicked: ?usize = null; 1327 var iter = root_focus.children.iterator(); 1328 while (iter.next()) |entry| { 1329 const child = entry.value_ptr.*; 1330 if (!child.focus.focusable) continue; 1331 const r = child.rect; 1332 if (mouse.x >= r.x and mouse.y >= r.y and 1333 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1334 { 1335 clicked = entry.key_ptr.*; 1336 break; 1337 } 1338 } 1339 if (clicked) |focus_id| { 1340 // follow a cross-page link 1341 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1342 return session.navigate(route); 1343 } 1344 root_focus.setFocus(focus_id); 1345 } 1346 // forward the press into the widget tree so buttons (and any 1347 // future click-aware widgets) can react. widgets that don't 1348 // care about presses ignore it. 1349 try root.input(allocator, key, root.getFocus()); 1350 } else { 1351 try root.input(allocator, key, root.getFocus()); 1352 } 1353 }, 1354 else => try root.input(allocator, key, root.getFocus()), 1355 } 1356 } 1357 1358 // if the focus target at focus_id is an `a:` link that should navigate (a 1359 // different parent page, or a different files directory within the repo page), 1360 // return its route; otherwise null. lets a host turn a click / enter on such a 1361 // link into a navigation rather than a focus change. 1362 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1363 const current = data.current_page; 1364 const child = root_focus.children.get(focus_id) orelse return null; 1365 const custom = switch (child.focus.kind) { 1366 .custom => |c| c, 1367 else => return null, 1368 }; 1369 const a_prefix = "a:"; 1370 if (!std.mem.startsWith(u8, custom, a_prefix)) return null; 1371 const path = custom[a_prefix.len..]; 1372 // local sessions build (and parse) links with the repo identity elided 1373 const route = (if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path)) orelse return null; 1374 // a link to a different parent page always navigates; within a page, a 1375 // files-directory / commits-page / list-window change navigates while tab 1376 // links stay in-page (header-handled). 1377 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1378 return null; 1379 } 1380 1381 // a display author, resolved against the admin db at read time 1382 pub const Author = union(enum) { 1383 unknown, 1384 email: []const u8, // an email no user matches 1385 user_name: []const u8, 1386 1387 // the commit author line's email, resolved like initFromEmail 1388 pub fn init( 1389 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1390 arena: *std.heap.ArenaAllocator, 1391 author_line: []const u8, 1392 ) !Author { 1393 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1394 } 1395 1396 // from a record's stored author email: it resolves to the user currently 1397 // holding it, else stays the email. the result is allocated in `arena`, 1398 // so the email may borrow transient memory. 1399 pub fn initFromEmail( 1400 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1401 arena: *std.heap.ArenaAllocator, 1402 author_email: ?[]const u8, 1403 ) !Author { 1404 const email = author_email orelse return .unknown; 1405 if (haxy_moment) |moment| { 1406 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1407 return .{ .user_name = user.event.name }; 1408 } 1409 } 1410 return .{ .email = try arena.allocator().dupe(u8, email) }; 1411 } 1412 }; 1413 1414 // the "a:" link to user `name`'s page. 1415 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1416 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1417 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1418 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1419 } 1420 1421 // a focusable " author " box showing `author`, linking to their user page 1422 // when it is a known user 1423 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox(Widget) { 1424 const text = switch (author) { 1425 .unknown => "", 1426 .email, .user_name => |t| t, 1427 }; 1428 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1429 errdefer tb.deinit(allocator); 1430 tb.getFocus().focusable = true; 1431 switch (author) { 1432 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1433 else => {}, 1434 } 1435 return tb; 1436 } 1437 1438 // native-TUI navigation 1439 pub const Nav = struct { 1440 root: Widget, 1441 route: RoutablePage, 1442 // backs the current page (its Page data and the page-scoped strings its 1443 // widgets build). owned here, swapped on each navigation. session.page_arena 1444 // tracks whichever of these is current so widgets allocate into it. 1445 arena: *std.heap.ArenaAllocator, 1446 history: std.ArrayList(Entry), 1447 1448 // each retained page keeps its own arena, freed when the page leaves the 1449 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1450 // long-lived session from accumulating every page it ever visited. 1451 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1452 1453 // cap on retained back-history so the chain can't grow memory without bound 1454 const max_history: usize = 16; 1455 1456 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1457 const arena = try allocator.create(std.heap.ArenaAllocator); 1458 arena.* = std.heap.ArenaAllocator.init(allocator); 1459 errdefer { 1460 arena.deinit(); 1461 allocator.destroy(arena); 1462 } 1463 1464 session.page_arena = arena; 1465 const route = session.data.current_page; 1466 1467 const page = try arena.allocator().create(Page); 1468 page.* = try Page.init(arena, session, route); 1469 return .{ 1470 .root = try initRoot(allocator, page, session), 1471 .route = route, 1472 .arena = arena, 1473 .history = .empty, 1474 }; 1475 } 1476 1477 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1478 self.root.deinit(allocator); 1479 freeArena(allocator, self.arena); 1480 for (self.history.items) |*entry| { 1481 entry.root.deinit(allocator); 1482 freeArena(allocator, entry.arena); 1483 } 1484 self.history.deinit(allocator); 1485 } 1486 1487 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1488 arena.deinit(); 1489 allocator.destroy(arena); 1490 } 1491 1492 // reconcile the displayed root with the session's navigation state. a 1493 // forward nav (current_page moved to a different parent page) pushes the 1494 // current root and builds the new page in a fresh arena; a back request 1495 // frees the current page and restores the previous one. when escape is 1496 // pressed with no history left we switch to the quit tab instead of quitting. 1497 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1498 // refresh: rebuild the current page in place from a current moment 1499 if (session.refresh_requested) { 1500 session.refresh_requested = false; 1501 if (session.haxy_moment != null or session.local != null) { 1502 const arena = try allocator.create(std.heap.ArenaAllocator); 1503 arena.* = std.heap.ArenaAllocator.init(allocator); 1504 errdefer freeArena(allocator, arena); 1505 1506 session.page_arena = arena; 1507 1508 const route = session.data.current_page; 1509 const page = try arena.allocator().create(Page); 1510 page.* = try Page.init(arena, session, route); 1511 const new_root = try initRoot(allocator, page, session); 1512 1513 self.root.deinit(allocator); 1514 freeArena(allocator, self.arena); 1515 self.root = new_root; 1516 self.route = route; 1517 self.arena = arena; 1518 } 1519 return; 1520 } 1521 1522 if (session.nav_back) { 1523 session.nav_back = false; 1524 if (self.history.pop()) |entry| { 1525 self.root.deinit(allocator); 1526 freeArena(allocator, self.arena); 1527 self.root = entry.root; 1528 self.route = entry.route; 1529 self.arena = entry.arena; 1530 session.page_arena = entry.arena; 1531 session.data.current_page = entry.route; 1532 return; 1533 } 1534 // nothing to go back to; switch to the quit confirmation 1535 const root_focus = self.root.getFocus(); 1536 var iter = root_focus.children.iterator(); 1537 while (iter.next()) |entry| { 1538 switch (entry.value_ptr.focus.kind) { 1539 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1540 root_focus.setFocus(entry.key_ptr.*); 1541 // a Stack only builds its selected child, so the quit 1542 // button isn't in the focus tree until a build runs with 1543 // the quit tab selected. build once, then send a 1544 // synthetic arrow_down to drop focus from the tab onto 1545 // the button so it's ready to confirm. 1546 try self.root.build(allocator, .{ 1547 .min_size = .{ .width = null, .height = 40 }, 1548 .max_size = .{ .width = 80, .height = null }, 1549 }, root_focus); 1550 try self.root.input(allocator, .arrow_down, root_focus); 1551 break; 1552 }, 1553 else => {}, 1554 } 1555 } 1556 return; 1557 } 1558 1559 // forward navigation: navigate() set next_page to the page to move to (a 1560 // cross-page link or tab change crossing pages) 1561 if (session.next_page) |route| { 1562 session.next_page = null; 1563 if (session.haxy_moment == null and session.local == null) return; 1564 // the page we navigated to becomes the current page 1565 session.data.current_page = route; 1566 1567 const arena = try allocator.create(std.heap.ArenaAllocator); 1568 arena.* = std.heap.ArenaAllocator.init(allocator); 1569 errdefer freeArena(allocator, arena); 1570 1571 session.page_arena = arena; 1572 1573 const page = try arena.allocator().create(Page); 1574 page.* = try Page.init(arena, session, route); 1575 const new_root = try initRoot(allocator, page, session); 1576 1577 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1578 // drop the oldest entry (freeing its widget tree and arena) once over cap 1579 if (self.history.items.len > max_history) { 1580 var oldest = self.history.orderedRemove(0); 1581 oldest.root.deinit(allocator); 1582 freeArena(allocator, oldest.arena); 1583 } 1584 self.root = new_root; 1585 self.route = route; 1586 self.arena = arena; 1587 } 1588 } 1589 }; 1590 1591 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1592 const page_widget: Widget = switch (page.*) { 1593 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1594 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1595 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1596 }; 1597 1598 const demon_art = @embedFile("embed/demon.ans"); 1599 1600 // on the TUI/SSH, the page sits above a one-row footer showing the url 1601 var root = if (session.is_terminal) blk: { 1602 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1603 errdefer box.deinit(allocator); 1604 const bg_id = bg_blk: { 1605 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1606 errdefer background.deinit(allocator); 1607 const id = background.getFocus().id; 1608 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1609 break :bg_blk id; 1610 }; 1611 { 1612 var footer = try Footer.init(allocator, session); 1613 errdefer footer.deinit(allocator); 1614 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1615 } 1616 box.getFocus().child_id = bg_id; 1617 break :blk Widget{ .box = box }; 1618 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1619 errdefer root.deinit(allocator); 1620 1621 // input-owning views build their TextInputs in init — so reset the 1622 // focus-id -> *TextInput map here 1623 session.text_inputs.clearRetainingCapacity(); 1624 session.input_values.clearRetainingCapacity(); 1625 1626 try root.build(allocator, .{ 1627 .min_size = .{ .width = null, .height = 40 }, 1628 .max_size = .{ .width = 100, .height = null }, 1629 }, root.getFocus()); 1630 1631 return root; 1632 } 1633 1634 pub const Widget = union(enum) { 1635 text: wgt.Text(Widget), 1636 box: wgt.Box(Widget), 1637 text_box: wgt.TextBox(Widget), 1638 text_input: wgt.TextInput(Widget), 1639 scroll: wgt.Scroll(Widget), 1640 stack: wgt.Stack(Widget), 1641 flow_box: FlowBox, 1642 flow_box_scroll: FlowBox.Scroll, 1643 tag_flow: TagFlow, 1644 spacer: Spacer, 1645 center: Center, 1646 section_label: SectionLabel, 1647 submit_button: SubmitButton, 1648 background: AnsiBackground, 1649 home: Home.View, 1650 user: User.View, 1651 repo: Repo.View, 1652 quit: Quit.View, 1653 unauthorized: Unauthorized.View, 1654 title: Title.View, 1655 sub_title: SubTitle.View, 1656 home_header: Home.Header.View, 1657 user_header: User.Header.View, 1658 repo_header: Repo.Header.View, 1659 repo_files_header: Repo.Files.Header.View, 1660 repo_commits_header: Repo.Commits.Header.View, 1661 repo_issues_header: Repo.Issues.Header, 1662 repo_files: Repo.Files.View, 1663 repo_commits: Repo.Commits.View, 1664 repo_refs: Repo.Refs.View, 1665 repo_issues: Repo.Issues.View, 1666 repo_events_header: Repo.Events.Header.View, 1667 repo_events: Repo.Events.View, 1668 repo_comment: Repo.Comment.Item, 1669 clone_url: CloneUrl.View, 1670 home_users: Home.Users.View, 1671 home_repos: Home.Repos.View, 1672 auth_tab: Home.Header.AuthTab.View, 1673 home_settings: Home.Settings.View, 1674 home_auth: Home.Auth.View, 1675 footer: Footer, 1676 1677 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1678 switch (self.*) { 1679 inline else => |*case| case.deinit(allocator), 1680 } 1681 } 1682 1683 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1684 switch (self.*) { 1685 inline else => |*case| try case.build(allocator, constraint, root_focus), 1686 } 1687 } 1688 1689 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1690 switch (self.*) { 1691 inline else => |*case| try case.input(allocator, key, root_focus), 1692 } 1693 } 1694 1695 pub fn clearGrid(self: *Widget) void { 1696 switch (self.*) { 1697 inline else => |*case| case.clearGrid(), 1698 } 1699 } 1700 1701 pub fn getGrid(self: Widget) ?Grid { 1702 switch (self) { 1703 inline else => |*case| return case.getGrid(), 1704 } 1705 } 1706 1707 pub fn getFocus(self: *Widget) *Focus { 1708 switch (self.*) { 1709 inline else => |*case| return case.getFocus(), 1710 } 1711 } 1712 }; 1713 1714 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1715 // `scroll` to keep it visible. 1716 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1717 const keys = box.children.keys(); 1718 if (keys.len == 0) return; 1719 const cur_id = box.getFocus().child_id orelse return; 1720 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1721 const last: isize = @intCast(keys.len - 1); 1722 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1723 if (next == @as(usize, @intCast(cur))) return; 1724 root_focus.setFocus(keys[next]); 1725 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1726 } 1727 1728 pub const FlowBox = struct { 1729 focus: *Focus, 1730 grid: ?Grid, 1731 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1732 // backs the per-item strings the text boxes borrow: each box's `content` 1733 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1734 // wholesale on each setItems so there's no per-string ownership to track. 1735 arena: std.heap.ArenaAllocator, 1736 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1737 // up/down can step by a row's worth of items. 1738 last_cols: usize, 1739 options: Options, 1740 1741 const border_rows: usize = 2; 1742 1743 pub const Options = struct { 1744 cell_width: usize = 40, 1745 cell_height: usize = 3, 1746 }; 1747 1748 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1749 return .{ 1750 .focus = try Focus.create(allocator, .container), 1751 .grid = null, 1752 .text_boxes = .empty, 1753 .arena = std.heap.ArenaAllocator.init(allocator), 1754 .last_cols = 1, 1755 .options = options, 1756 }; 1757 } 1758 1759 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1760 self.focus.destroy(allocator); 1761 if (self.grid) |*grid| { 1762 grid.deinit(); 1763 self.grid = null; 1764 } 1765 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1766 self.text_boxes.deinit(allocator); 1767 self.arena.deinit(); 1768 } 1769 1770 // one flow item: its display text plus an optional link. a non-empty `link` 1771 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1772 // the web renderer turns into an anchor. 1773 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1774 1775 // both the item text and the link are copied into the arena, so the caller's 1776 // slices needn't outlive this call. 1777 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1778 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1779 self.text_boxes.clearAndFree(allocator); 1780 1781 // the old text boxes (and their borrowed content/links) are gone now, so 1782 // the arena that backed those strings can be reclaimed. 1783 _ = self.arena.reset(.retain_capacity); 1784 const aa = self.arena.allocator(); 1785 1786 self.focus.clear(); 1787 self.focus.child_id = null; 1788 1789 for (items) |item| { 1790 const line = try aa.dupe(u8, item.text); 1791 1792 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1793 errdefer text_box.deinit(allocator); 1794 text_box.getFocus().focusable = true; 1795 1796 if (item.link.len > 0) { 1797 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1798 } 1799 1800 try self.text_boxes.append(allocator, text_box); 1801 } 1802 1803 if (self.text_boxes.items.len > 0) { 1804 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1805 } 1806 } 1807 1808 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1809 self.clearGrid(); 1810 self.focus.clear(); 1811 1812 const cell_width = self.options.cell_width; 1813 const max_width = constraint.max_size.width orelse cell_width; 1814 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1815 self.last_cols = cols; 1816 1817 const count = self.text_boxes.items.len; 1818 if (count == 0) return; 1819 const slot_height = self.options.cell_height + border_rows; 1820 if (slot_height == 0) return; 1821 1822 // build at the slot size so every text box fits a single grid cell 1823 for (self.text_boxes.items) |*tb| { 1824 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1825 try tb.build(allocator, .{ 1826 .min_size = .{ .width = cell_width, .height = null }, 1827 .max_size = .{ .width = cell_width, .height = slot_height }, 1828 }, root_focus); 1829 } 1830 1831 const rows = (count + cols - 1) / cols; 1832 const content_height = rows * slot_height; 1833 const total_width = cols * cell_width; 1834 if (total_width == 0 or content_height == 0) return; 1835 1836 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1837 errdefer grid.deinit(); 1838 1839 for (self.text_boxes.items, 0..) |*tb, i| { 1840 const tb_grid = tb.getGrid() orelse continue; 1841 const col = i % cols; 1842 const row = i / cols; 1843 const cell_x = col * cell_width; 1844 const cell_y = row * slot_height; 1845 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1846 try grid.drawGrid(tb_grid, cell_x, cell_y); 1847 } 1848 1849 self.grid = grid; 1850 } 1851 1852 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1853 _ = self; 1854 _ = allocator; 1855 _ = key; 1856 _ = root_focus; 1857 } 1858 1859 pub fn clearGrid(self: *FlowBox) void { 1860 if (self.grid) |*grid| { 1861 grid.deinit(); 1862 self.grid = null; 1863 } 1864 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1865 } 1866 1867 pub fn getGrid(self: FlowBox) ?Grid { 1868 return self.grid; 1869 } 1870 1871 pub fn getFocus(self: *FlowBox) *Focus { 1872 return self.focus; 1873 } 1874 1875 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1876 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1877 const slot_height = self.options.cell_height + border_rows; 1878 const col = index % self.last_cols; 1879 const row = index / self.last_cols; 1880 return .{ 1881 .x = @intCast(col * self.options.cell_width), 1882 .y = @intCast(row * slot_height), 1883 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1884 }; 1885 } 1886 1887 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1888 for (self.text_boxes.items, 0..) |tb, i| { 1889 if (tb.box.focus.id == focus_id) return i; 1890 } 1891 return null; 1892 } 1893 1894 pub const Scroll = struct { 1895 scroll: wgt.Scroll(Widget), 1896 1897 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1898 var layout_inner = try FlowBox.init(allocator, options); 1899 errdefer layout_inner.deinit(allocator); 1900 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1901 errdefer scroll.deinit(allocator); 1902 return .{ .scroll = scroll }; 1903 } 1904 1905 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1906 self.scroll.deinit(allocator); 1907 } 1908 1909 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1910 self.scroll.x = 0; 1911 self.scroll.y = 0; 1912 try self.inner().setItems(allocator, items); 1913 } 1914 1915 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1916 try self.scroll.build(allocator, constraint, root_focus); 1917 } 1918 1919 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1920 _ = allocator; 1921 const in = self.inner(); 1922 const child_id = in.focus.child_id orelse return; 1923 const current_index = in.indexOfFocusId(child_id) orelse return; 1924 const count = in.text_boxes.items.len; 1925 if (count == 0) return; 1926 const cols = in.last_cols; 1927 const slot_height = in.options.cell_height + FlowBox.border_rows; 1928 1929 var index = current_index; 1930 switch (key) { 1931 .arrow_up => index -|= cols, 1932 .arrow_down => if (index + cols < count) { 1933 index += cols; 1934 }, 1935 .arrow_left => index -|= 1, 1936 .arrow_right => if (index + 1 < count) { 1937 index += 1; 1938 }, 1939 .home => index = 0, 1940 .end => index = count - 1, 1941 .page_up => { 1942 if (self.scroll.grid) |grid| if (slot_height > 0) { 1943 const rows_per_page = grid.size.height / slot_height; 1944 index -|= rows_per_page * cols; 1945 }; 1946 }, 1947 .page_down => { 1948 if (self.scroll.grid) |grid| if (slot_height > 0) { 1949 const rows_per_page = grid.size.height / slot_height; 1950 index = @min(index + rows_per_page * cols, count - 1); 1951 }; 1952 }, 1953 // scroll wheel moves the focused cell by a full row so the 1954 // viewport (via scrollToRect) follows in row-sized steps, 1955 // matching how a scroll wheel feels in a grid view. 1956 .mouse => |mouse| switch (mouse.action) { 1957 .scroll => |dir| switch (dir) { 1958 .up => index -|= cols, 1959 .down => if (index + cols < count) { 1960 index += cols; 1961 }, 1962 }, 1963 else => {}, 1964 }, 1965 else => {}, 1966 } 1967 1968 if (index != current_index) { 1969 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1970 if (in.cellRect(index)) |rect| { 1971 self.scroll.scrollToRect(rect); 1972 } 1973 } 1974 } 1975 1976 pub fn clearGrid(self: *Scroll) void { 1977 self.scroll.clearGrid(); 1978 } 1979 1980 pub fn getGrid(self: Scroll) ?Grid { 1981 return self.scroll.getGrid(); 1982 } 1983 1984 pub fn getFocus(self: *Scroll) *Focus { 1985 return self.scroll.getFocus(); 1986 } 1987 1988 pub fn getSelectedIndex(self: Scroll) ?usize { 1989 const in = self.scroll.child.flow_box; 1990 const child_id = in.focus.child_id orelse return null; 1991 return in.indexOfFocusId(child_id); 1992 } 1993 1994 fn inner(self: *Scroll) *FlowBox { 1995 return &self.scroll.child.flow_box; 1996 } 1997 }; 1998 }; 1999 2000 // a left-to-right flow of variable-width focusable items that wraps at the 2001 // available width, like words in a paragraph. unlike FlowBox there is no cell 2002 // grid: each item is as wide as its text. selection movement is driven by the 2003 // owning view via indexOfFocusId/verticalNeighbor. 2004 pub const TagFlow = struct { 2005 focus: *Focus, 2006 grid: ?Grid, 2007 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 2008 // last build's item rects (content space), for vertical navigation and 2009 // scroll-into-view. 2010 rects: std.ArrayList(layout.IRect), 2011 // backs the per-item strings the text boxes borrow. 2012 arena: std.heap.ArenaAllocator, 2013 2014 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 2015 2016 pub fn init(allocator: std.mem.Allocator) !TagFlow { 2017 return .{ 2018 .focus = try Focus.create(allocator, .container), 2019 .grid = null, 2020 .text_boxes = .empty, 2021 .rects = .empty, 2022 .arena = std.heap.ArenaAllocator.init(allocator), 2023 }; 2024 } 2025 2026 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 2027 self.focus.destroy(allocator); 2028 if (self.grid) |*grid| { 2029 grid.deinit(); 2030 self.grid = null; 2031 } 2032 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2033 self.text_boxes.deinit(allocator); 2034 self.rects.deinit(allocator); 2035 self.arena.deinit(); 2036 } 2037 2038 // both the item text and the link are copied into the arena, so the caller's 2039 // slices needn't outlive this call. 2040 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 2041 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2042 self.text_boxes.clearAndFree(allocator); 2043 2044 _ = self.arena.reset(.retain_capacity); 2045 const aa = self.arena.allocator(); 2046 2047 self.focus.clear(); 2048 self.focus.child_id = null; 2049 2050 for (items) |item| { 2051 const line = try aa.dupe(u8, item.text); 2052 2053 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2054 errdefer text_box.deinit(allocator); 2055 text_box.getFocus().focusable = true; 2056 2057 if (item.link.len > 0) { 2058 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 2059 } 2060 2061 try self.text_boxes.append(allocator, text_box); 2062 } 2063 2064 if (self.text_boxes.items.len > 0) { 2065 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 2066 } 2067 } 2068 2069 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2070 self.clearGrid(); 2071 self.focus.clear(); 2072 2073 if (self.text_boxes.items.len == 0) return; 2074 const max_width = constraint.max_size.width orelse 80; 2075 if (max_width == 0) return; 2076 2077 // lay the items out left to right, wrapping to a new row when the next 2078 // one wouldn't fit. 2079 self.rects.clearRetainingCapacity(); 2080 var x: usize = 0; 2081 var y: usize = 0; 2082 var row_height: usize = 0; 2083 var total_width: usize = 0; 2084 for (self.text_boxes.items) |*tb| { 2085 try tb.build(allocator, .{ 2086 .min_size = .{ .width = null, .height = null }, 2087 .max_size = .{ .width = max_width, .height = null }, 2088 }, root_focus); 2089 const tb_grid_maybe = tb.getGrid(); 2090 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 2091 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 2092 if (x > 0 and x + w > max_width) { 2093 x = 0; 2094 y += row_height; 2095 row_height = 0; 2096 } 2097 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 2098 if (h > row_height) row_height = h; 2099 x += w; 2100 if (x > total_width) total_width = x; 2101 } 2102 2103 const total_height = y + row_height; 2104 if (total_width == 0 or total_height == 0) return; 2105 2106 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 2107 errdefer grid.deinit(); 2108 2109 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 2110 const tb_grid = tb.getGrid() orelse continue; 2111 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2112 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2113 } 2114 2115 self.grid = grid; 2116 } 2117 2118 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2119 _ = self; 2120 _ = allocator; 2121 _ = key; 2122 _ = root_focus; 2123 } 2124 2125 pub fn clearGrid(self: *TagFlow) void { 2126 if (self.grid) |*grid| { 2127 grid.deinit(); 2128 self.grid = null; 2129 } 2130 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2131 } 2132 2133 pub fn getGrid(self: TagFlow) ?Grid { 2134 return self.grid; 2135 } 2136 2137 pub fn getFocus(self: *TagFlow) *Focus { 2138 return self.focus; 2139 } 2140 2141 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2142 for (self.text_boxes.items, 0..) |tb, i| { 2143 if (tb.box.focus.id == focus_id) return i; 2144 } 2145 return null; 2146 } 2147 2148 // the first item of the row below (when `downward`) or above, or null at 2149 // the flow's edge. items are laid out in order, so y is nondecreasing. 2150 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2151 const rects = self.rects.items; 2152 if (index >= rects.len) return null; 2153 const cur_y = rects[index].y; 2154 if (downward) { 2155 for (rects[index + 1 ..], index + 1..) |r, i| { 2156 if (r.y > cur_y) return i; 2157 } 2158 return null; 2159 } 2160 // walk the earlier rows; the last one visited is the row above. 2161 var prev_first: ?usize = null; 2162 var i: usize = 0; 2163 while (i < rects.len and rects[i].y < cur_y) { 2164 prev_first = i; 2165 const y = rects[i].y; 2166 while (i < rects.len and rects[i].y == y) i += 1; 2167 } 2168 return prev_first; 2169 } 2170 }; 2171 2172 // an invisible widget that fills the horizontal space granted by its parent. 2173 // used inside Box(horiz) with a min_size so the box reserves space for the 2174 // children that follow, pushing them to the right. 2175 pub const Spacer = struct { 2176 focus: *Focus, 2177 grid: ?Grid, 2178 2179 pub fn init(allocator: std.mem.Allocator) !Spacer { 2180 return .{ 2181 .focus = try Focus.create(allocator, .container), 2182 .grid = null, 2183 }; 2184 } 2185 2186 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2187 self.focus.destroy(allocator); 2188 if (self.grid) |*grid| { 2189 grid.deinit(); 2190 self.grid = null; 2191 } 2192 } 2193 2194 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2195 _ = root_focus; 2196 self.clearGrid(); 2197 const width = constraint.max_size.width orelse return; 2198 if (width == 0) return; 2199 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2200 } 2201 2202 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2203 _ = self; 2204 _ = allocator; 2205 _ = key; 2206 _ = root_focus; 2207 } 2208 2209 pub fn clearGrid(self: *Spacer) void { 2210 if (self.grid) |*grid| { 2211 grid.deinit(); 2212 self.grid = null; 2213 } 2214 } 2215 2216 pub fn getGrid(self: Spacer) ?Grid { 2217 return self.grid; 2218 } 2219 2220 pub fn getFocus(self: *Spacer) *Focus { 2221 return self.focus; 2222 } 2223 }; 2224 2225 // a one-row, non-focusable status bar showing the current page's url 2226 pub const Footer = struct { 2227 focus: *Focus, 2228 grid: ?Grid, 2229 session: *Session, 2230 arena: std.heap.ArenaAllocator, 2231 url: []const u8 = "", 2232 url_width: usize = 0, 2233 2234 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2235 return .{ 2236 .focus = try Focus.create(allocator, .container), 2237 .grid = null, 2238 .session = session, 2239 .arena = std.heap.ArenaAllocator.init(allocator), 2240 }; 2241 } 2242 2243 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2244 self.focus.destroy(allocator); 2245 if (self.grid) |*grid| grid.deinit(); 2246 self.arena.deinit(); 2247 } 2248 2249 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2250 _ = root_focus; 2251 self.clearGrid(); 2252 const width = constraint.max_size.width orelse return; 2253 if (width == 0) return; 2254 2255 _ = self.arena.reset(.retain_capacity); 2256 self.url = ""; 2257 self.url_width = 0; 2258 const aa = self.arena.allocator(); 2259 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2260 const text = if (self.session.web_port) |port| 2261 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2262 else 2263 path; 2264 self.url = text; 2265 2266 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2267 errdefer grid.deinit(); 2268 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2269 var i: usize = 0; 2270 while (utf8.nextCodepointSlice()) |ch| { 2271 if (i == width) break; 2272 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2273 i += 1; 2274 } 2275 self.url_width = i; 2276 self.grid = grid; 2277 } 2278 2279 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2280 _ = allocator; 2281 switch (key) { 2282 .mouse => |mouse| { 2283 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2284 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2285 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2286 }, 2287 else => {}, 2288 } 2289 } 2290 2291 pub fn clearGrid(self: *Footer) void { 2292 if (self.grid) |*grid| { 2293 grid.deinit(); 2294 self.grid = null; 2295 } 2296 } 2297 2298 pub fn getGrid(self: Footer) ?Grid { 2299 return self.grid; 2300 } 2301 2302 pub fn getFocus(self: *Footer) *Focus { 2303 return self.focus; 2304 } 2305 }; 2306 2307 // a single-child wrapper that builds the child at its natural size and 2308 // positions its grid in the middle of the area granted by the parent 2309 // a bordered label, indented past the content it precedes 2310 pub const SectionLabel = struct { 2311 box: wgt.Box(Widget), 2312 2313 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2314 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2315 errdefer box.deinit(allocator); 2316 2317 { 2318 var gap = try wgt.Text(Widget).init(allocator, " "); 2319 errdefer gap.deinit(allocator); 2320 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2321 } 2322 2323 { 2324 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2325 errdefer tb.deinit(allocator); 2326 tb.getFocus().focusable = true; 2327 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2328 } 2329 2330 box.getFocus().child_id = box.children.keys()[1]; 2331 return .{ .box = box }; 2332 } 2333 2334 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2335 self.box.deinit(allocator); 2336 } 2337 2338 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2339 try self.box.build(allocator, constraint, root_focus); 2340 } 2341 2342 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2343 _ = self; 2344 _ = allocator; 2345 _ = key; 2346 _ = root_focus; 2347 } 2348 2349 pub fn clearGrid(self: *SectionLabel) void { 2350 self.box.clearGrid(); 2351 } 2352 2353 pub fn getGrid(self: SectionLabel) ?Grid { 2354 return self.box.getGrid(); 2355 } 2356 2357 pub fn getFocus(self: *SectionLabel) *Focus { 2358 return self.box.getFocus(); 2359 } 2360 }; 2361 2362 // a submit button, indented past its form's content. the web overlay POSTs 2363 // the button's form; terminal hosts detect presses through `buttonId`. 2364 pub const SubmitButton = struct { 2365 box: wgt.Box(Widget), 2366 2367 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2368 return initLabeled(allocator, "submit"); 2369 } 2370 2371 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2372 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2373 errdefer box.deinit(allocator); 2374 2375 { 2376 var gap = try wgt.Text(Widget).init(allocator, " "); 2377 errdefer gap.deinit(allocator); 2378 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2379 } 2380 2381 { 2382 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2383 errdefer button.deinit(allocator); 2384 button.getFocus().focusable = true; 2385 // the renderer distinguishes plain clickables from buttons that 2386 // should POST to a server route by this kind. 2387 button.getFocus().kind = .{ .custom = "submit" }; 2388 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2389 } 2390 2391 box.getFocus().child_id = box.children.keys()[1]; 2392 return .{ .box = box }; 2393 } 2394 2395 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2396 self.box.deinit(allocator); 2397 } 2398 2399 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2400 try self.box.build(allocator, constraint, root_focus); 2401 } 2402 2403 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2404 _ = self; 2405 _ = allocator; 2406 _ = key; 2407 _ = root_focus; 2408 } 2409 2410 pub fn clearGrid(self: *SubmitButton) void { 2411 self.box.clearGrid(); 2412 } 2413 2414 pub fn getGrid(self: SubmitButton) ?Grid { 2415 return self.box.getGrid(); 2416 } 2417 2418 pub fn getFocus(self: *SubmitButton) *Focus { 2419 return self.box.getFocus(); 2420 } 2421 2422 // the button's focus id, for click hit-testing 2423 pub fn buttonId(self: *const SubmitButton) usize { 2424 return self.box.children.keys()[1]; 2425 } 2426 }; 2427 2428 pub const Center = struct { 2429 focus: *Focus, 2430 grid: ?Grid, 2431 child: *Widget, 2432 2433 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2434 const child = try allocator.create(Widget); 2435 errdefer allocator.destroy(child); 2436 child.* = child_widget; 2437 return .{ 2438 .focus = try Focus.create(allocator, .container), 2439 .grid = null, 2440 .child = child, 2441 }; 2442 } 2443 2444 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2445 self.focus.destroy(allocator); 2446 if (self.grid) |*grid| { 2447 grid.deinit(); 2448 self.grid = null; 2449 } 2450 self.child.deinit(allocator); 2451 allocator.destroy(self.child); 2452 } 2453 2454 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2455 self.clearGrid(); 2456 self.getFocus().clear(); 2457 2458 // build the child without forcing it to fill min_size; it sizes 2459 // itself to its content within the available max. 2460 try self.child.build(allocator, .{ 2461 .min_size = .{ .width = null, .height = null }, 2462 .max_size = constraint.max_size, 2463 }, root_focus); 2464 2465 const child_grid = self.child.getGrid() orelse return; 2466 2467 // prefer max when given; otherwise grow to max(min, child) so that 2468 // when only a min is set (e.g. the wasm path passes viewport rows 2469 // as min_height with no max), we can still vertically center while 2470 // letting taller content extend past the min. 2471 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; 2472 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; 2473 if (width == 0 or height == 0) return; 2474 2475 const offset_x: usize = (width -| child_grid.size.width) / 2; 2476 const offset_y: usize = (height -| child_grid.size.height) / 2; 2477 2478 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2479 errdefer grid.deinit(); 2480 try grid.drawGrid(child_grid, offset_x, offset_y); 2481 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2482 self.getFocus().child_id = self.child.getFocus().id; 2483 2484 self.grid = grid; 2485 } 2486 2487 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2488 try self.child.input(allocator, key, root_focus); 2489 } 2490 2491 pub fn clearGrid(self: *Center) void { 2492 if (self.grid) |*grid| { 2493 grid.deinit(); 2494 self.grid = null; 2495 } 2496 self.child.clearGrid(); 2497 } 2498 2499 pub fn getGrid(self: Center) ?Grid { 2500 return self.grid; 2501 } 2502 2503 pub fn getFocus(self: *Center) *Focus { 2504 return self.focus; 2505 } 2506 }; 2507 2508 // renders truecolor ANSI art 2509 pub const AnsiArt = struct { 2510 focus: *Focus, 2511 grid: ?Grid, 2512 content: []const u8, 2513 2514 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2515 return .{ 2516 .focus = try Focus.create(allocator, .container), 2517 .grid = null, 2518 .content = content, 2519 }; 2520 } 2521 2522 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2523 self.focus.destroy(allocator); 2524 if (self.grid) |*grid| { 2525 grid.deinit(); 2526 self.grid = null; 2527 } 2528 } 2529 2530 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2531 _ = root_focus; 2532 self.clearGrid(); 2533 2534 // parse into rows of cells, then copy into a rectangular grid 2535 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2536 defer { 2537 for (rows.items) |*row| row.deinit(allocator); 2538 rows.deinit(allocator); 2539 } 2540 var row: std.ArrayList(Grid.Cell) = .empty; 2541 errdefer row.deinit(allocator); 2542 2543 var style: Grid.Style = .{}; 2544 var width: usize = 0; 2545 const content = self.content; 2546 var i: usize = 0; 2547 while (i < content.len) { 2548 const byte = content[i]; 2549 if (byte == '\n') { 2550 width = @max(width, row.items.len); 2551 try rows.append(allocator, row); 2552 row = .empty; 2553 i += 1; 2554 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2555 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2556 var j = i + 2; 2557 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2558 if (j >= content.len) { 2559 i = content.len; // malformed trailing escape; stop 2560 } else { 2561 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2562 i = j + 1; 2563 } 2564 } else { 2565 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2566 const end = @min(content.len, i + len); 2567 const rune = content[i..end]; 2568 const transparent = end - i == 1 and rune[0] == ' ' and 2569 style.fg == null and style.bg == null and !style.inverted; 2570 try row.append(allocator, .{ 2571 .rune = if (transparent) null else rune, 2572 .style = style, 2573 }); 2574 i = end; 2575 } 2576 } 2577 // a trailing row with content but no closing newline 2578 if (row.items.len > 0) { 2579 width = @max(width, row.items.len); 2580 try rows.append(allocator, row); 2581 } else { 2582 row.deinit(allocator); 2583 } 2584 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2585 2586 const height = rows.items.len; 2587 if (width == 0 or height == 0) return; 2588 2589 const clamped_w = @min(width, constraint.max_size.width orelse width); 2590 const clamped_h = @min(height, constraint.max_size.height orelse height); 2591 2592 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2593 errdefer grid.deinit(); 2594 for (rows.items[0..clamped_h], 0..) |r, y| { 2595 const n = @min(r.items.len, clamped_w); 2596 for (r.items[0..n], 0..) |cell, x| { 2597 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2598 } 2599 } 2600 self.grid = grid; 2601 } 2602 2603 pub fn clearGrid(self: *AnsiArt) void { 2604 if (self.grid) |*grid| { 2605 grid.deinit(); 2606 self.grid = null; 2607 } 2608 } 2609 2610 pub fn getGrid(self: AnsiArt) ?Grid { 2611 return self.grid; 2612 } 2613 2614 pub fn getFocus(self: *AnsiArt) *Focus { 2615 return self.focus; 2616 } 2617 2618 fn applySgr(style: *Grid.Style, params: []const u8) void { 2619 var nums: [16]u32 = undefined; 2620 var n: usize = 0; 2621 var it = std.mem.splitScalar(u8, params, ';'); 2622 while (it.next()) |tok| { 2623 if (n >= nums.len) break; 2624 // an empty parameter (e.g. bare "\x1b[m") means 0 2625 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2626 n += 1; 2627 } 2628 if (n == 0) { 2629 style.* = .{}; 2630 return; 2631 } 2632 var i: usize = 0; 2633 while (i < n) : (i += 1) { 2634 switch (nums[i]) { 2635 0 => style.* = .{}, 2636 7 => style.inverted = true, 2637 27 => style.inverted = false, 2638 39 => style.fg = null, 2639 49 => style.bg = null, 2640 38, 48 => { 2641 // truecolor form: 38;2;r;g;b — anything else is ignored 2642 if (i + 4 < n and nums[i + 1] == 2) { 2643 const c = Grid.Color{ 2644 .r = @truncate(nums[i + 2]), 2645 .g = @truncate(nums[i + 3]), 2646 .b = @truncate(nums[i + 4]), 2647 }; 2648 if (nums[i] == 38) style.fg = c else style.bg = c; 2649 i += 4; 2650 } 2651 }, 2652 else => {}, 2653 } 2654 } 2655 } 2656 }; 2657 2658 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2659 pub const AnsiBackground = struct { 2660 grid: ?Grid, 2661 child: *Widget, 2662 art: AnsiArt, 2663 session: *Session, 2664 2665 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2666 var cw = child_widget; 2667 const child = allocator.create(Widget) catch |e| { 2668 cw.deinit(allocator); 2669 return e; 2670 }; 2671 child.* = cw; 2672 errdefer { 2673 child.deinit(allocator); 2674 allocator.destroy(child); 2675 } 2676 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2677 } 2678 2679 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2680 if (self.grid) |*grid| grid.deinit(); 2681 self.art.deinit(allocator); 2682 self.child.deinit(allocator); 2683 allocator.destroy(self.child); 2684 } 2685 2686 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2687 self.clearGrid(); 2688 2689 // make the wrapped page fill the whole available area 2690 try self.child.build(allocator, .{ 2691 .min_size = .{ 2692 .width = constraint.max_size.width orelse constraint.min_size.width, 2693 .height = constraint.max_size.height orelse constraint.min_size.height, 2694 }, 2695 .max_size = constraint.max_size, 2696 }, root_focus); 2697 2698 if (self.session.data.enable_ansi) { 2699 if (self.child.getGrid()) |fg| { 2700 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2701 } 2702 } 2703 } 2704 2705 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2706 try self.child.input(allocator, key, root_focus); 2707 } 2708 2709 pub fn clearGrid(self: *AnsiBackground) void { 2710 if (self.grid) |*grid| { 2711 grid.deinit(); 2712 self.grid = null; 2713 } 2714 self.child.clearGrid(); 2715 self.art.clearGrid(); 2716 } 2717 2718 pub fn getGrid(self: AnsiBackground) ?Grid { 2719 return self.grid orelse self.child.getGrid(); 2720 } 2721 2722 pub fn getFocus(self: *AnsiBackground) *Focus { 2723 return self.child.getFocus(); 2724 } 2725 2726 // which top corner the art hugs. 2727 const ArtAnchor = enum { top_left, top_right }; 2728 2729 // a foreground cell counts as blank if it's empty or a bare space without styling 2730 fn cellIsBlank(cell: Grid.Cell) bool { 2731 if (cell.rune) |rune| { 2732 return std.mem.eql(u8, rune, " ") and 2733 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2734 } 2735 return true; 2736 } 2737 2738 // the art is dimmed to this fraction of its brightness so foreground text 2739 // stays legible over it 2740 const art_brightness = 45; // percent 2741 2742 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2743 const c = color orelse return null; 2744 return .{ 2745 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2746 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2747 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2748 }; 2749 } 2750 2751 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2752 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2753 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2754 // snap such channels clear of that range so the backdrop doesn't blink or 2755 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2756 // actually render truecolor. 2757 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2758 const c = color orelse return null; 2759 const snap = struct { 2760 fn f(v: u8) u8 { 2761 return if (v >= 1 and v <= 9) 10 else v; 2762 } 2763 }.f; 2764 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2765 } 2766 2767 // composites ANSI art behind a foreground grid 2768 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2769 art.clearGrid(); 2770 try art.build(allocator, .{ 2771 .min_size = .{ .width = null, .height = null }, 2772 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2773 }, root_focus); 2774 2775 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2776 errdefer out.deinit(); 2777 2778 if (art.getGrid()) |art_grid| { 2779 const anchor_x = switch (anchor) { 2780 .top_left => 0, 2781 .top_right => foreground.size.width -| art_grid.size.width, 2782 }; 2783 for (0..art_grid.size.height) |y| { 2784 for (0..art_grid.size.width) |x| { 2785 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2786 if (src.rune == null) continue; 2787 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2788 const dst = &out.cells.items[idx]; 2789 // composite the dimmed art behind every cell so it shows 2790 // through the whole UI: a blank cell becomes the art, while a 2791 // cell with a glyph keeps the glyph but takes the dimmed art as 2792 // its background (replacing whatever background it had, which 2793 // would otherwise obscure the art). 2794 if (cellIsBlank(dst.*)) { 2795 dst.* = src; 2796 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2797 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2798 } else { 2799 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2800 dst.style.bg = background_maybe; 2801 // change the color of text overlaying ANSI art to ensure 2802 // it is readable. don't do this in the web UI, since we 2803 // have direct control of the theme there (also, inner 2804 // scroll panes allow text to move without a re-render 2805 // by the TUI, which would break this). also don't do this 2806 // if the text is explicitly styled. 2807 if (is_terminal and dst.style.fg == null) { 2808 if (background_maybe) |background| { 2809 // use near-black and near-white, which is easier on the eyes 2810 // while retaining high contrast 2811 const luminance = (@as(u32, background.r) * 299 + 2812 @as(u32, background.g) * 587 + 2813 @as(u32, background.b) * 114) / 1000; 2814 dst.style.fg = if (luminance >= 128) 2815 .{ .r = 16, .g = 16, .b = 16 } 2816 else 2817 .{ .r = 240, .g = 240, .b = 240 }; 2818 } 2819 } 2820 } 2821 } 2822 } 2823 } 2824 return out; 2825 } 2826 }; 2827