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