╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const xitui = xit.xitui; 5 const term = xitui.terminal; 6 const wgt = xitui.widget; 7 const layout = xitui.layout; 8 const inp = xitui.input; 9 const Grid = xitui.grid.Grid; 10 const Focus = xitui.focus.Focus; 11 const evt = @import("./event.zig"); 12 13 pub const canonical_repo_opts: rp.RepoOpts(.xit) = .{}; 14 pub const DB = rp.Repo(.xit, canonical_repo_opts).DB; 15 16 pub const Home = @import("./ui/Home.zig"); 17 pub const Title = @import("./ui/Title.zig"); 18 19 pub const Page = union(enum) { 20 home: Home, 21 }; 22 23 // a top-level "page" the user can navigate to 24 pub const RoutablePage = enum { 25 home_users, 26 home_repos, 27 home_ansi, 28 home_auth, 29 30 pub const default: RoutablePage = .home_users; 31 32 pub fn url(self: RoutablePage) []const u8 { 33 return switch (self) { 34 .home_users => "/users", 35 .home_repos => "/repos", 36 .home_ansi => "/ansi", 37 .home_auth => "/auth", 38 }; 39 } 40 41 pub fn fromUrl(path: []const u8) ?RoutablePage { 42 if (std.mem.eql(u8, path, "/")) return default; 43 if (std.mem.eql(u8, path, "/users")) return .home_users; 44 if (std.mem.eql(u8, path, "/repos")) return .home_repos; 45 if (std.mem.eql(u8, path, "/ansi")) return .home_ansi; 46 if (std.mem.eql(u8, path, "/auth")) return .home_auth; 47 return null; 48 } 49 }; 50 51 // per-connection mutable state. each SSH session / web session / local TUI 52 // run gets its own 53 pub const Session = struct { 54 data: Data = .{}, // serializable data sent down to web client 55 arena: *std.heap.ArenaAllocator, 56 haxy_moment: ?DB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 57 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 58 59 pub const Data = struct { 60 user_id: ?[]const u8 = null, 61 // a transient outcome to surface from the last /login POST attempt 62 login_failure: ?Home.Auth.Login.Failure = null, 63 current_page: RoutablePage = .default, 64 // whether to render the ANSI art backdrop 65 enable_ansi: bool = false, 66 }; 67 68 pub fn init( 69 comptime repo_opts: rp.RepoOpts(.xit), 70 arena: *std.heap.ArenaAllocator, 71 repo: *rp.Repo(.xit, repo_opts), 72 data: Data, 73 ) !Session { 74 var session = Session{ 75 .data = data, 76 .arena = arena, 77 .haxy_moment = try evt.currentMoment(repo_opts, repo), 78 }; 79 try session.loadUserPrefs(); 80 return session; 81 } 82 83 // load the logged-in user's persisted preferences from the db 84 pub fn loadUserPrefs(self: *Session) !void { 85 const user_id = self.data.user_id orelse return; 86 const moment = self.haxy_moment orelse return; 87 if (try evt.User.readById(DB, canonical_repo_opts.hash, moment, self.arena, user_id)) |user| { 88 self.data.enable_ansi = user.enable_ansi; 89 } 90 } 91 92 // queue an action for the host to drain this frame. 93 pub fn push(self: *Session, action: Action) !void { 94 try self.pending.append(self.arena.allocator(), action); 95 } 96 97 // apply a single action's in-memory effect only (no persistence). 98 pub fn apply(self: *Session, action: Action) void { 99 switch (action) { 100 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 101 } 102 } 103 104 // drain queued actions, applying only their in-memory effect. used on the 105 // wasm path, which has no repo to persist to (and only anonymous toggles 106 // reach it anyway). 107 pub fn applyPending(self: *Session) void { 108 for (self.pending.items) |action| self.apply(action); 109 self.pending.clearRetainingCapacity(); 110 } 111 }; 112 113 // a user-initiated state change. widgets enqueue these on the Session during 114 // input instead of mutating state or touching the DB themselves; the host 115 // drains them each frame (see dispatchPending / applyPending). this keeps DB 116 // side effects out of the widget tree and gives every render path one place to 117 // turn a UI action into a state change + event. 118 pub const Action = union(enum) { 119 toggle_ansi, 120 121 // apply this action and, when it maps to persistent state and the session 122 // is logged in, emit the corresponding event. the single place that knows 123 // how a UI action becomes a DB write. 124 pub fn dispatch( 125 self: Action, 126 comptime repo_opts: rp.RepoOpts(.xit), 127 io: std.Io, 128 allocator: std.mem.Allocator, 129 repo: *rp.Repo(.xit, repo_opts), 130 session: *Session, 131 ) !void { 132 session.apply(self); 133 switch (self) { 134 .toggle_ansi => if (session.data.user_id) |user_id| { 135 try evt.User.toggleAnsi(repo_opts, io, allocator, repo, user_id); 136 }, 137 } 138 } 139 140 // drain the session's queued actions through dispatch. always clears the 141 // queue, even if a dispatch fails, so a single bad write isn't retried 142 // every frame. 143 pub fn dispatchPending( 144 comptime repo_opts: rp.RepoOpts(.xit), 145 io: std.Io, 146 allocator: std.mem.Allocator, 147 repo: *rp.Repo(.xit, repo_opts), 148 session: *Session, 149 ) !void { 150 defer session.pending.clearRetainingCapacity(); 151 for (session.pending.items) |action| { 152 try action.dispatch(repo_opts, io, allocator, repo, session); 153 } 154 } 155 }; 156 157 // what the server hands to the client (and what main_wasm parses on _start). 158 // keeps Page free of any per-request session state. 159 pub const Snapshot = struct { 160 page: Page, 161 session: Session.Data = .{}, 162 }; 163 164 pub fn run(io: std.Io, allocator: std.mem.Allocator, page: *const Page, session: *Session) !void { 165 var root = try initRoot(allocator, page, session); 166 defer root.deinit(allocator); 167 168 var terminal = try term.Terminal.init(io, allocator); 169 defer terminal.deinit(io); 170 171 var last_size = layout.Size{ .width = 0, .height = 0 }; 172 var last_grid = try Grid.init(allocator, last_size); 173 defer last_grid.deinit(); 174 175 while (!terminal.shouldQuit()) { 176 const grid_changed = try terminal.render(&root, &last_grid, &last_size); 177 178 // process any inputs. 179 // 180 // if the grid didn't change, then first do a blocking 181 // read, so the thread will sleep until further input. 182 // after that, all remaining reads are non-blocking so 183 // we can process the rest of the queued inputs. 184 // 185 // if the grid *did* change, then only do non-blocking 186 // reads. we do not want to sleep the thread because 187 // there may be an animation that requires more looping. 188 var blocking = !grid_changed; 189 while (try terminal.readKey(io, blocking)) |key| { 190 try inputKey(allocator, &root, key, &terminal); 191 blocking = false; 192 } 193 194 // local run has no repo to persist to, so just apply in-memory 195 session.applyPending(); 196 197 try root.build(allocator, .{ 198 .min_size = .{ .width = null, .height = null }, 199 .max_size = .{ .width = last_size.width, .height = last_size.height }, 200 }, root.getFocus()); 201 } 202 } 203 204 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: inp.Key, terminal: anytype) !void { 205 switch (key) { 206 .escape => terminal.requestQuit(), 207 .mouse => |mouse| { 208 if (mouse.action == .press and mouse.action.press == .left) { 209 const root_focus = root.getFocus(); 210 var iter = root_focus.children.iterator(); 211 while (iter.next()) |entry| { 212 const child = entry.value_ptr.*; 213 if (!child.focus.focusable) continue; 214 const r = child.rect; 215 if (mouse.x >= r.x and mouse.y >= r.y and 216 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 217 { 218 try root_focus.setFocus(entry.key_ptr.*); 219 break; 220 } 221 } 222 // forward the press into the widget tree so buttons (and any 223 // future click-aware widgets) can react. widgets that don't 224 // care about presses ignore it. 225 try root.input(allocator, key, root.getFocus()); 226 } else { 227 try root.input(allocator, key, root.getFocus()); 228 } 229 }, 230 else => try root.input(allocator, key, root.getFocus()), 231 } 232 } 233 234 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 235 const page_widget: Widget = switch (page.*) { 236 .home => |*p| .{ .home = try .init(allocator, p, session) }, 237 }; 238 239 const demon_art = @embedFile("embed/demon.ans"); 240 241 var root = Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 242 errdefer root.deinit(allocator); 243 244 try root.build(allocator, .{ 245 .min_size = .{ .width = null, .height = null }, 246 .max_size = .{ .width = 80, .height = null }, 247 }, root.getFocus()); 248 if (root.getFocus().child_id) |child_id| { 249 try root.getFocus().setFocus(child_id); 250 } 251 252 return root; 253 } 254 255 pub const Widget = union(enum) { 256 text: wgt.Text(Widget), 257 box: wgt.Box(Widget), 258 text_box: wgt.TextBox(Widget), 259 text_input: wgt.TextInput(Widget), 260 scroll: wgt.Scroll(Widget), 261 stack: wgt.Stack(Widget), 262 flow_box: FlowBox, 263 spacer: Spacer, 264 center: Center, 265 ansi_art: AnsiArt, 266 background: AnsiBackground, 267 home: Home.View, 268 title: Title.View, 269 home_header: Home.Header.View, 270 home_users: Home.Users.View, 271 home_repos: Home.Repos.View, 272 home_auth_tab: Home.Header.AuthTab.View, 273 home_ansi: Home.Ansi.View, 274 home_auth: Home.Auth.View, 275 276 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 277 switch (self.*) { 278 inline else => |*case| case.deinit(allocator), 279 } 280 } 281 282 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 283 switch (self.*) { 284 inline else => |*case| try case.build(allocator, constraint, root_focus), 285 } 286 } 287 288 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) anyerror!void { 289 switch (self.*) { 290 inline else => |*case| try case.input(allocator, key, root_focus), 291 } 292 } 293 294 pub fn clearGrid(self: *Widget) void { 295 switch (self.*) { 296 inline else => |*case| case.clearGrid(), 297 } 298 } 299 300 pub fn getGrid(self: Widget) ?Grid { 301 switch (self) { 302 inline else => |*case| return case.getGrid(), 303 } 304 } 305 306 pub fn getFocus(self: *Widget) *Focus { 307 switch (self.*) { 308 inline else => |*case| return case.getFocus(), 309 } 310 } 311 }; 312 313 pub const FlowBox = struct { 314 focus: Focus, 315 grid: ?Grid, 316 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 317 lines: std.ArrayList([]const u8), 318 // column count from the last build — FlowBox.Scroll.input uses it so arrow 319 // up/down can step by a row's worth of items. 320 last_cols: usize, 321 options: Options, 322 323 const border_rows: usize = 2; 324 325 pub const Options = struct { 326 cell_width: usize = 40, 327 cell_height: usize = 3, 328 }; 329 330 pub fn init(options: Options) FlowBox { 331 return .{ 332 .focus = Focus.init(.container), 333 .grid = null, 334 .text_boxes = .empty, 335 .lines = .empty, 336 .last_cols = 1, 337 .options = options, 338 }; 339 } 340 341 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 342 self.focus.deinit(allocator); 343 if (self.grid) |*grid| { 344 grid.deinit(); 345 self.grid = null; 346 } 347 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 348 self.text_boxes.deinit(allocator); 349 for (self.lines.items) |line| allocator.free(line); 350 self.lines.deinit(allocator); 351 } 352 353 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const []const u8) !void { 354 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 355 self.text_boxes.clearAndFree(allocator); 356 357 for (self.lines.items) |line| allocator.free(line); 358 self.lines.clearAndFree(allocator); 359 360 self.focus.clear(); 361 self.focus.child_id = null; 362 363 for (items) |item| { 364 const line = try allocator.dupe(u8, item); 365 { 366 errdefer allocator.free(line); 367 try self.lines.append(allocator, line); 368 } 369 370 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 371 errdefer text_box.deinit(allocator); 372 text_box.getFocus().focusable = true; 373 try self.text_boxes.append(allocator, text_box); 374 } 375 376 if (self.text_boxes.items.len > 0) { 377 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 378 } 379 } 380 381 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 382 self.clearGrid(); 383 self.focus.clear(); 384 385 const cell_width = self.options.cell_width; 386 const max_width = constraint.max_size.width orelse cell_width; 387 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 388 self.last_cols = cols; 389 390 const count = self.text_boxes.items.len; 391 if (count == 0) return; 392 const slot_height = self.options.cell_height + border_rows; 393 if (slot_height == 0) return; 394 395 // build at the slot size so every text box fits a single grid cell 396 for (self.text_boxes.items) |*tb| { 397 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 398 try tb.build(allocator, .{ 399 .min_size = .{ .width = cell_width, .height = null }, 400 .max_size = .{ .width = cell_width, .height = slot_height }, 401 }, root_focus); 402 } 403 404 const rows = (count + cols - 1) / cols; 405 const content_height = rows * slot_height; 406 const total_width = cols * cell_width; 407 if (total_width == 0 or content_height == 0) return; 408 409 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 410 errdefer grid.deinit(); 411 412 for (self.text_boxes.items, 0..) |*tb, i| { 413 const tb_grid = tb.getGrid() orelse continue; 414 const col = i % cols; 415 const row = i / cols; 416 const cell_x = col * cell_width; 417 const cell_y = row * slot_height; 418 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 419 try grid.drawGrid(tb_grid, cell_x, cell_y); 420 } 421 422 self.grid = grid; 423 } 424 425 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 426 _ = self; 427 _ = allocator; 428 _ = key; 429 _ = root_focus; 430 } 431 432 pub fn clearGrid(self: *FlowBox) void { 433 if (self.grid) |*grid| { 434 grid.deinit(); 435 self.grid = null; 436 } 437 for (self.text_boxes.items) |*tb| tb.clearGrid(); 438 } 439 440 pub fn getGrid(self: FlowBox) ?Grid { 441 return self.grid; 442 } 443 444 pub fn getFocus(self: *FlowBox) *Focus { 445 return &self.focus; 446 } 447 448 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 449 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 450 const slot_height = self.options.cell_height + border_rows; 451 const col = index % self.last_cols; 452 const row = index / self.last_cols; 453 return .{ 454 .x = @intCast(col * self.options.cell_width), 455 .y = @intCast(row * slot_height), 456 .size = .{ .width = self.options.cell_width, .height = slot_height }, 457 }; 458 } 459 460 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 461 for (self.text_boxes.items, 0..) |tb, i| { 462 if (tb.box.focus.id == focus_id) return i; 463 } 464 return null; 465 } 466 467 pub const Scroll = struct { 468 scroll: wgt.Scroll(Widget), 469 470 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options) !Scroll { 471 var layout_inner = FlowBox.init(options); 472 errdefer layout_inner.deinit(allocator); 473 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .vert); 474 errdefer scroll.deinit(allocator); 475 return .{ .scroll = scroll }; 476 } 477 478 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 479 self.scroll.deinit(allocator); 480 } 481 482 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const []const u8) !void { 483 self.scroll.x = 0; 484 self.scroll.y = 0; 485 try self.inner().setItems(allocator, items); 486 } 487 488 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 489 try self.scroll.build(allocator, constraint, root_focus); 490 } 491 492 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 493 _ = allocator; 494 const in = self.inner(); 495 const child_id = in.focus.child_id orelse return; 496 const current_index = in.indexOfFocusId(child_id) orelse return; 497 const count = in.text_boxes.items.len; 498 if (count == 0) return; 499 const cols = in.last_cols; 500 const slot_height = in.options.cell_height + FlowBox.border_rows; 501 502 var index = current_index; 503 switch (key) { 504 .arrow_up => index -|= cols, 505 .arrow_down => if (index + cols < count) { 506 index += cols; 507 }, 508 .arrow_left => index -|= 1, 509 .arrow_right => if (index + 1 < count) { 510 index += 1; 511 }, 512 .home => index = 0, 513 .end => index = count - 1, 514 .page_up => { 515 if (self.scroll.grid) |grid| if (slot_height > 0) { 516 const rows_per_page = grid.size.height / slot_height; 517 index -|= rows_per_page * cols; 518 }; 519 }, 520 .page_down => { 521 if (self.scroll.grid) |grid| if (slot_height > 0) { 522 const rows_per_page = grid.size.height / slot_height; 523 index = @min(index + rows_per_page * cols, count - 1); 524 }; 525 }, 526 // scroll wheel moves the focused cell by a full row so the 527 // viewport (via scrollToRect) follows in row-sized steps, 528 // matching how a scroll wheel feels in a grid view. 529 .mouse => |mouse| switch (mouse.action) { 530 .scroll => |dir| switch (dir) { 531 .up => index -|= cols, 532 .down => if (index + cols < count) { 533 index += cols; 534 }, 535 }, 536 else => {}, 537 }, 538 else => {}, 539 } 540 541 if (index != current_index) { 542 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 543 if (in.cellRect(index)) |rect| { 544 self.scroll.scrollToRect(rect); 545 } 546 } 547 } 548 549 pub fn clearGrid(self: *Scroll) void { 550 self.scroll.clearGrid(); 551 } 552 553 pub fn getGrid(self: Scroll) ?Grid { 554 return self.scroll.getGrid(); 555 } 556 557 pub fn getFocus(self: *Scroll) *Focus { 558 return self.scroll.getFocus(); 559 } 560 561 pub fn getSelectedIndex(self: Scroll) ?usize { 562 const in = self.scroll.child.flow_box; 563 const child_id = in.focus.child_id orelse return null; 564 return in.indexOfFocusId(child_id); 565 } 566 567 fn inner(self: *Scroll) *FlowBox { 568 return &self.scroll.child.flow_box; 569 } 570 }; 571 }; 572 573 // an invisible widget that fills the horizontal space granted by its parent. 574 // used inside Box(horiz) with a min_size so the box reserves space for the 575 // children that follow, pushing them to the right. 576 pub const Spacer = struct { 577 focus: Focus, 578 grid: ?Grid, 579 580 pub fn init() Spacer { 581 return .{ 582 .focus = Focus.init(.container), 583 .grid = null, 584 }; 585 } 586 587 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 588 self.focus.deinit(allocator); 589 if (self.grid) |*grid| { 590 grid.deinit(); 591 self.grid = null; 592 } 593 } 594 595 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 596 _ = root_focus; 597 self.clearGrid(); 598 const width = constraint.max_size.width orelse return; 599 if (width == 0) return; 600 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 601 } 602 603 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 604 _ = self; 605 _ = allocator; 606 _ = key; 607 _ = root_focus; 608 } 609 610 pub fn clearGrid(self: *Spacer) void { 611 if (self.grid) |*grid| { 612 grid.deinit(); 613 self.grid = null; 614 } 615 } 616 617 pub fn getGrid(self: Spacer) ?Grid { 618 return self.grid; 619 } 620 621 pub fn getFocus(self: *Spacer) *Focus { 622 return &self.focus; 623 } 624 }; 625 626 // a single-child wrapper that builds the child at its natural size and 627 // positions its grid in the middle of the area granted by the parent 628 pub const Center = struct { 629 focus: Focus, 630 grid: ?Grid, 631 child: *Widget, 632 direction: Direction, 633 634 pub const Direction = enum { both, horiz, vert }; 635 636 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 637 const child = try allocator.create(Widget); 638 errdefer allocator.destroy(child); 639 child.* = child_widget; 640 return .{ 641 .focus = Focus.init(.container), 642 .grid = null, 643 .child = child, 644 .direction = direction, 645 }; 646 } 647 648 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 649 self.focus.deinit(allocator); 650 if (self.grid) |*grid| { 651 grid.deinit(); 652 self.grid = null; 653 } 654 self.child.deinit(allocator); 655 allocator.destroy(self.child); 656 } 657 658 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 659 self.clearGrid(); 660 self.getFocus().clear(); 661 662 // build the child without forcing it to fill min_size; it sizes 663 // itself to its content within the available max. 664 try self.child.build(allocator, .{ 665 .min_size = .{ .width = null, .height = null }, 666 .max_size = constraint.max_size, 667 }, root_focus); 668 669 const child_grid = self.child.getGrid() orelse return; 670 671 // prefer max when given; otherwise grow to max(min, child) so that 672 // when only a min is set (e.g. the wasm path passes viewport rows 673 // as min_height with no max), we can still vertically center while 674 // letting taller content extend past the min. 675 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; 676 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; 677 if (width == 0 or height == 0) return; 678 679 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 680 (width -| child_grid.size.width) / 2 681 else 682 0; 683 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 684 (height -| child_grid.size.height) / 2 685 else 686 0; 687 688 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 689 errdefer grid.deinit(); 690 try grid.drawGrid(child_grid, offset_x, offset_y); 691 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 692 self.getFocus().child_id = self.child.getFocus().id; 693 694 self.grid = grid; 695 } 696 697 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 698 try self.child.input(allocator, key, root_focus); 699 } 700 701 pub fn clearGrid(self: *Center) void { 702 if (self.grid) |*grid| { 703 grid.deinit(); 704 self.grid = null; 705 } 706 self.child.clearGrid(); 707 } 708 709 pub fn getGrid(self: Center) ?Grid { 710 return self.grid; 711 } 712 713 pub fn getFocus(self: *Center) *Focus { 714 return &self.focus; 715 } 716 }; 717 718 // renders truecolor ANSI art 719 pub const AnsiArt = struct { 720 focus: Focus, 721 grid: ?Grid, 722 content: []const u8, 723 724 pub fn init(content: []const u8) AnsiArt { 725 return .{ 726 .focus = Focus.init(.container), 727 .grid = null, 728 .content = content, 729 }; 730 } 731 732 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 733 self.focus.deinit(allocator); 734 if (self.grid) |*grid| { 735 grid.deinit(); 736 self.grid = null; 737 } 738 } 739 740 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 741 _ = root_focus; 742 self.clearGrid(); 743 744 // parse into rows of cells, then copy into a rectangular grid 745 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 746 defer { 747 for (rows.items) |*row| row.deinit(allocator); 748 rows.deinit(allocator); 749 } 750 var row: std.ArrayList(Grid.Cell) = .empty; 751 errdefer row.deinit(allocator); 752 753 var style: Grid.Style = .{}; 754 var width: usize = 0; 755 const content = self.content; 756 var i: usize = 0; 757 while (i < content.len) { 758 const byte = content[i]; 759 if (byte == '\n') { 760 width = @max(width, row.items.len); 761 try rows.append(allocator, row); 762 row = .empty; 763 i += 1; 764 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 765 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 766 var j = i + 2; 767 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 768 if (j >= content.len) { 769 i = content.len; // malformed trailing escape; stop 770 } else { 771 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 772 i = j + 1; 773 } 774 } else { 775 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 776 const end = @min(content.len, i + len); 777 const rune = content[i..end]; 778 const transparent = end - i == 1 and rune[0] == ' ' and 779 style.fg == null and style.bg == null and !style.inverted; 780 try row.append(allocator, .{ 781 .rune = if (transparent) null else rune, 782 .style = style, 783 }); 784 i = end; 785 } 786 } 787 // a trailing row with content but no closing newline 788 if (row.items.len > 0) { 789 width = @max(width, row.items.len); 790 try rows.append(allocator, row); 791 } else { 792 row.deinit(allocator); 793 } 794 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 795 796 const height = rows.items.len; 797 if (width == 0 or height == 0) return; 798 799 const clamped_w = @min(width, constraint.max_size.width orelse width); 800 const clamped_h = @min(height, constraint.max_size.height orelse height); 801 802 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 803 errdefer grid.deinit(); 804 for (rows.items[0..clamped_h], 0..) |r, y| { 805 const n = @min(r.items.len, clamped_w); 806 for (r.items[0..n], 0..) |cell, x| { 807 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 808 } 809 } 810 self.grid = grid; 811 } 812 813 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 814 _ = self; 815 _ = allocator; 816 _ = key; 817 _ = root_focus; 818 } 819 820 pub fn clearGrid(self: *AnsiArt) void { 821 if (self.grid) |*grid| { 822 grid.deinit(); 823 self.grid = null; 824 } 825 } 826 827 pub fn getGrid(self: AnsiArt) ?Grid { 828 return self.grid; 829 } 830 831 pub fn getFocus(self: *AnsiArt) *Focus { 832 return &self.focus; 833 } 834 835 fn applySgr(style: *Grid.Style, params: []const u8) void { 836 var nums: [16]u32 = undefined; 837 var n: usize = 0; 838 var it = std.mem.splitScalar(u8, params, ';'); 839 while (it.next()) |tok| { 840 if (n >= nums.len) break; 841 // an empty parameter (e.g. bare "\x1b[m") means 0 842 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 843 n += 1; 844 } 845 if (n == 0) { 846 style.* = .{}; 847 return; 848 } 849 var i: usize = 0; 850 while (i < n) : (i += 1) { 851 switch (nums[i]) { 852 0 => style.* = .{}, 853 7 => style.inverted = true, 854 27 => style.inverted = false, 855 39 => style.fg = null, 856 49 => style.bg = null, 857 38, 48 => { 858 // truecolor form: 38;2;r;g;b — anything else is ignored 859 if (i + 4 < n and nums[i + 1] == 2) { 860 const c = Grid.Color{ 861 .r = @truncate(nums[i + 2]), 862 .g = @truncate(nums[i + 3]), 863 .b = @truncate(nums[i + 4]), 864 }; 865 if (nums[i] == 38) style.fg = c else style.bg = c; 866 i += 4; 867 } 868 }, 869 else => {}, 870 } 871 } 872 } 873 }; 874 875 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 876 pub const AnsiBackground = struct { 877 grid: ?Grid, 878 child: *Widget, 879 art: AnsiArt, 880 session: *Session, 881 882 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 883 var cw = child_widget; 884 const child = allocator.create(Widget) catch |e| { 885 cw.deinit(allocator); 886 return e; 887 }; 888 child.* = cw; 889 return .{ .grid = null, .child = child, .art = AnsiArt.init(art_content), .session = session }; 890 } 891 892 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 893 if (self.grid) |*grid| grid.deinit(); 894 self.art.deinit(allocator); 895 self.child.deinit(allocator); 896 allocator.destroy(self.child); 897 } 898 899 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 900 self.clearGrid(); 901 902 // make the wrapped page fill the whole available area 903 try self.child.build(allocator, .{ 904 .min_size = .{ 905 .width = constraint.max_size.width orelse constraint.min_size.width, 906 .height = constraint.max_size.height orelse constraint.min_size.height, 907 }, 908 .max_size = constraint.max_size, 909 }, root_focus); 910 911 if (self.session.data.enable_ansi) { 912 if (self.child.getGrid()) |fg| { 913 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 914 } 915 } 916 } 917 918 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 919 try self.child.input(allocator, key, root_focus); 920 } 921 922 pub fn clearGrid(self: *AnsiBackground) void { 923 if (self.grid) |*grid| { 924 grid.deinit(); 925 self.grid = null; 926 } 927 self.child.clearGrid(); 928 self.art.clearGrid(); 929 } 930 931 pub fn getGrid(self: AnsiBackground) ?Grid { 932 return self.grid orelse self.child.getGrid(); 933 } 934 935 pub fn getFocus(self: *AnsiBackground) *Focus { 936 return self.child.getFocus(); 937 } 938 939 // which top corner the art hugs. 940 const ArtAnchor = enum { top_left, top_right }; 941 942 // a foreground cell counts as blank if it's empty or a bare space without styling 943 fn cellIsBlank(cell: Grid.Cell) bool { 944 if (cell.rune) |rune| { 945 return std.mem.eql(u8, rune, " ") and 946 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 947 } 948 return true; 949 } 950 951 // a single representative color for an art cell 952 fn artCellColor(style: Grid.Style) ?Grid.Color { 953 const f = style.fg orelse return style.bg; 954 const b = style.bg orelse return f; 955 return .{ 956 .r = @intCast((@as(u16, f.r) + b.r) / 2), 957 .g = @intCast((@as(u16, f.g) + b.g) / 2), 958 .b = @intCast((@as(u16, f.b) + b.b) / 2), 959 }; 960 } 961 962 // behind a visible glyph the art color is dimmed to this fraction of its 963 // brightness so light terminal text stays legible on top of it 964 const glyph_bg_brightness = 45; // percent 965 966 // a glyph sitting over the art also gets its foreground pinned to this color 967 // (unless it set its own), so the text stays legible 968 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 969 970 fn dimColor(color: ?Grid.Color) ?Grid.Color { 971 const c = color orelse return null; 972 return .{ 973 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 974 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 975 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 976 }; 977 } 978 979 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 980 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 981 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 982 // snap such channels clear of that range so the backdrop doesn't blink or 983 // hide text there; the ≤9/255 shift is imperceptible on terminals that 984 // actually render truecolor. 985 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 986 const c = color orelse return null; 987 const snap = struct { 988 fn f(v: u8) u8 { 989 return if (v >= 1 and v <= 9) 10 else v; 990 } 991 }.f; 992 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 993 } 994 995 // composites ANSI art behind a foreground grid 996 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 997 art.clearGrid(); 998 try art.build(allocator, .{ 999 .min_size = .{ .width = null, .height = null }, 1000 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1001 }, root_focus); 1002 1003 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1004 errdefer out.deinit(); 1005 1006 if (art.getGrid()) |art_grid| { 1007 const anchor_x = switch (anchor) { 1008 .top_left => 0, 1009 .top_right => foreground.size.width -| art_grid.size.width, 1010 }; 1011 for (0..art_grid.size.height) |y| { 1012 for (0..art_grid.size.width) |x| { 1013 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1014 if (src.rune == null) continue; 1015 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1016 const dst = &out.cells.items[idx]; 1017 if (cellIsBlank(dst.*)) { 1018 dst.* = src; 1019 dst.style.fg = sgrSafe(dst.style.fg); 1020 dst.style.bg = sgrSafe(dst.style.bg); 1021 } else if (dst.style.bg == null) { 1022 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1023 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1024 } 1025 } 1026 } 1027 } 1028 return out; 1029 } 1030 }; 1031