╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const inp = @import("../input.zig"); 6 const xit = @import("xit"); 7 const rp = xit.repo; 8 const rf = xit.ref; 9 const hash = xit.hash; 10 const xitui = xit.xitui; 11 const wgt = xitui.widget; 12 const layout = xitui.layout; 13 const Key = xitui.input.Key; 14 const Grid = xitui.grid.Grid; 15 const Focus = xitui.focus.Focus; 16 17 const page_size = 20; 18 19 pub const Item = struct { 20 id: []const u8, 21 kind: evt.EventKind, 22 author: ui.Author, 23 view_url: ?[]const u8 = null, 24 }; 25 26 pub const EventRef = struct { 27 id: []const u8, 28 kind: evt.EventKind, 29 }; 30 31 pub const Window = struct { 32 count: usize = 0, 33 events: []const Item = &.{}, 34 next: ?EventRef = null, 35 }; 36 37 identity: []const u8, 38 view: ui.RoutablePage.EventsView, 39 selected: ?EventRef, 40 active: Window, 41 removed: Window, 42 header: Header, 43 44 const Self = @This(); 45 46 pub fn init( 47 comptime repo_kind: rp.RepoKind, 48 comptime repo_opts: rp.RepoOpts(repo_kind), 49 arena: *std.heap.ArenaAllocator, 50 repo: *rp.Repo(repo_kind, repo_opts), 51 io: std.Io, 52 admin_moment: ?evt.AdminDB.HashMap(.read_only), 53 identity: []const u8, 54 requested_view: ui.RoutablePage.EventsView, 55 selected_kind: ?evt.EventKind, 56 selected: []const u8, 57 local: bool, 58 failure: ?[]const u8, 59 ) !Self { 60 const aa = arena.allocator(); 61 var result = try empty(aa, identity, requested_view, local, failure); 62 if (local) result.header = try Header.local(repo_kind, repo_opts, aa, repo, io, failure); 63 const require_selected_event = selected_kind != null or selected.len != 0; 64 if ((selected_kind == null) != (selected.len == 0)) return error.NotFound; 65 66 const DB = evt.EventDB(repo_opts.hash); 67 const gpa = arena.child_allocator; 68 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 69 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 70 const haxy_moment = (if (event_db_maybe) |*event_db| 71 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 72 else if (repo_kind == .git) 73 return result 74 else 75 evt.currentMoment(repo_opts, repo)) catch { 76 if (require_selected_event) return error.NotFound; 77 return result; 78 }; 79 80 const kind_map_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse { 81 if (require_selected_event) return error.NotFound; 82 return result; 83 }; 84 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 85 86 const active_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.active_event_id_set_key)); 87 const removed_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.removed_event_id_set_key)); 88 89 var root_key: ?[@sizeOf(u64) + evt.event_id_size]u8 = null; 90 if (selected.len != 0) { 91 var id: [evt.event_id_size]u8 = undefined; 92 _ = std.fmt.hexToBytes(&id, selected) catch return error.NotFound; 93 if (!std.mem.eql(u8, selected, &std.fmt.bytesToHex(id, .lower))) return error.NotFound; 94 const kind = selected_kind orelse return error.NotFound; 95 const stored_kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse return error.NotFound; 96 if (stored_kind != kind) return error.NotFound; 97 result.selected = .{ .id = try aa.dupe(u8, selected), .kind = kind }; 98 const meta = try readMeta(repo_opts.hash, arena, haxy_moment, kind, &id); 99 result.view = if (meta.removed) .removed else .active; 100 root_key = evt.orderKeyDesc(meta.created_order, &id); 101 } 102 103 for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 104 const id_set_cursor = switch (view) { 105 .active => active_cursor, 106 .removed => removed_cursor, 107 }; 108 const event_window = switch (view) { 109 .active => &result.active, 110 .removed => &result.removed, 111 }; 112 const id_set = try DB.SortedSet(.read_only).init(id_set_cursor orelse { 113 if (require_selected_event and result.view == view) return error.NotFound; 114 continue; 115 }); 116 event_window.count = @intCast(try id_set.count()); 117 const selected_key = if (result.view == view) root_key else null; 118 if (selected_key) |*key| if (!try id_set.contains(key)) return error.NotFound; 119 120 var iter = if (selected_key) |*key| try id_set.iteratorFrom(key) else try id_set.iteratorFromIndex(0); 121 var events: std.ArrayList(Item) = .empty; 122 while (try iter.next()) |cursor_value| { 123 const id = try evt.readOrderKeyId(DB, cursor_value); 124 const kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse continue; 125 if (events.items.len == page_size) { 126 event_window.next = .{ .id = try formatId(aa, &id), .kind = kind }; 127 break; 128 } 129 const item = (try readItem(repo_opts.hash, arena, haxy_moment, kind_map, admin_moment, identity, view == .active, kind, &id)) orelse continue; 130 try events.append(aa, item); 131 } 132 event_window.events = events.items; 133 } 134 return result; 135 } 136 137 pub fn empty(aa: std.mem.Allocator, identity: []const u8, view: ui.RoutablePage.EventsView, local: bool, failure: ?[]const u8) !Self { 138 return .{ 139 .identity = try aa.dupe(u8, identity), 140 .view = view, 141 .selected = null, 142 .active = .{}, 143 .removed = .{}, 144 .header = if (local) try Header.empty(aa, failure) else Header.remote(), 145 }; 146 } 147 148 fn window(self: *const Self, view: ui.RoutablePage.EventsView) *const Window { 149 return switch (view) { 150 .active => &self.active, 151 .removed => &self.removed, 152 }; 153 } 154 155 fn formatId(aa: std.mem.Allocator, id: *const [evt.event_id_size]u8) ![]const u8 { 156 const hex = std.fmt.bytesToHex(id.*, .lower); 157 return try aa.dupe(u8, &hex); 158 } 159 160 fn readRecord( 161 comptime T: type, 162 comptime hash_kind: hash.HashKind, 163 arena: *std.heap.ArenaAllocator, 164 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 165 id: *const [evt.event_id_size]u8, 166 ) !?T.Record { 167 const DB = evt.EventDB(hash_kind); 168 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, T.record_map_key)) orelse return null; 169 const records = try DB.HashMap(.read_only).init(records_cursor); 170 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 171 const record = try DB.HashMap(.read_only).init(record_cursor); 172 return try evt.read(T.Record, DB, hash_kind, arena, record); 173 } 174 175 const EventMeta = struct { 176 created_order: u64, 177 removed: bool, 178 }; 179 180 fn readMeta( 181 comptime hash_kind: hash.HashKind, 182 arena: *std.heap.ArenaAllocator, 183 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 184 kind: evt.EventKind, 185 id: *const [evt.event_id_size]u8, 186 ) !EventMeta { 187 return switch (kind) { 188 inline else => |event_kind| blk: { 189 const T = switch (event_kind) { 190 .user => evt.User, 191 .repo => evt.Repo, 192 .fork => evt.Fork, 193 .issue => evt.Issue, 194 .discuss => evt.Discussion, 195 .comment => evt.Comment, 196 .attach => evt.Attachment, 197 .patchrev => evt.PatchRev, 198 .patch => evt.Patch, 199 }; 200 const record = (try readRecord(T, hash_kind, arena, haxy_moment, id)) orelse return error.NotFound; 201 break :blk .{ .created_order = record.created_order, .removed = record.removed }; 202 }, 203 }; 204 } 205 206 fn readItem( 207 comptime hash_kind: hash.HashKind, 208 arena: *std.heap.ArenaAllocator, 209 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 210 kind_map: evt.EventDB(hash_kind).HashMap(.read_only), 211 admin_moment: ?evt.AdminDB.HashMap(.read_only), 212 identity: []const u8, 213 include_view_url: bool, 214 kind: evt.EventKind, 215 id: *const [evt.event_id_size]u8, 216 ) !?Item { 217 var item: Item = .{ 218 .id = try formatId(arena.allocator(), id), 219 .kind = kind, 220 .author = .unknown, 221 }; 222 switch (kind) { 223 .user => { 224 const record = (try readRecord(evt.User, hash_kind, arena, haxy_moment, id)) orelse return null; 225 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.event.email); 226 }, 227 .repo => { 228 const record = (try readRecord(evt.Repo, hash_kind, arena, haxy_moment, id)) orelse return null; 229 if (admin_moment) |moment| { 230 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, record.event.user_id)) |user| 231 item.author = .{ .user_name = user.event.name }; 232 } 233 }, 234 .fork => { 235 const record = (try readRecord(evt.Fork, hash_kind, arena, haxy_moment, id)) orelse return null; 236 if (admin_moment) |moment| { 237 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, record.event.user_id)) |user| 238 item.author = .{ .user_name = user.event.name }; 239 } 240 }, 241 .issue => { 242 const record = (try readRecord(evt.Issue, hash_kind, arena, haxy_moment, id)) orelse return null; 243 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 244 if (!include_view_url) return item; 245 const route = ui.RoutablePage.repoThreadCommentsRoute(.issue, identity, item.id, 0) orelse return error.RouteTooLong; 246 item.view_url = try route.toUrl(arena); 247 }, 248 .discuss => { 249 const record = (try readRecord(evt.Discussion, hash_kind, arena, haxy_moment, id)) orelse return null; 250 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 251 if (!include_view_url) return item; 252 const route = ui.RoutablePage.repoThreadCommentsRoute(.discuss, identity, item.id, 0) orelse return error.RouteTooLong; 253 item.view_url = try route.toUrl(arena); 254 }, 255 .comment => { 256 const record = (try readRecord(evt.Comment, hash_kind, arena, haxy_moment, id)) orelse return null; 257 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 258 if (!include_view_url) return item; 259 var thread_id: [evt.event_id_size]u8 = undefined; 260 _ = std.fmt.hexToBytes(&thread_id, &record.event.thread_id) catch return error.InvalidEventId; 261 const thread_kind = (try evt.readEventKind(hash_kind, kind_map, &thread_id)) orelse return item; 262 const route = ui.RoutablePage.repoThreadCommentRoute(thread_kind, identity, &record.event.thread_id, item.id, 0) orelse return item; 263 item.view_url = try route.toUrl(arena); 264 }, 265 .attach => { 266 const record = (try readRecord(evt.Attachment, hash_kind, arena, haxy_moment, id)) orelse return null; 267 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 268 if (!include_view_url) return item; 269 var parent_id: [evt.event_id_size]u8 = undefined; 270 _ = std.fmt.hexToBytes(&parent_id, &record.event.parent_id) catch return error.InvalidEventId; 271 const parent_kind = (try evt.readEventKind(hash_kind, kind_map, &parent_id)) orelse return item; 272 const route = ui.RoutablePage.repoThreadCommentsRoute(parent_kind, identity, &record.event.parent_id, 0) orelse return item; 273 item.view_url = try route.toUrl(arena); 274 }, 275 .patch => { 276 const record = (try readRecord(evt.Patch, hash_kind, arena, haxy_moment, id)) orelse return null; 277 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 278 if (!include_view_url) return item; 279 const route = ui.RoutablePage.repoThreadCommentsRoute(.patch, identity, item.id, 0) orelse return error.RouteTooLong; 280 item.view_url = try route.toUrl(arena); 281 }, 282 .patchrev => { 283 const record = (try readRecord(evt.PatchRev, hash_kind, arena, haxy_moment, id)) orelse return null; 284 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 285 }, 286 } 287 return item; 288 } 289 290 pub const View = struct { 291 box: wgt.Box(ui.Widget), 292 data: *const Self, 293 session: *ui.Session, 294 detailed_index: [2]?usize, 295 296 const header_index = 0; 297 const content_index = 1; 298 const list_index = 0; 299 const detail_index = 1; 300 const list_max_width = 20; 301 const detail_min_width = 40; 302 303 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 304 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 305 errdefer outer.deinit(allocator); 306 307 var header_view = try Header.View.init(allocator, data, session); 308 errdefer header_view.deinit(allocator); 309 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_events_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 310 311 var content_stack = try wgt.Stack(ui.Widget).init(allocator); 312 errdefer content_stack.deinit(allocator); 313 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 314 var content_box = try initContent(allocator, data, session, view); 315 errdefer content_box.deinit(allocator); 316 try content_stack.children.put(allocator, content_box.getFocus().id, .{ .box = content_box }); 317 } 318 content_stack.getFocus().child_id = content_stack.children.keys()[@intFromEnum(data.view)]; 319 try outer.children.put(allocator, content_stack.getFocus().id, .{ .widget = .{ .stack = content_stack }, .rect = null, .min_size = null }); 320 outer.getFocus().child_id = outer.children.keys()[header_index]; 321 return .{ .box = outer, .data = data, .session = session, .detailed_index = .{ null, null } }; 322 } 323 324 fn initContent(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, view: ui.RoutablePage.EventsView) !wgt.Box(ui.Widget) { 325 var content_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 326 errdefer content_box.deinit(allocator); 327 328 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 329 errdefer list_box.deinit(allocator); 330 const event_window = data.window(view); 331 for (event_window.events) |event| { 332 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, event.kind, event.id) orelse return error.RouteTooLong; 333 const link = try std.fmt.allocPrint(session.page_arena.allocator(), "ai:{s}", .{try route.toUrl(session.page_arena)}); 334 try addRow(allocator, &list_box, @tagName(event.kind), link); 335 } 336 if (event_window.next) |next| { 337 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, next.kind, next.id) orelse return error.RouteTooLong; 338 const link = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}); 339 try addRow(allocator, &list_box, "next →", link); 340 } 341 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 342 var list_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 343 errdefer list_scroll.deinit(allocator); 344 try content_box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 345 346 var detail_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 347 errdefer detail_box.deinit(allocator); 348 var detail_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 349 errdefer detail_scroll.deinit(allocator); 350 try content_box.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 351 352 content_box.getFocus().child_id = content_box.children.keys()[list_index]; 353 return content_box; 354 } 355 356 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 357 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 358 errdefer row.deinit(allocator); 359 row.getFocus().focusable = true; 360 row.getFocus().kind = .{ .custom = link }; 361 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 362 } 363 364 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 365 self.box.deinit(allocator); 366 } 367 368 fn header(self: *View) *Header.View { 369 return &self.box.children.values()[header_index].widget.repo_events_header; 370 } 371 372 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 373 return &self.box.children.values()[content_index].widget.stack; 374 } 375 376 fn selectedView(self: *View) ui.RoutablePage.EventsView { 377 const stack = self.contentStack(); 378 const id = stack.getFocus().child_id orelse return .active; 379 return @enumFromInt(stack.children.getIndex(id) orelse 0); 380 } 381 382 fn eventWindow(self: *View) *const Window { 383 return self.data.window(self.selectedView()); 384 } 385 386 fn content(self: *View) *wgt.Box(ui.Widget) { 387 const stack = self.contentStack(); 388 return &stack.children.values()[@intFromEnum(self.selectedView())].box; 389 } 390 391 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 392 return &self.content().children.values()[list_index].widget.scroll; 393 } 394 395 fn listBox(self: *View) *wgt.Box(ui.Widget) { 396 return &self.listScroll().child.box; 397 } 398 399 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 400 return &self.content().children.values()[detail_index].widget.scroll; 401 } 402 403 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 404 return &self.detailScroll().child.box; 405 } 406 407 fn detailActive(self: *View) bool { 408 return self.content().getFocus().child_id == self.detailScroll().getFocus().id; 409 } 410 411 fn headerActive(self: *View) bool { 412 return self.box.getFocus().child_id == self.header().getFocus().id; 413 } 414 415 fn selectedEventIndex(self: *View) ?usize { 416 const id = self.listBox().getFocus().child_id orelse return null; 417 const index = self.listBox().children.getIndex(id) orelse return null; 418 return if (index < self.eventWindow().events.len) index else null; 419 } 420 421 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 422 self.clearGrid(); 423 if (self.header().getSelectedIndex()) |index| 424 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 425 const view = self.selectedView(); 426 var selected_kind: ?evt.EventKind = null; 427 var selected_id: []const u8 = ""; 428 if (view == self.data.view) { 429 if (self.data.selected) |selected| { 430 selected_kind = selected.kind; 431 selected_id = selected.id; 432 } 433 } 434 if (ui.RoutablePage.repoEventsRoute(self.data.identity, view, selected_kind, selected_id)) |route| 435 self.session.data.current_page = route; 436 try self.refreshDetail(allocator); 437 438 if (root_focus.grandchild_id) |id| if (self.box.getFocus().children.contains(id)) { 439 if (self.selectedEventIndex()) |index| { 440 const event = self.eventWindow().events[index]; 441 if (ui.RoutablePage.repoEventsRoute(self.data.identity, view, event.kind, event.id)) |route| 442 self.session.data.current_page = route; 443 } 444 }; 445 446 const list = self.listBox(); 447 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 448 .text_box => |*text_box| text_box.options.border_style = if (list.getFocus().child_id == id) .single else .hidden, 449 else => {}, 450 }; 451 452 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 453 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 454 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 455 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 456 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 457 458 try self.box.build(allocator, constraint, root_focus); 459 } 460 461 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 462 const index = self.selectedEventIndex() orelse return; 463 const detailed_index = &self.detailed_index[@intFromEnum(self.selectedView())]; 464 if (detailed_index.*) |old| if (old == index) return; 465 const box = self.detailBox(); 466 for (box.children.values()) |*child| child.widget.deinit(allocator); 467 box.children.clearAndFree(allocator); 468 box.getFocus().child_id = null; 469 470 const item = self.eventWindow().events[index]; 471 if (item.view_url) |url| { 472 var link = try wgt.TextBox(ui.Widget).init(allocator, "view event", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 473 errdefer link.deinit(allocator); 474 link.getFocus().focusable = true; 475 link.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{url}) }; 476 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 477 } 478 { 479 var kind = try wgt.TextBox(ui.Widget).init(allocator, @tagName(item.kind), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " kind " }); 480 errdefer kind.deinit(allocator); 481 kind.getFocus().focusable = true; 482 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 483 } 484 { 485 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 486 errdefer author.deinit(allocator); 487 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 488 } 489 490 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 491 self.detailScroll().x = 0; 492 self.detailScroll().y = 0; 493 self.detailScroll().getFocus().version +%= 1; 494 detailed_index.* = index; 495 } 496 497 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 498 if (self.headerActive()) { 499 if (key == .arrow_down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 500 return; 501 } 502 if (key == .arrow_up and self.contentAtTop()) { 503 self.header().focusCurrent(root_focus); 504 return; 505 } 506 if (self.detailActive()) { 507 if (key == .arrow_left) { 508 root_focus.setFocus(self.listScroll().getFocus().id); 509 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 510 ui.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 511 } 512 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 513 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 514 } else switch (key) { 515 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 516 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 517 else => {}, 518 } 519 } 520 521 fn contentAtTop(self: *View) bool { 522 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 523 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 524 const id = box.getFocus().child_id orelse return true; 525 return box.children.getIndex(id) == 0 and scroll.y == 0; 526 } 527 528 pub fn getSelectedIndex(self: *View) ?usize { 529 return if (self.headerActive()) 0 else 1; 530 } 531 532 pub fn clearGrid(self: *View) void { 533 self.box.clearGrid(); 534 } 535 536 pub fn getGrid(self: View) ?Grid { 537 return self.box.getGrid(); 538 } 539 540 pub fn getFocus(self: *View) *Focus { 541 return self.box.getFocus(); 542 } 543 }; 544 545 pub const Header = struct { 546 sync_status: ?[]const u8, 547 548 const nothing = "nothing to sync"; 549 const unsynced = "sync needed!"; 550 551 const Progress = struct { 552 message: *std.Io.Writer.Allocating, 553 554 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 555 switch (event) { 556 .text => |value| { 557 self.message.clearRetainingCapacity(); 558 try self.message.writer.writeAll(value); 559 }, 560 else => {}, 561 } 562 } 563 }; 564 565 pub fn local( 566 comptime repo_kind: rp.RepoKind, 567 comptime repo_opts: rp.RepoOpts(repo_kind), 568 aa: std.mem.Allocator, 569 repo: *rp.Repo(repo_kind, repo_opts), 570 io: std.Io, 571 failure: ?[]const u8, 572 ) !Header { 573 if (failure != null) return Header.empty(aa, failure); 574 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 575 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 576 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 577 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 578 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 579 } 580 581 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 582 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 583 } 584 585 pub fn remote() Header { 586 return .{ .sync_status = null }; 587 } 588 589 pub const View = struct { 590 box: wgt.Box(ui.Widget), 591 session: *ui.Session, 592 tab_ids: [2]usize, 593 button_id: ?usize, 594 595 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 596 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 597 errdefer box.deinit(allocator); 598 var tab_ids: [2]usize = undefined; 599 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, 0..) |view, i| { 600 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "") orelse return error.RouteTooLong; 601 const count = data.window(view).count; 602 var label_buf: [64]u8 = undefined; 603 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(view), count }); 604 var tab = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 605 errdefer tab.deinit(allocator); 606 tab.getFocus().focusable = true; 607 tab.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "ai:{s}", .{try route.toUrl(session.page_arena)}) }; 608 tab_ids[i] = tab.getFocus().id; 609 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 610 } 611 612 var button_id: ?usize = null; 613 if (data.header.sync_status != null) { 614 box.getFocus().kind = .{ .custom = "form:/sync" }; 615 var button = try wgt.TextBox(ui.Widget).init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 616 errdefer button.deinit(allocator); 617 button.getFocus().focusable = true; 618 button.getFocus().kind = .{ .custom = "submit" }; 619 button_id = button.getFocus().id; 620 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 621 } 622 623 if (data.header.sync_status) |status_text| { 624 var status = try wgt.TextBox(ui.Widget).init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 625 errdefer status.deinit(allocator); 626 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 627 } 628 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 629 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 630 } 631 632 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 633 self.box.deinit(allocator); 634 } 635 636 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 637 self.clearGrid(); 638 for (self.tab_ids) |id| { 639 const child = self.box.children.getPtr(id) orelse continue; 640 const tab = switch (child.widget) { 641 .text_box => |*text_box| text_box, 642 else => continue, 643 }; 644 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 645 } 646 try self.box.build(allocator, constraint, root_focus); 647 } 648 649 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 650 const selected = self.box.getFocus().child_id orelse return; 651 const current = self.box.children.getIndex(selected) orelse return; 652 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 653 if (inp.moveTab(key, current, count)) |next| { 654 root_focus.setFocus(self.box.children.keys()[next]); 655 return; 656 } 657 switch (key) { 658 .enter => if (self.syncFocused()) try self.requestSync(allocator), 659 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 660 else => {}, 661 } 662 } 663 664 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 665 if (!self.session.is_terminal) return; 666 if (self.session.host_request != null) return; 667 const button_id = self.button_id orelse return; 668 const child = self.box.children.getPtr(button_id) orelse return; 669 const button = switch (child.widget) { 670 .text_box => |*button| button, 671 else => return, 672 }; 673 try button.setContent(allocator, "syncing..."); 674 self.session.host_request = .sync_events; 675 } 676 677 fn syncFocused(self: *Header.View) bool { 678 const button_id = self.button_id orelse return false; 679 return self.box.getFocus().child_id == button_id; 680 } 681 682 pub fn getSelectedIndex(self: *Header.View) ?usize { 683 const id = self.box.getFocus().child_id orelse return null; 684 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 685 } 686 687 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 688 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 689 } 690 691 pub fn clearGrid(self: *Header.View) void { 692 self.box.clearGrid(); 693 } 694 695 pub fn getGrid(self: Header.View) ?Grid { 696 return self.box.getGrid(); 697 } 698 699 pub fn getFocus(self: *Header.View) *Focus { 700 return self.box.getFocus(); 701 } 702 }; 703 }; 704 705 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 706 if (comptime builtin.target.cpu.arch == .wasm32) return; 707 const io = session.io orelse return; 708 const source = session.local orelse return; 709 defer session.refresh_requested = true; 710 const failure = sync(io, allocator, source) catch |err| { 711 session.data.sync_failure = @errorName(err); 712 return; 713 }; 714 if (failure) |message| { 715 defer allocator.free(message); 716 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 717 return; 718 } 719 session.data.sync_failure = null; 720 switch (session.data.current_page) { 721 .repo_events => |route| { 722 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "")) |first| 723 session.data.current_page = first; 724 }, 725 else => {}, 726 } 727 } 728 729 fn remoteName( 730 comptime repo_kind: rp.RepoKind, 731 comptime repo_opts: rp.RepoOpts(repo_kind), 732 repo: *rp.Repo(repo_kind, repo_opts), 733 io: std.Io, 734 allocator: std.mem.Allocator, 735 ) !?[]u8 { 736 var remotes = try repo.listRemotes(io, allocator); 737 defer remotes.deinit(); 738 const names = remotes.sections.keys(); 739 if (names.len == 0) return null; 740 for (names) |name| { 741 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 742 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 743 } 744 return try allocator.dupe(u8, names[0]); 745 } 746 747 fn isLoopbackSshUrl(url: []const u8) bool { 748 const uri = std.Uri.parse(url) catch return false; 749 const host = switch (uri.host orelse return false) { 750 .raw => |value| value, 751 .percent_encoded => |value| value, 752 }; 753 return std.mem.eql(u8, host, "localhost") or 754 std.mem.eql(u8, host, "127.0.0.1") or 755 std.mem.eql(u8, host, "::1") or 756 std.mem.eql(u8, host, "[::1]"); 757 } 758 759 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 760 var message: std.Io.Writer.Allocating = .init(allocator); 761 defer message.deinit(); 762 763 switch (source.repo_kind) { 764 inline else => |repo_kind| { 765 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 766 defer any_repo.deinit(io, allocator); 767 switch (any_repo) { 768 inline else => |*repo| { 769 const repo_opts = repo.self_repo_opts; 770 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 771 defer allocator.free(remote_name); 772 773 var config = try repo.listConfig(io, allocator); 774 defer config.deinit(); 775 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 776 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 777 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 778 defer allocator.free(remote_section); 779 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 780 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 781 const push_url = remote_config.get("pushurl") orelse fetch_url; 782 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 783 const fetch_ssh_command = try std.fmt.allocPrint(allocator, "{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 784 defer allocator.free(fetch_ssh_command); 785 const push_ssh_command = try std.fmt.allocPrint(allocator, "{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 786 defer allocator.free(push_ssh_command); 787 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 788 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 789 790 const fetch_refspec = try std.fmt.allocPrint(allocator, "+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 791 defer allocator.free(fetch_refspec); 792 var fetch_opts = fetch_transport_opts; 793 fetch_opts.refspecs = &.{fetch_refspec}; 794 message.clearRetainingCapacity(); 795 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 796 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 797 return err; 798 }; 799 800 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 801 const local_oid = try repo.readRef(io, evt.events_ref); 802 const remote_oid = try repo.readRef(io, remote_ref); 803 if (local_oid == null and remote_oid == null) { 804 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 805 return null; 806 } 807 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 808 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 809 810 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 811 defer allocator.free(push_refspec); 812 message.clearRetainingCapacity(); 813 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 814 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 815 return err; 816 }; 817 message.clearRetainingCapacity(); 818 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 819 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 820 return err; 821 }; 822 }, 823 } 824 }, 825 } 826 return null; 827 } 828