Added the new Table Widget

- Added `Table.zig` under the `src/widgets` directory and `widgets.zig` module.
- Created the `drawTable()` function to draw a Table to the provided parent Window based on the provided ArrayList.
- Created the `TableContext` struct to manage state and attributes for the `drawTable()` function.
This commit is contained in:
00JCIV00 2024-02-26 11:08:14 -05:00 committed by Tim Culverhouse
parent 392dffb307
commit 09a8188851

View file

@ -1,6 +1,9 @@
const std = @import("std"); const std = @import("std");
const ascii = std.ascii;
const fmt = std.fmt; const fmt = std.fmt;
const fs = std.fs;
const heap = std.heap; const heap = std.heap;
const log = std.log;
const mem = std.mem; const mem = std.mem;
const meta = std.meta; const meta = std.meta;
@ -61,7 +64,9 @@ pub fn drawTable(
if (table_ctx.col > headers.len - 1) table_ctx.*.col = headers.len - 1; if (table_ctx.col > headers.len - 1) table_ctx.*.col = headers.len - 1;
for (headers[0..], 0..) |hdr_txt, idx| { for (headers[0..], 0..) |hdr_txt, idx| {
const hdr_bg = const hdr_bg =
if (table_ctx.active and idx == table_ctx.col) table_ctx.selected_bg else if (idx % 2 == 0) table_ctx.hdr_bg_1 else table_ctx.hdr_bg_2; if (table_ctx.active and idx == table_ctx.col) table_ctx.selected_bg
else if (idx % 2 == 0) table_ctx.hdr_bg_1
else table_ctx.hdr_bg_2;
const hdr_win = table_win.initChild( const hdr_win = table_win.initChild(
idx * item_width, idx * item_width,
0, 0,
@ -99,7 +104,9 @@ pub fn drawTable(
if (end > data_list.items.len) end = data_list.items.len; if (end > data_list.items.len) end = data_list.items.len;
for (data_list.items[table_ctx.start..end], 0..) |data, idx| { for (data_list.items[table_ctx.start..end], 0..) |data, idx| {
const row_bg = const row_bg =
if (table_ctx.active and table_ctx.start + idx == table_ctx.row) table_ctx.selected_bg else if (idx % 2 == 0) table_ctx.row_bg_1 else table_ctx.row_bg_2; if (table_ctx.active and table_ctx.start + idx == table_ctx.row) table_ctx.selected_bg
else if (idx % 2 == 0) table_ctx.row_bg_1
else table_ctx.row_bg_2;
const row_win = table_win.initChild( const row_win = table_win.initChild(
0, 0,
@ -129,13 +136,17 @@ pub fn drawTable(
switch(@typeInfo(ItemT).Optional.child) { switch(@typeInfo(ItemT).Optional.child) {
[]const u8 => break :nonStr opt_item, []const u8 => break :nonStr opt_item,
else => { else => {
break :nonStr if (alloc) |_alloc| try fmt.allocPrint(_alloc, "{any}", .{opt_item}) else fmt.comptimePrint("[unsupported ({s})]", .{@typeName(DataT)}); break :nonStr
}, if (alloc) |_alloc| try fmt.allocPrint(_alloc, "{any}", .{ opt_item })
else fmt.comptimePrint("[unsupported ({s})]", .{ @typeName(DataT) });
}
} }
}, },
else => { else => {
break :nonStr if (alloc) |_alloc| try fmt.allocPrint(_alloc, "{any}", .{item}) else fmt.comptimePrint("[unsupported ({s})]", .{@typeName(DataT)}); break :nonStr
}, if (alloc) |_alloc| try fmt.allocPrint(_alloc, "{any}", .{ item })
else fmt.comptimePrint("[unsupported ({s})]", .{ @typeName(DataT) });
}
} }
}, },
}, },