╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1080 const page = try arena.allocator().create(Page); 1081 page.* = try Page.init(arena, session, route); 1082 return .{ 1083 .root = try initRoot(allocator, page, session), 1084 .route = route, 1085 .arena = arena, 1086 .history = .empty, 1087 }; 1088 } 1089 1090 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1091 self.root.deinit(allocator); 1092 freeArena(allocator, self.arena); 1093 for (self.history.items) |*entry| { 1094 entry.root.deinit(allocator); 1095 freeArena(allocator, entry.arena); 1096 } 1097 self.history.deinit(allocator); 1098 } 1099 1100 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1101 arena.deinit(); 1102 allocator.destroy(arena); 1103 } 1104 1105 // reconcile the displayed root with the session's navigation state. a 1106 // forward nav (current_page moved to a different parent page) pushes the 1107 // current root and builds the new page in a fresh arena; a back request 1108 // frees the current page and restores the previous one. when escape is 1109 // pressed with no history left we switch to the quit tab instead of quitting. 1110 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1111 // refresh: rebuild the current page in place from a current moment 1112 if (session.refresh_requested) { 1113 session.refresh_requested = false; 1114 if (session.haxy_moment != null or session.local != null) { 1115 const arena = try allocator.create(std.heap.ArenaAllocator); 1116 arena.* = std.heap.ArenaAllocator.init(allocator); 1117 errdefer freeArena(allocator, arena); 1118 1119 session.page_arena = arena; 1120 1121 const page = try arena.allocator().create(Page); 1122 page.* = try Page.init(arena, session, self.route); 1123 const new_root = try initRoot(allocator, page, session); 1124 1125 self.root.deinit(allocator); 1126 freeArena(allocator, self.arena); 1127 self.root = new_root; 1128 self.arena = arena; 1129 } 1130 return; 1131 } 1132 1133 if (session.nav_back) { 1134 session.nav_back = false; 1135 if (self.history.pop()) |entry| { 1136 self.root.deinit(allocator); 1137 freeArena(allocator, self.arena); 1138 self.root = entry.root; 1139 self.route = entry.route; 1140 self.arena = entry.arena; 1141 session.page_arena = entry.arena; 1142 session.data.current_page = entry.route; 1143 return; 1144 } 1145 // nothing to go back to; switch to the quit confirmation 1146 const root_focus = self.root.getFocus(); 1147 var iter = root_focus.children.iterator(); 1148 while (iter.next()) |entry| { 1149 switch (entry.value_ptr.focus.kind) { 1150 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1151 root_focus.setFocus(entry.key_ptr.*); 1152 // a Stack only builds its selected child, so the quit 1153 // button isn't in the focus tree until a build runs with 1154 // the quit tab selected. build once, then send a 1155 // synthetic arrow_down to drop focus from the tab onto 1156 // the button so it's ready to confirm. 1157 try self.root.build(allocator, .{ 1158 .min_size = .{ .width = null, .height = 40 }, 1159 .max_size = .{ .width = 80, .height = null }, 1160 }, root_focus); 1161 try self.root.input(allocator, .arrow_down, root_focus); 1162 break; 1163 }, 1164 else => {}, 1165 } 1166 } 1167 return; 1168 } 1169 1170 // forward navigation: navigate() set next_page to the page to move to (a 1171 // cross-page link or tab change crossing pages) 1172 if (session.next_page) |route| { 1173 session.next_page = null; 1174 if (session.haxy_moment == null and session.local == null) return; 1175 // the page we navigated to becomes the current page 1176 session.data.current_page = route; 1177 1178 const arena = try allocator.create(std.heap.ArenaAllocator); 1179 arena.* = std.heap.ArenaAllocator.init(allocator); 1180 errdefer freeArena(allocator, arena); 1181 1182 session.page_arena = arena; 1183 1184 const page = try arena.allocator().create(Page); 1185 page.* = try Page.init(arena, session, route); 1186 const new_root = try initRoot(allocator, page, session); 1187 1188 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1189 // drop the oldest entry (freeing its widget tree and arena) once over cap 1190 if (self.history.items.len > max_history) { 1191 var oldest = self.history.orderedRemove(0); 1192 oldest.root.deinit(allocator); 1193 freeArena(allocator, oldest.arena); 1194 } 1195 self.root = new_root; 1196 self.route = route; 1197 self.arena = arena; 1198 } 1199 } 1200 }; 1201 1202 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1203 const page_widget: Widget = switch (page.*) { 1204 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1205 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1206 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1207 }; 1208 1209 const demon_art = @embedFile("embed/demon.ans"); 1210 1211 // on the TUI/SSH, the page sits above a one-row footer showing the url 1212 var root = if (session.is_terminal) blk: { 1213 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1214 errdefer box.deinit(allocator); 1215 const bg_id = bg_blk: { 1216 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 1217 errdefer background.deinit(allocator); 1218 const id = background.getFocus().id; 1219 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 1220 break :bg_blk id; 1221 }; 1222 { 1223 var footer = try Footer.init(allocator, session); 1224 errdefer footer.deinit(allocator); 1225 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 1226 } 1227 box.getFocus().child_id = bg_id; 1228 break :blk Widget{ .box = box }; 1229 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 1230 errdefer root.deinit(allocator); 1231 1232 // input-owning views build their TextInputs in init — so reset the 1233 // focus-id -> *TextInput map here 1234 session.text_inputs.clearRetainingCapacity(); 1235 session.input_values.clearRetainingCapacity(); 1236 1237 try root.build(allocator, .{ 1238 .min_size = .{ .width = null, .height = 40 }, 1239 .max_size = .{ .width = 100, .height = null }, 1240 }, root.getFocus()); 1241 1242 return root; 1243 } 1244 1245 pub const Widget = union(enum) { 1246 text: wgt.Text(Widget), 1247 box: wgt.Box(Widget), 1248 text_box: wgt.TextBox(Widget), 1249 text_input: wgt.TextInput(Widget), 1250 scroll: wgt.Scroll(Widget), 1251 stack: wgt.Stack(Widget), 1252 flow_box: FlowBox, 1253 flow_box_scroll: FlowBox.Scroll, 1254 tag_flow: TagFlow, 1255 spacer: Spacer, 1256 center: Center, 1257 background: AnsiBackground, 1258 home: Home.View, 1259 user: User.View, 1260 repo: Repo.View, 1261 quit: Quit.View, 1262 title: Title.View, 1263 sub_title: SubTitle.View, 1264 home_header: Home.Header.View, 1265 user_header: User.Header.View, 1266 repo_header: Repo.Header.View, 1267 repo_files_header: Repo.Files.Header.View, 1268 repo_commits_header: Repo.Commits.Header.View, 1269 repo_issues_header: Repo.Issues.Header, 1270 repo_files: Repo.Files.View, 1271 repo_commits: Repo.Commits.View, 1272 repo_refs: Repo.Refs.View, 1273 repo_issues: Repo.Issues.View, 1274 home_users: Home.Users.View, 1275 home_repos: Home.Repos.View, 1276 auth_tab: Home.Header.AuthTab.View, 1277 home_settings: Home.Settings.View, 1278 home_auth: Home.Auth.View, 1279 footer: Footer, 1280 1281 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 1282 switch (self.*) { 1283 inline else => |*case| case.deinit(allocator), 1284 } 1285 } 1286 1287 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 1288 switch (self.*) { 1289 inline else => |*case| try case.build(allocator, constraint, root_focus), 1290 } 1291 } 1292 1293 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 1294 switch (self.*) { 1295 inline else => |*case| try case.input(allocator, key, root_focus), 1296 } 1297 } 1298 1299 pub fn clearGrid(self: *Widget) void { 1300 switch (self.*) { 1301 inline else => |*case| case.clearGrid(), 1302 } 1303 } 1304 1305 pub fn getGrid(self: Widget) ?Grid { 1306 switch (self) { 1307 inline else => |*case| return case.getGrid(), 1308 } 1309 } 1310 1311 pub fn getFocus(self: *Widget) *Focus { 1312 switch (self.*) { 1313 inline else => |*case| return case.getFocus(), 1314 } 1315 } 1316 }; 1317 1318 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 1319 // `scroll` to keep it visible. 1320 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 1321 const keys = box.children.keys(); 1322 if (keys.len == 0) return; 1323 const cur_id = box.getFocus().child_id orelse return; 1324 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 1325 const last: isize = @intCast(keys.len - 1); 1326 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 1327 if (next == @as(usize, @intCast(cur))) return; 1328 root_focus.setFocus(keys[next]); 1329 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 1330 } 1331 1332 pub const FlowBox = struct { 1333 focus: *Focus, 1334 grid: ?Grid, 1335 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1336 // backs the per-item strings the text boxes borrow: each box's `content` 1337 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1338 // wholesale on each setItems so there's no per-string ownership to track. 1339 arena: std.heap.ArenaAllocator, 1340 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1341 // up/down can step by a row's worth of items. 1342 last_cols: usize, 1343 options: Options, 1344 1345 const border_rows: usize = 2; 1346 1347 pub const Options = struct { 1348 cell_width: usize = 40, 1349 cell_height: usize = 3, 1350 }; 1351 1352 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1353 return .{ 1354 .focus = try Focus.create(allocator, .container), 1355 .grid = null, 1356 .text_boxes = .empty, 1357 .arena = std.heap.ArenaAllocator.init(allocator), 1358 .last_cols = 1, 1359 .options = options, 1360 }; 1361 } 1362 1363 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1364 self.focus.destroy(allocator); 1365 if (self.grid) |*grid| { 1366 grid.deinit(); 1367 self.grid = null; 1368 } 1369 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1370 self.text_boxes.deinit(allocator); 1371 self.arena.deinit(); 1372 } 1373 1374 // one flow item: its display text plus an optional link. a non-empty `link` 1375 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1376 // the web renderer turns into an anchor. 1377 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1378 1379 // both the item text and the link are copied into the arena, so the caller's 1380 // slices needn't outlive this call. 1381 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1382 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1383 self.text_boxes.clearAndFree(allocator); 1384 1385 // the old text boxes (and their borrowed content/links) are gone now, so 1386 // the arena that backed those strings can be reclaimed. 1387 _ = self.arena.reset(.retain_capacity); 1388 const aa = self.arena.allocator(); 1389 1390 self.focus.clear(); 1391 self.focus.child_id = null; 1392 1393 for (items) |item| { 1394 const line = try aa.dupe(u8, item.text); 1395 1396 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1397 errdefer text_box.deinit(allocator); 1398 text_box.getFocus().focusable = true; 1399 1400 if (item.link.len > 0) { 1401 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1402 } 1403 1404 try self.text_boxes.append(allocator, text_box); 1405 } 1406 1407 if (self.text_boxes.items.len > 0) { 1408 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1409 } 1410 } 1411 1412 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1413 self.clearGrid(); 1414 self.focus.clear(); 1415 1416 const cell_width = self.options.cell_width; 1417 const max_width = constraint.max_size.width orelse cell_width; 1418 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1419 self.last_cols = cols; 1420 1421 const count = self.text_boxes.items.len; 1422 if (count == 0) return; 1423 const slot_height = self.options.cell_height + border_rows; 1424 if (slot_height == 0) return; 1425 1426 // build at the slot size so every text box fits a single grid cell 1427 for (self.text_boxes.items) |*tb| { 1428 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1429 try tb.build(allocator, .{ 1430 .min_size = .{ .width = cell_width, .height = null }, 1431 .max_size = .{ .width = cell_width, .height = slot_height }, 1432 }, root_focus); 1433 } 1434 1435 const rows = (count + cols - 1) / cols; 1436 const content_height = rows * slot_height; 1437 const total_width = cols * cell_width; 1438 if (total_width == 0 or content_height == 0) return; 1439 1440 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1441 errdefer grid.deinit(); 1442 1443 for (self.text_boxes.items, 0..) |*tb, i| { 1444 const tb_grid = tb.getGrid() orelse continue; 1445 const col = i % cols; 1446 const row = i / cols; 1447 const cell_x = col * cell_width; 1448 const cell_y = row * slot_height; 1449 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1450 try grid.drawGrid(tb_grid, cell_x, cell_y); 1451 } 1452 1453 self.grid = grid; 1454 } 1455 1456 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1457 _ = self; 1458 _ = allocator; 1459 _ = key; 1460 _ = root_focus; 1461 } 1462 1463 pub fn clearGrid(self: *FlowBox) void { 1464 if (self.grid) |*grid| { 1465 grid.deinit(); 1466 self.grid = null; 1467 } 1468 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1469 } 1470 1471 pub fn getGrid(self: FlowBox) ?Grid { 1472 return self.grid; 1473 } 1474 1475 pub fn getFocus(self: *FlowBox) *Focus { 1476 return self.focus; 1477 } 1478 1479 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1480 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1481 const slot_height = self.options.cell_height + border_rows; 1482 const col = index % self.last_cols; 1483 const row = index / self.last_cols; 1484 return .{ 1485 .x = @intCast(col * self.options.cell_width), 1486 .y = @intCast(row * slot_height), 1487 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1488 }; 1489 } 1490 1491 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1492 for (self.text_boxes.items, 0..) |tb, i| { 1493 if (tb.box.focus.id == focus_id) return i; 1494 } 1495 return null; 1496 } 1497 1498 pub const Scroll = struct { 1499 scroll: wgt.Scroll(Widget), 1500 1501 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1502 var layout_inner = try FlowBox.init(allocator, options); 1503 errdefer layout_inner.deinit(allocator); 1504 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1505 errdefer scroll.deinit(allocator); 1506 return .{ .scroll = scroll }; 1507 } 1508 1509 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1510 self.scroll.deinit(allocator); 1511 } 1512 1513 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1514 self.scroll.x = 0; 1515 self.scroll.y = 0; 1516 try self.inner().setItems(allocator, items); 1517 } 1518 1519 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1520 try self.scroll.build(allocator, constraint, root_focus); 1521 } 1522 1523 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1524 _ = allocator; 1525 const in = self.inner(); 1526 const child_id = in.focus.child_id orelse return; 1527 const current_index = in.indexOfFocusId(child_id) orelse return; 1528 const count = in.text_boxes.items.len; 1529 if (count == 0) return; 1530 const cols = in.last_cols; 1531 const slot_height = in.options.cell_height + FlowBox.border_rows; 1532 1533 var index = current_index; 1534 switch (key) { 1535 .arrow_up => index -|= cols, 1536 .arrow_down => if (index + cols < count) { 1537 index += cols; 1538 }, 1539 .arrow_left => index -|= 1, 1540 .arrow_right => if (index + 1 < count) { 1541 index += 1; 1542 }, 1543 .home => index = 0, 1544 .end => index = count - 1, 1545 .page_up => { 1546 if (self.scroll.grid) |grid| if (slot_height > 0) { 1547 const rows_per_page = grid.size.height / slot_height; 1548 index -|= rows_per_page * cols; 1549 }; 1550 }, 1551 .page_down => { 1552 if (self.scroll.grid) |grid| if (slot_height > 0) { 1553 const rows_per_page = grid.size.height / slot_height; 1554 index = @min(index + rows_per_page * cols, count - 1); 1555 }; 1556 }, 1557 // scroll wheel moves the focused cell by a full row so the 1558 // viewport (via scrollToRect) follows in row-sized steps, 1559 // matching how a scroll wheel feels in a grid view. 1560 .mouse => |mouse| switch (mouse.action) { 1561 .scroll => |dir| switch (dir) { 1562 .up => index -|= cols, 1563 .down => if (index + cols < count) { 1564 index += cols; 1565 }, 1566 }, 1567 else => {}, 1568 }, 1569 else => {}, 1570 } 1571 1572 if (index != current_index) { 1573 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1574 if (in.cellRect(index)) |rect| { 1575 self.scroll.scrollToRect(rect); 1576 } 1577 } 1578 } 1579 1580 pub fn clearGrid(self: *Scroll) void { 1581 self.scroll.clearGrid(); 1582 } 1583 1584 pub fn getGrid(self: Scroll) ?Grid { 1585 return self.scroll.getGrid(); 1586 } 1587 1588 pub fn getFocus(self: *Scroll) *Focus { 1589 return self.scroll.getFocus(); 1590 } 1591 1592 pub fn getSelectedIndex(self: Scroll) ?usize { 1593 const in = self.scroll.child.flow_box; 1594 const child_id = in.focus.child_id orelse return null; 1595 return in.indexOfFocusId(child_id); 1596 } 1597 1598 fn inner(self: *Scroll) *FlowBox { 1599 return &self.scroll.child.flow_box; 1600 } 1601 }; 1602 }; 1603 1604 // a left-to-right flow of variable-width focusable items that wraps at the 1605 // available width, like words in a paragraph. unlike FlowBox there is no cell 1606 // grid: each item is as wide as its text. selection movement is driven by the 1607 // owning view via indexOfFocusId/verticalNeighbor. 1608 pub const TagFlow = struct { 1609 focus: *Focus, 1610 grid: ?Grid, 1611 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1612 // last build's item rects (content space), for vertical navigation and 1613 // scroll-into-view. 1614 rects: std.ArrayList(layout.IRect), 1615 // backs the per-item strings the text boxes borrow. 1616 arena: std.heap.ArenaAllocator, 1617 1618 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1619 1620 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1621 return .{ 1622 .focus = try Focus.create(allocator, .container), 1623 .grid = null, 1624 .text_boxes = .empty, 1625 .rects = .empty, 1626 .arena = std.heap.ArenaAllocator.init(allocator), 1627 }; 1628 } 1629 1630 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1631 self.focus.destroy(allocator); 1632 if (self.grid) |*grid| { 1633 grid.deinit(); 1634 self.grid = null; 1635 } 1636 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1637 self.text_boxes.deinit(allocator); 1638 self.rects.deinit(allocator); 1639 self.arena.deinit(); 1640 } 1641 1642 // both the item text and the link are copied into the arena, so the caller's 1643 // slices needn't outlive this call. 1644 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1645 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1646 self.text_boxes.clearAndFree(allocator); 1647 1648 _ = self.arena.reset(.retain_capacity); 1649 const aa = self.arena.allocator(); 1650 1651 self.focus.clear(); 1652 self.focus.child_id = null; 1653 1654 for (items) |item| { 1655 const line = try aa.dupe(u8, item.text); 1656 1657 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1658 errdefer text_box.deinit(allocator); 1659 text_box.getFocus().focusable = true; 1660 1661 if (item.link.len > 0) { 1662 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1663 } 1664 1665 try self.text_boxes.append(allocator, text_box); 1666 } 1667 1668 if (self.text_boxes.items.len > 0) { 1669 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1670 } 1671 } 1672 1673 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1674 self.clearGrid(); 1675 self.focus.clear(); 1676 1677 if (self.text_boxes.items.len == 0) return; 1678 const max_width = constraint.max_size.width orelse 80; 1679 if (max_width == 0) return; 1680 1681 // lay the items out left to right, wrapping to a new row when the next 1682 // one wouldn't fit. 1683 self.rects.clearRetainingCapacity(); 1684 var x: usize = 0; 1685 var y: usize = 0; 1686 var row_height: usize = 0; 1687 var total_width: usize = 0; 1688 for (self.text_boxes.items) |*tb| { 1689 try tb.build(allocator, .{ 1690 .min_size = .{ .width = null, .height = null }, 1691 .max_size = .{ .width = max_width, .height = null }, 1692 }, root_focus); 1693 const tb_grid_maybe = tb.getGrid(); 1694 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1695 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1696 if (x > 0 and x + w > max_width) { 1697 x = 0; 1698 y += row_height; 1699 row_height = 0; 1700 } 1701 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1702 if (h > row_height) row_height = h; 1703 x += w; 1704 if (x > total_width) total_width = x; 1705 } 1706 1707 const total_height = y + row_height; 1708 if (total_width == 0 or total_height == 0) return; 1709 1710 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1711 errdefer grid.deinit(); 1712 1713 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1714 const tb_grid = tb.getGrid() orelse continue; 1715 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1716 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1717 } 1718 1719 self.grid = grid; 1720 } 1721 1722 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1723 _ = self; 1724 _ = allocator; 1725 _ = key; 1726 _ = root_focus; 1727 } 1728 1729 pub fn clearGrid(self: *TagFlow) void { 1730 if (self.grid) |*grid| { 1731 grid.deinit(); 1732 self.grid = null; 1733 } 1734 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1735 } 1736 1737 pub fn getGrid(self: TagFlow) ?Grid { 1738 return self.grid; 1739 } 1740 1741 pub fn getFocus(self: *TagFlow) *Focus { 1742 return self.focus; 1743 } 1744 1745 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1746 for (self.text_boxes.items, 0..) |tb, i| { 1747 if (tb.box.focus.id == focus_id) return i; 1748 } 1749 return null; 1750 } 1751 1752 // the first item of the row below (when `downward`) or above, or null at 1753 // the flow's edge. items are laid out in order, so y is nondecreasing. 1754 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1755 const rects = self.rects.items; 1756 if (index >= rects.len) return null; 1757 const cur_y = rects[index].y; 1758 if (downward) { 1759 for (rects[index + 1 ..], index + 1..) |r, i| { 1760 if (r.y > cur_y) return i; 1761 } 1762 return null; 1763 } 1764 // walk the earlier rows; the last one visited is the row above. 1765 var prev_first: ?usize = null; 1766 var i: usize = 0; 1767 while (i < rects.len and rects[i].y < cur_y) { 1768 prev_first = i; 1769 const y = rects[i].y; 1770 while (i < rects.len and rects[i].y == y) i += 1; 1771 } 1772 return prev_first; 1773 } 1774 }; 1775 1776 // an invisible widget that fills the horizontal space granted by its parent. 1777 // used inside Box(horiz) with a min_size so the box reserves space for the 1778 // children that follow, pushing them to the right. 1779 pub const Spacer = struct { 1780 focus: *Focus, 1781 grid: ?Grid, 1782 1783 pub fn init(allocator: std.mem.Allocator) !Spacer { 1784 return .{ 1785 .focus = try Focus.create(allocator, .container), 1786 .grid = null, 1787 }; 1788 } 1789 1790 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1791 self.focus.destroy(allocator); 1792 if (self.grid) |*grid| { 1793 grid.deinit(); 1794 self.grid = null; 1795 } 1796 } 1797 1798 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1799 _ = root_focus; 1800 self.clearGrid(); 1801 const width = constraint.max_size.width orelse return; 1802 if (width == 0) return; 1803 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1804 } 1805 1806 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1807 _ = self; 1808 _ = allocator; 1809 _ = key; 1810 _ = root_focus; 1811 } 1812 1813 pub fn clearGrid(self: *Spacer) void { 1814 if (self.grid) |*grid| { 1815 grid.deinit(); 1816 self.grid = null; 1817 } 1818 } 1819 1820 pub fn getGrid(self: Spacer) ?Grid { 1821 return self.grid; 1822 } 1823 1824 pub fn getFocus(self: *Spacer) *Focus { 1825 return self.focus; 1826 } 1827 }; 1828 1829 // a one-row, non-focusable status bar showing the current page's url 1830 pub const Footer = struct { 1831 focus: *Focus, 1832 grid: ?Grid, 1833 session: *Session, 1834 arena: std.heap.ArenaAllocator, 1835 1836 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1837 return .{ 1838 .focus = try Focus.create(allocator, .container), 1839 .grid = null, 1840 .session = session, 1841 .arena = std.heap.ArenaAllocator.init(allocator), 1842 }; 1843 } 1844 1845 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1846 self.focus.destroy(allocator); 1847 if (self.grid) |*grid| grid.deinit(); 1848 self.arena.deinit(); 1849 } 1850 1851 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1852 _ = root_focus; 1853 self.clearGrid(); 1854 const width = constraint.max_size.width orelse return; 1855 if (width == 0) return; 1856 1857 _ = self.arena.reset(.retain_capacity); 1858 const aa = self.arena.allocator(); 1859 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1860 const text = if (self.session.web_port) |port| 1861 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1862 else 1863 path; 1864 1865 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1866 errdefer grid.deinit(); 1867 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1868 var i: usize = 0; 1869 while (utf8.nextCodepointSlice()) |ch| { 1870 if (i == width) break; 1871 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1872 i += 1; 1873 } 1874 self.grid = grid; 1875 } 1876 1877 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1878 _ = self; 1879 _ = allocator; 1880 _ = key; 1881 _ = root_focus; 1882 } 1883 1884 pub fn clearGrid(self: *Footer) void { 1885 if (self.grid) |*grid| { 1886 grid.deinit(); 1887 self.grid = null; 1888 } 1889 } 1890 1891 pub fn getGrid(self: Footer) ?Grid { 1892 return self.grid; 1893 } 1894 1895 pub fn getFocus(self: *Footer) *Focus { 1896 return self.focus; 1897 } 1898 }; 1899 1900 // a single-child wrapper that builds the child at its natural size and 1901 // positions its grid in the middle of the area granted by the parent 1902 pub const Center = struct { 1903 focus: *Focus, 1904 grid: ?Grid, 1905 child: *Widget, 1906 1907 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1908 const child = try allocator.create(Widget); 1909 errdefer allocator.destroy(child); 1910 child.* = child_widget; 1911 return .{ 1912 .focus = try Focus.create(allocator, .container), 1913 .grid = null, 1914 .child = child, 1915 }; 1916 } 1917 1918 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1919 self.focus.destroy(allocator); 1920 if (self.grid) |*grid| { 1921 grid.deinit(); 1922 self.grid = null; 1923 } 1924 self.child.deinit(allocator); 1925 allocator.destroy(self.child); 1926 } 1927 1928 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1929 self.clearGrid(); 1930 self.getFocus().clear(); 1931 1932 // build the child without forcing it to fill min_size; it sizes 1933 // itself to its content within the available max. 1934 try self.child.build(allocator, .{ 1935 .min_size = .{ .width = null, .height = null }, 1936 .max_size = constraint.max_size, 1937 }, root_focus); 1938 1939 const child_grid = self.child.getGrid() orelse return; 1940 1941 // prefer max when given; otherwise grow to max(min, child) so that 1942 // when only a min is set (e.g. the wasm path passes viewport rows 1943 // as min_height with no max), we can still vertically center while 1944 // letting taller content extend past the min. 1945 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; 1946 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; 1947 if (width == 0 or height == 0) return; 1948 1949 const offset_x: usize = (width -| child_grid.size.width) / 2; 1950 const offset_y: usize = (height -| child_grid.size.height) / 2; 1951 1952 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1953 errdefer grid.deinit(); 1954 try grid.drawGrid(child_grid, offset_x, offset_y); 1955 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1956 self.getFocus().child_id = self.child.getFocus().id; 1957 1958 self.grid = grid; 1959 } 1960 1961 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1962 try self.child.input(allocator, key, root_focus); 1963 } 1964 1965 pub fn clearGrid(self: *Center) void { 1966 if (self.grid) |*grid| { 1967 grid.deinit(); 1968 self.grid = null; 1969 } 1970 self.child.clearGrid(); 1971 } 1972 1973 pub fn getGrid(self: Center) ?Grid { 1974 return self.grid; 1975 } 1976 1977 pub fn getFocus(self: *Center) *Focus { 1978 return self.focus; 1979 } 1980 }; 1981 1982 // renders truecolor ANSI art 1983 pub const AnsiArt = struct { 1984 focus: *Focus, 1985 grid: ?Grid, 1986 content: []const u8, 1987 1988 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1989 return .{ 1990 .focus = try Focus.create(allocator, .container), 1991 .grid = null, 1992 .content = content, 1993 }; 1994 } 1995 1996 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1997 self.focus.destroy(allocator); 1998 if (self.grid) |*grid| { 1999 grid.deinit(); 2000 self.grid = null; 2001 } 2002 } 2003 2004 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2005 _ = root_focus; 2006 self.clearGrid(); 2007 2008 // parse into rows of cells, then copy into a rectangular grid 2009 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2010 defer { 2011 for (rows.items) |*row| row.deinit(allocator); 2012 rows.deinit(allocator); 2013 } 2014 var row: std.ArrayList(Grid.Cell) = .empty; 2015 errdefer row.deinit(allocator); 2016 2017 var style: Grid.Style = .{}; 2018 var width: usize = 0; 2019 const content = self.content; 2020 var i: usize = 0; 2021 while (i < content.len) { 2022 const byte = content[i]; 2023 if (byte == '\n') { 2024 width = @max(width, row.items.len); 2025 try rows.append(allocator, row); 2026 row = .empty; 2027 i += 1; 2028 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2029 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2030 var j = i + 2; 2031 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2032 if (j >= content.len) { 2033 i = content.len; // malformed trailing escape; stop 2034 } else { 2035 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2036 i = j + 1; 2037 } 2038 } else { 2039 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2040 const end = @min(content.len, i + len); 2041 const rune = content[i..end]; 2042 const transparent = end - i == 1 and rune[0] == ' ' and 2043 style.fg == null and style.bg == null and !style.inverted; 2044 try row.append(allocator, .{ 2045 .rune = if (transparent) null else rune, 2046 .style = style, 2047 }); 2048 i = end; 2049 } 2050 } 2051 // a trailing row with content but no closing newline 2052 if (row.items.len > 0) { 2053 width = @max(width, row.items.len); 2054 try rows.append(allocator, row); 2055 } else { 2056 row.deinit(allocator); 2057 } 2058 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2059 2060 const height = rows.items.len; 2061 if (width == 0 or height == 0) return; 2062 2063 const clamped_w = @min(width, constraint.max_size.width orelse width); 2064 const clamped_h = @min(height, constraint.max_size.height orelse height); 2065 2066 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2067 errdefer grid.deinit(); 2068 for (rows.items[0..clamped_h], 0..) |r, y| { 2069 const n = @min(r.items.len, clamped_w); 2070 for (r.items[0..n], 0..) |cell, x| { 2071 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2072 } 2073 } 2074 self.grid = grid; 2075 } 2076 2077 pub fn clearGrid(self: *AnsiArt) void { 2078 if (self.grid) |*grid| { 2079 grid.deinit(); 2080 self.grid = null; 2081 } 2082 } 2083 2084 pub fn getGrid(self: AnsiArt) ?Grid { 2085 return self.grid; 2086 } 2087 2088 pub fn getFocus(self: *AnsiArt) *Focus { 2089 return self.focus; 2090 } 2091 2092 fn applySgr(style: *Grid.Style, params: []const u8) void { 2093 var nums: [16]u32 = undefined; 2094 var n: usize = 0; 2095 var it = std.mem.splitScalar(u8, params, ';'); 2096 while (it.next()) |tok| { 2097 if (n >= nums.len) break; 2098 // an empty parameter (e.g. bare "\x1b[m") means 0 2099 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2100 n += 1; 2101 } 2102 if (n == 0) { 2103 style.* = .{}; 2104 return; 2105 } 2106 var i: usize = 0; 2107 while (i < n) : (i += 1) { 2108 switch (nums[i]) { 2109 0 => style.* = .{}, 2110 7 => style.inverted = true, 2111 27 => style.inverted = false, 2112 39 => style.fg = null, 2113 49 => style.bg = null, 2114 38, 48 => { 2115 // truecolor form: 38;2;r;g;b — anything else is ignored 2116 if (i + 4 < n and nums[i + 1] == 2) { 2117 const c = Grid.Color{ 2118 .r = @truncate(nums[i + 2]), 2119 .g = @truncate(nums[i + 3]), 2120 .b = @truncate(nums[i + 4]), 2121 }; 2122 if (nums[i] == 38) style.fg = c else style.bg = c; 2123 i += 4; 2124 } 2125 }, 2126 else => {}, 2127 } 2128 } 2129 } 2130 }; 2131 2132 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2133 pub const AnsiBackground = struct { 2134 grid: ?Grid, 2135 child: *Widget, 2136 art: AnsiArt, 2137 session: *Session, 2138 2139 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2140 var cw = child_widget; 2141 const child = allocator.create(Widget) catch |e| { 2142 cw.deinit(allocator); 2143 return e; 2144 }; 2145 child.* = cw; 2146 errdefer { 2147 child.deinit(allocator); 2148 allocator.destroy(child); 2149 } 2150 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2151 } 2152 2153 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2154 if (self.grid) |*grid| grid.deinit(); 2155 self.art.deinit(allocator); 2156 self.child.deinit(allocator); 2157 allocator.destroy(self.child); 2158 } 2159 2160 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2161 self.clearGrid(); 2162 2163 // make the wrapped page fill the whole available area 2164 try self.child.build(allocator, .{ 2165 .min_size = .{ 2166 .width = constraint.max_size.width orelse constraint.min_size.width, 2167 .height = constraint.max_size.height orelse constraint.min_size.height, 2168 }, 2169 .max_size = constraint.max_size, 2170 }, root_focus); 2171 2172 if (self.session.data.enable_ansi) { 2173 if (self.child.getGrid()) |fg| { 2174 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2175 } 2176 } 2177 } 2178 2179 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2180 try self.child.input(allocator, key, root_focus); 2181 } 2182 2183 pub fn clearGrid(self: *AnsiBackground) void { 2184 if (self.grid) |*grid| { 2185 grid.deinit(); 2186 self.grid = null; 2187 } 2188 self.child.clearGrid(); 2189 self.art.clearGrid(); 2190 } 2191 2192 pub fn getGrid(self: AnsiBackground) ?Grid { 2193 return self.grid orelse self.child.getGrid(); 2194 } 2195 2196 pub fn getFocus(self: *AnsiBackground) *Focus { 2197 return self.child.getFocus(); 2198 } 2199 2200 // which top corner the art hugs. 2201 const ArtAnchor = enum { top_left, top_right }; 2202 2203 // a foreground cell counts as blank if it's empty or a bare space without styling 2204 fn cellIsBlank(cell: Grid.Cell) bool { 2205 if (cell.rune) |rune| { 2206 return std.mem.eql(u8, rune, " ") and 2207 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2208 } 2209 return true; 2210 } 2211 2212 // the art is dimmed to this fraction of its brightness so foreground text 2213 // stays legible over it 2214 const art_brightness = 45; // percent 2215 2216 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2217 const c = color orelse return null; 2218 return .{ 2219 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2220 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2221 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2222 }; 2223 } 2224 2225 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2226 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2227 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2228 // snap such channels clear of that range so the backdrop doesn't blink or 2229 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2230 // actually render truecolor. 2231 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2232 const c = color orelse return null; 2233 const snap = struct { 2234 fn f(v: u8) u8 { 2235 return if (v >= 1 and v <= 9) 10 else v; 2236 } 2237 }.f; 2238 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2239 } 2240 2241 // composites ANSI art behind a foreground grid 2242 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2243 art.clearGrid(); 2244 try art.build(allocator, .{ 2245 .min_size = .{ .width = null, .height = null }, 2246 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2247 }, root_focus); 2248 2249 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2250 errdefer out.deinit(); 2251 2252 if (art.getGrid()) |art_grid| { 2253 const anchor_x = switch (anchor) { 2254 .top_left => 0, 2255 .top_right => foreground.size.width -| art_grid.size.width, 2256 }; 2257 for (0..art_grid.size.height) |y| { 2258 for (0..art_grid.size.width) |x| { 2259 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2260 if (src.rune == null) continue; 2261 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2262 const dst = &out.cells.items[idx]; 2263 // composite the dimmed art behind every cell so it shows 2264 // through the whole UI: a blank cell becomes the art, while a 2265 // cell with a glyph keeps the glyph but takes the dimmed art as 2266 // its background (replacing whatever background it had, which 2267 // would otherwise obscure the art). 2268 if (cellIsBlank(dst.*)) { 2269 dst.* = src; 2270 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2271 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2272 } else { 2273 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2274 } 2275 } 2276 } 2277 } 2278 return out; 2279 } 2280 }; 2281