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