╭ Users.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const hash = xit.hash; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const inp = xitui.input; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 13 users: []const evt.User, 14 15 const Self = @This(); 16 17 pub fn init( 18 arena: *std.heap.ArenaAllocator, 19 haxy_moment: evt.AdminDB.HashMap(.read_only), 20 ) !Self { 21 const DB = evt.AdminDB; 22 23 var users: std.ArrayList(evt.User) = .empty; 24 25 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "event-id->user")) orelse return error.NotFound; 26 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 27 28 var users_iter = try event_id_to_user.iterator(); 29 while (try users_iter.next()) |kv_cursor| { 30 const kv = try kv_cursor.readKeyValuePair(); 31 const user_map = try DB.HashMap(.read_only).init(kv.value_cursor); 32 const user_event = try evt.read(evt.User, DB, evt.admin_repo_opts.hash, arena, user_map); 33 try users.append(arena.allocator(), user_event); 34 } 35 36 return .{ 37 .users = users.items, 38 }; 39 } 40 41 pub const View = struct { 42 list: ui.FlowBox.Scroll, 43 data: *const Self, 44 45 pub fn init(allocator: std.mem.Allocator, data: *const Self) !View { 46 var self = blk: { 47 var list = try ui.FlowBox.Scroll.init(allocator, .{ .cell_height = 1 }); 48 errdefer list.deinit(allocator); 49 50 break :blk View{ 51 .list = list, 52 .data = data, 53 }; 54 }; 55 errdefer self.deinit(allocator); 56 57 var arena = std.heap.ArenaAllocator.init(allocator); 58 defer arena.deinit(); 59 const aa = arena.allocator(); 60 61 const lines = try aa.alloc([]const u8, data.users.len); 62 for (data.users, 0..) |user, i| { 63 lines[i] = try std.fmt.allocPrint(aa, "{s} ({s})", .{ user.name, user.display_name }); 64 } 65 try self.list.setItems(allocator, lines); 66 67 return self; 68 } 69 70 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 71 self.list.deinit(allocator); 72 } 73 74 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 75 try self.list.build(allocator, constraint, root_focus); 76 } 77 78 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 79 try self.list.input(allocator, key, root_focus); 80 } 81 82 pub fn clearGrid(self: *View) void { 83 self.list.clearGrid(); 84 } 85 86 pub fn getGrid(self: View) ?Grid { 87 return self.list.getGrid(); 88 } 89 90 pub fn getFocus(self: *View) *Focus { 91 return self.list.getFocus(); 92 } 93 94 pub fn getSelectedIndex(self: View) ?usize { 95 return self.list.getSelectedIndex(); 96 } 97 }; 98