this repo has no description
13
fork

Configure Feed

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

widgets(table): implemented header and column alignment

- Added the `header_align` and `col_align` to `TableContext` along with the corresponding `HorizontalAlignment` and `ColumnAlignment` Types.
- Updated the `table.zig` example.

authored by

00JCIV00 and committed by
Tim Culverhouse
46ecd65c ad12308a

+39 -3
+3 -1
examples/table.zig
··· 81 81 .active_fg = .{ .rgb = .{ 0, 0, 0 } }, 82 82 .selected_bg = selected_bg, 83 83 .header_names = .{ .custom = &.{ "First", "Last", "Username", "Phone#", "Email" } }, 84 - //.header_names = .{ .custom = &.{ "First", "Last", "Email", "Phone#" } }, 84 + //.header_align = .left, 85 85 .col_indexes = .{ .by_idx = &.{ 0, 1, 2, 4, 3 } }, 86 + //.col_align = .{ .by_idx = &.{ .left, .left, .center, .center, .left } }, 87 + .col_align = .{ .all = .center }, 86 88 //.col_width = .{ .static_all = 15 }, 87 89 //.col_width = .{ .dynamic_header_len = 3 }, 88 90 //.col_width = .{ .static_individual = &.{ 10, 20, 15, 25, 15 } },
+36 -2
src/widgets/Table.zig
··· 60 60 header_names: HeaderNames = .field_names, 61 61 // Column Indexes 62 62 col_indexes: ColumnIndexes = .all, 63 + // Header Alignment 64 + header_align: HorizontalAlignment = .center, 65 + // Column Alignment 66 + col_align: ColumnAlignment = .{ .all = .left }, 63 67 }; 64 68 65 69 /// Width Styles for `col_width`. ··· 88 92 field_names, 89 93 /// Custom 90 94 custom: []const []const u8, 95 + }; 96 + 97 + /// Horizontal Alignment 98 + pub const HorizontalAlignment = enum { 99 + left, 100 + center, 101 + }; 102 + /// Column Alignment 103 + pub const ColumnAlignment = union(enum) { 104 + all: HorizontalAlignment, 105 + by_idx: []const HorizontalAlignment, 91 106 }; 92 107 93 108 /// Draw a Table for the TUI. ··· 219 234 .width = .{ .limit = col_width }, 220 235 .height = .{ .limit = 1 }, 221 236 }); 222 - var hdr = vaxis.widgets.alignment.center(hdr_win, @min(col_width -| 1, hdr_txt.len +| 1), 1); 237 + var hdr = switch (table_ctx.header_align) { 238 + .left => hdr_win, 239 + .center => vaxis.widgets.alignment.center(hdr_win, @min(col_width -| 1, hdr_txt.len +| 1), 1), 240 + }; 223 241 hdr_win.fill(.{ .style = .{ .bg = hdr_bg } }); 224 242 var seg = [_]vaxis.Cell.Segment{.{ 225 243 .text = if (hdr_txt.len > col_width and alloc != null) try fmt.allocPrint(alloc.?, "{s}...", .{hdr_txt[0..(col_width -| 4)]}) else hdr_txt, ··· 280 298 } 281 299 col_start = 0; 282 300 const item_fields = meta.fields(DataT); 301 + var col_idx: usize = 0; 283 302 for (field_indexes) |f_idx| { 284 303 inline for (item_fields[0..], 0..) |item_field, item_idx| contFields: { 285 304 switch (table_ctx.col_indexes) { ··· 288 307 if (item_idx != f_idx) break :contFields; 289 308 }, 290 309 } 310 + defer col_idx += 1; 291 311 const col_width = try calcColWidth( 292 312 item_idx, 293 313 headers, ··· 331 351 }, 332 352 }; 333 353 item_win.fill(.{ .style = .{ .bg = row_bg } }); 354 + const item_align_win = itemAlignWin: { 355 + const col_align = switch (table_ctx.col_align) { 356 + .all => |all| all, 357 + .by_idx => |aligns| aligns[col_idx], 358 + }; 359 + break :itemAlignWin switch (col_align) { 360 + .left => item_win, 361 + .center => center: { 362 + const center = vaxis.widgets.alignment.center(item_win, @min(col_width -| 1, item_txt.len +| 1), 1); 363 + center.fill(.{ .style = .{ .bg = row_bg } }); 364 + break :center center; 365 + }, 366 + }; 367 + }; 334 368 var seg = [_]vaxis.Cell.Segment{.{ 335 369 .text = if (item_txt.len > col_width and alloc != null) try fmt.allocPrint(alloc.?, "{s}...", .{item_txt[0..(col_width -| 4)]}) else item_txt, 336 370 .style = .{ .fg = row_fg, .bg = row_bg }, 337 371 }}; 338 - _ = try item_win.print(seg[0..], .{ .wrap = .word, .col_offset = table_ctx.cell_x_off }); 372 + _ = try item_align_win.print(seg[0..], .{ .wrap = .word, .col_offset = table_ctx.cell_x_off }); 339 373 } 340 374 } 341 375 }