a modern tui library written in zig
17
fork

Configure Feed

Select the types of activity you want to include in your feed.

widgets(table): implemented more header and column options

- Added the `HeaderNames` Type and `header_names` field to `TableContext` to let users choose between automatically getting header names from fields or setting custom names.
- Removed the `headers` parameter from `drawTable()`.
- Added the `ColumnIndexes` Type and `col_indexes` field to `TableContext` to let users choose which columns/fields they actually want to display.

authored by

00JCIV00 and committed by rockorager.dev 500488f7 d1b4e3f0

+56 -4
+3 -1
examples/table.zig
··· 79 79 var demo_tbl: vaxis.widgets.Table.TableContext = .{ 80 80 .active_bg = active_bg, 81 81 .selected_bg = selected_bg, 82 + .header_names = .{ .custom = &.{ "First", "Last", "Username", "Email", "Phone#" } }, 83 + //.header_names = .{ .custom = &.{ "First", "Last", "Email", "Phone#" } }, 84 + //.col_indexes = .{ .by_idx = &.{ 0, 1, 3, 4 } }, 82 85 //.col_width = .{ .static_all = 15 }, 83 86 //.col_width = .{ .dynamic_header_len = 3 }, 84 87 //.col_width = .{ .static_individual = &.{ 10, 20, 15, 25, 15 } }, ··· 274 277 try vaxis.widgets.Table.drawTable( 275 278 event_alloc, 276 279 middle_bar, 277 - &.{ "First", "Last", "Username", "Email", "Phone#" }, 278 280 //users_buf[0..], 279 281 user_list, 280 282 //user_mal,
+53 -3
src/widgets/Table.zig
··· 49 49 /// Note, if this is left `null` the Column Width will be dynamically calculated during `drawTable()`. 50 50 //col_width: ?usize = null, 51 51 col_width: WidthStyle = .dynamic_fill, 52 + 53 + // Header Names 54 + header_names: HeaderNames = .field_names, 55 + // Column Indexes 56 + col_indexes: ColumnIndexes = .all, 52 57 }; 53 58 54 59 /// Width Styles for `col_width`. ··· 63 68 static_individual: []const usize, 64 69 }; 65 70 71 + /// Column Indexes 72 + pub const ColumnIndexes = union(enum) { 73 + /// Use all of the Columns. 74 + all, 75 + /// Use Columns from the specified indexes. 76 + by_idx: []const usize, 77 + }; 78 + 79 + /// Header Names 80 + pub const HeaderNames = union(enum) { 81 + /// Use Field Names as Headers 82 + field_names, 83 + /// Custom 84 + custom: []const []const u8, 85 + }; 86 + 66 87 /// Draw a Table for the TUI. 67 88 pub fn drawTable( 68 89 /// This should be an ArenaAllocator that can be deinitialized after each event call. ··· 73 94 alloc: ?mem.Allocator, 74 95 /// The parent Window to draw to. 75 96 win: vaxis.Window, 76 - /// Headers for the Table 77 - headers: []const []const u8, 78 97 /// This must be a Slice, ArrayList, or MultiArrayList. 79 98 /// Note, MultiArrayList support currently requires allocation. 80 99 data_list: anytype, ··· 131 150 }; 132 151 defer if (di_is_mal) alloc.?.free(data_items); 133 152 153 + // Headers for the Table 154 + var hdrs_buf: [100][]const u8 = undefined; 155 + const headers = hdrs: { 156 + switch (table_ctx.header_names) { 157 + .field_names => { 158 + const DataT = @TypeOf(data_items[0]); 159 + const fields = meta.fields(DataT); 160 + var num_hdrs: usize = 0; 161 + inline for (fields, 0..) |field, idx| contFields: { 162 + switch (table_ctx.col_indexes) { 163 + .all => {}, 164 + .by_idx => |idxs| { 165 + if (mem.indexOfScalar(usize, idxs, idx) == null) break :contFields; 166 + }, 167 + } 168 + num_hdrs += 1; 169 + hdrs_buf[idx] = field.name; 170 + } 171 + break :hdrs hdrs_buf[0..num_hdrs]; 172 + }, 173 + .custom => |hdrs| break :hdrs hdrs, 174 + } 175 + }; 176 + 177 + 134 178 const table_win = win.initChild( 135 179 0, 136 180 table_ctx.y_off, ··· 214 258 const DataT = @TypeOf(data); 215 259 col_start = 0; 216 260 const item_fields = meta.fields(DataT); 217 - inline for (item_fields[0..], 0..) |item_field, item_idx| { 261 + inline for (item_fields[0..], 0..) |item_field, item_idx| contFields: { 262 + switch (table_ctx.col_indexes) { 263 + .all => {}, 264 + .by_idx => |idxs| { 265 + if (mem.indexOfScalar(usize, idxs, item_idx) == null) break :contFields; 266 + }, 267 + } 218 268 const col_width = try calcColWidth( 219 269 item_idx, 220 270 headers,