Skip to content

Commit

Permalink
widgets(table): implemented header and column borders
Browse files Browse the repository at this point in the history
- Added the `header_border` and `col_border` to `TableContext`. Note, row borders don't work as well due to borders taking up an entire cell.
- Updated the `table.zig` example.
  • Loading branch information
00JCIV00 authored and rockorager committed Oct 5, 2024
1 parent 46ecd65 commit ac9839c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ pub fn main() !void {
var demo_tbl: vaxis.widgets.Table.TableContext = .{
.active_bg = active_bg,
.active_fg = .{ .rgb = .{ 0, 0, 0 } },
.row_bg_1 = .{ .rgb = .{ 8, 8, 8 } },
.selected_bg = selected_bg,
.header_names = .{ .custom = &.{ "First", "Last", "Username", "Phone#", "Email" } },
//.header_align = .left,
.col_indexes = .{ .by_idx = &.{ 0, 1, 2, 4, 3 } },
//.col_align = .{ .by_idx = &.{ .left, .left, .center, .center, .left } },
.col_align = .{ .all = .center },
//.col_align = .{ .all = .center },
//.header_borders = true,
//.col_borders = true,
//.col_width = .{ .static_all = 15 },
//.col_width = .{ .dynamic_header_len = 3 },
//.col_width = .{ .static_individual = &.{ 10, 20, 15, 25, 15 } },
Expand Down
10 changes: 10 additions & 0 deletions src/widgets/Table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ pub const TableContext = struct {
header_align: HorizontalAlignment = .center,
// Column Alignment
col_align: ColumnAlignment = .{ .all = .left },

// Header Borders
header_borders: bool = false,
// Row Borders
//row_borders: bool = false,
// Col Borders
col_borders: bool = false,
};

/// Width Styles for `col_width`.
Expand Down Expand Up @@ -233,6 +240,7 @@ pub fn drawTable(
.y_off = 0,
.width = .{ .limit = col_width },
.height = .{ .limit = 1 },
.border = .{ .where = if (table_ctx.header_borders and idx > 0) .left else .none }
});
var hdr = switch (table_ctx.header_align) {
.left => hdr_win,
Expand Down Expand Up @@ -292,6 +300,7 @@ pub fn drawTable(
.y_off = 1 + row + table_ctx.active_y_off,
.width = .{ .limit = table_win.width },
.height = .{ .limit = 1 },
//.border = .{ .where = if (table_ctx.row_borders) .top else .none },
});
if (table_ctx.start + row == table_ctx.row) {
table_ctx.active_y_off = if (table_ctx.active_content_fn) |content| try content(&row_win, table_ctx.active_ctx) else 0;
Expand Down Expand Up @@ -322,6 +331,7 @@ pub fn drawTable(
.y_off = 0,
.width = .{ .limit = col_width },
.height = .{ .limit = 1 },
.border = .{ .where = if (table_ctx.col_borders and col_idx > 0) .left else .none },
});
const item_txt = switch (ItemT) {
[]const u8 => item,
Expand Down

0 comments on commit ac9839c

Please sign in to comment.