Added the table.zig example

- Created a basic user management example for the Table widget.
This commit is contained in:
00JCIV00 2024-02-26 13:43:39 -05:00 committed by Tim Culverhouse
parent 09a8188851
commit f8544be262
2 changed files with 21 additions and 14 deletions

View file

@ -9,7 +9,11 @@ const vaxis = @import("vaxis");
const log = std.log.scoped(.main); const log = std.log.scoped(.main);
/// Active TUI Section /// Active TUI Section
<<<<<<< HEAD
const ActiveSection = enum { const ActiveSection = enum {
=======
const ActiveSection = enum{
>>>>>>> b015a5d (Added the `table.zig` example)
top, top,
mid, mid,
btm, btm,
@ -95,9 +99,10 @@ pub fn main() !void {
break :keyEvt; break :keyEvt;
} }
// Command State // Command State
if (active != .btm and if (
key.matchesAny(&.{ ':', '/', 'g', 'G' }, .{})) active != .btm and
{ key.matchesAny(&.{ ':', '/', 'g', 'G' }, .{})
) {
active = .btm; active = .btm;
for (0..cmd_input.buf.items.len) |_| _ = cmd_input.buf.orderedRemove(0); for (0..cmd_input.buf.items.len) |_| _ = cmd_input.buf.orderedRemove(0);
try cmd_input.update(.{ .key_press = key }); try cmd_input.update(.{ .key_press = key });
@ -127,9 +132,11 @@ pub fn main() !void {
// Run Command and Clear Command Bar // Run Command and Clear Command Bar
else if (key.matchExact(vaxis.Key.enter, .{})) { else if (key.matchExact(vaxis.Key.enter, .{})) {
const cmd = cmd_input.buf.items; const cmd = cmd_input.buf.items;
if (mem.eql(u8, ":q", cmd) or if (
mem.eql(u8, ":q", cmd) or
mem.eql(u8, ":quit", cmd) or mem.eql(u8, ":quit", cmd) or
mem.eql(u8, ":exit", cmd)) return; mem.eql(u8, ":exit", cmd)
) return;
if (mem.eql(u8, "G", cmd)) { if (mem.eql(u8, "G", cmd)) {
demo_tbl.row = user_list.items.len - 1; demo_tbl.row = user_list.items.len - 1;
active = .mid; active = .mid;
@ -141,7 +148,8 @@ pub fn main() !void {
} }
for (0..cmd_input.buf.items.len) |_| _ = cmd_input.buf.orderedRemove(0); for (0..cmd_input.buf.items.len) |_| _ = cmd_input.buf.orderedRemove(0);
cmd_input.cursor_idx = 0; cmd_input.cursor_idx = 0;
} else try cmd_input.update(.{ .key_press = key }); }
else try cmd_input.update(.{ .key_press = key });
}, },
} }
moving = false; moving = false;
@ -165,9 +173,9 @@ pub fn main() !void {
); );
for (title_segs[0..]) |*title_seg| for (title_segs[0..]) |*title_seg|
title_seg.*.style.bg = if (active == .top) selected_bg else other_bg; title_seg.*.style.bg = if (active == .top) selected_bg else other_bg;
top_bar.fill(.{ .style = .{ top_bar.fill(.{
.bg = if (active == .top) selected_bg else other_bg, .style = .{ .bg = if (active == .top) selected_bg else other_bg, }
} }); });
const logo_bar = vaxis.widgets.alignment.center( const logo_bar = vaxis.widgets.alignment.center(
top_bar, top_bar,
44, 44,
@ -200,7 +208,9 @@ pub fn main() !void {
.{ .limit = win.width }, .{ .limit = win.width },
.{ .limit = 1 }, .{ .limit = 1 },
); );
if (active == .btm) bottom_bar.fill(.{ .style = .{ .bg = selected_bg } }); if (active == .btm) bottom_bar.fill(.{
.style = .{ .bg = selected_bg }
});
cmd_input.draw(bottom_bar); cmd_input.draw(bottom_bar);
// Render the screen // Render the screen
@ -209,7 +219,7 @@ pub fn main() !void {
} }
/// User Struct /// User Struct
pub const User = struct { pub const User = struct{
first: []const u8, first: []const u8,
last: []const u8, last: []const u8,
user: []const u8, user: []const u8,

View file

@ -1,9 +1,6 @@
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;