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