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