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