2024-02-26 19:43:39 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const fmt = std.fmt;
|
|
|
|
const heap = std.heap;
|
|
|
|
const mem = std.mem;
|
|
|
|
const meta = std.meta;
|
|
|
|
|
|
|
|
const vaxis = @import("vaxis");
|
|
|
|
|
|
|
|
const log = std.log.scoped(.main);
|
|
|
|
|
2024-05-22 21:06:26 +02:00
|
|
|
const ActiveSection = enum {
|
2024-02-26 19:43:39 +01:00
|
|
|
top,
|
|
|
|
mid,
|
|
|
|
btm,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
var gpa = heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
defer if (gpa.detectLeaks()) log.err("Memory leak detected!", .{});
|
|
|
|
const alloc = gpa.allocator();
|
|
|
|
|
|
|
|
// Users set up below the main function
|
|
|
|
const users_buf = try alloc.dupe(User, users[0..]);
|
|
|
|
const user_list = std.ArrayList(User).fromOwnedSlice(alloc, users_buf);
|
|
|
|
defer user_list.deinit();
|
|
|
|
|
2024-05-29 20:13:54 +02:00
|
|
|
var tty = try vaxis.Tty.init();
|
2024-07-04 17:27:50 +02:00
|
|
|
defer tty.deinit();
|
2024-05-29 20:13:54 +02:00
|
|
|
|
2024-04-30 00:03:54 +02:00
|
|
|
var vx = try vaxis.init(alloc, .{});
|
2024-05-29 20:13:54 +02:00
|
|
|
defer vx.deinit(alloc, tty.anyWriter());
|
2024-02-26 19:43:39 +01:00
|
|
|
|
2024-04-29 19:35:02 +02:00
|
|
|
var loop: vaxis.Loop(union(enum) {
|
|
|
|
key_press: vaxis.Key,
|
|
|
|
winsize: vaxis.Winsize,
|
2024-05-29 20:13:54 +02:00
|
|
|
}) = .{ .tty = &tty, .vaxis = &vx };
|
2024-04-29 19:35:02 +02:00
|
|
|
|
2024-05-29 20:13:54 +02:00
|
|
|
try loop.start();
|
2024-04-29 19:35:02 +02:00
|
|
|
defer loop.stop();
|
2024-05-29 20:13:54 +02:00
|
|
|
try vx.enterAltScreen(tty.anyWriter());
|
|
|
|
try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
|
2024-02-26 19:43:39 +01:00
|
|
|
|
|
|
|
const logo =
|
|
|
|
\\░█░█░█▀█░█░█░▀█▀░█▀▀░░░▀█▀░█▀█░█▀▄░█░░░█▀▀░
|
|
|
|
\\░▀▄▀░█▀█░▄▀▄░░█░░▀▀█░░░░█░░█▀█░█▀▄░█░░░█▀▀░
|
|
|
|
\\░░▀░░▀░▀░▀░▀░▀▀▀░▀▀▀░░░░▀░░▀░▀░▀▀░░▀▀▀░▀▀▀░
|
|
|
|
;
|
|
|
|
const title_logo = vaxis.Cell.Segment{
|
|
|
|
.text = logo,
|
|
|
|
.style = .{},
|
|
|
|
};
|
|
|
|
const title_info = vaxis.Cell.Segment{
|
|
|
|
.text = "===A Demo of the the Vaxis Table Widget!===",
|
|
|
|
.style = .{},
|
|
|
|
};
|
|
|
|
const title_disclaimer = vaxis.Cell.Segment{
|
|
|
|
.text = "(All data is non-sensical & LLM generated.)",
|
|
|
|
.style = .{},
|
|
|
|
};
|
|
|
|
var title_segs = [_]vaxis.Cell.Segment{ title_logo, title_info, title_disclaimer };
|
|
|
|
|
2024-04-30 00:03:54 +02:00
|
|
|
var cmd_input = vaxis.widgets.TextInput.init(alloc, &vx.unicode);
|
2024-02-26 19:43:39 +01:00
|
|
|
defer cmd_input.deinit();
|
|
|
|
|
|
|
|
// Colors
|
|
|
|
const selected_bg: vaxis.Cell.Color = .{ .rgb = .{ 64, 128, 255 } };
|
|
|
|
const other_bg: vaxis.Cell.Color = .{ .rgb = .{ 32, 32, 48 } };
|
|
|
|
|
|
|
|
// Table Context
|
|
|
|
var demo_tbl: vaxis.widgets.Table.TableContext = .{ .selected_bg = selected_bg };
|
|
|
|
|
|
|
|
// TUI State
|
|
|
|
var active: ActiveSection = .mid;
|
|
|
|
var moving = false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Create an Arena Allocator for easy allocations on each Event.
|
|
|
|
var event_arena = heap.ArenaAllocator.init(alloc);
|
|
|
|
defer event_arena.deinit();
|
|
|
|
const event_alloc = event_arena.allocator();
|
2024-04-29 19:35:02 +02:00
|
|
|
const event = loop.nextEvent();
|
2024-02-26 19:43:39 +01:00
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
.key_press => |key| keyEvt: {
|
|
|
|
// Close the Program
|
|
|
|
if (key.matches('c', .{ .ctrl = true })) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Refresh the Screen
|
|
|
|
if (key.matches('l', .{ .ctrl = true })) {
|
|
|
|
vx.queueRefresh();
|
|
|
|
break :keyEvt;
|
|
|
|
}
|
|
|
|
// Enter Moving State
|
|
|
|
if (key.matches('w', .{ .ctrl = true })) {
|
|
|
|
moving = !moving;
|
|
|
|
break :keyEvt;
|
|
|
|
}
|
|
|
|
// Command State
|
2024-05-22 21:06:26 +02:00
|
|
|
if (active != .btm and
|
|
|
|
key.matchesAny(&.{ ':', '/', 'g', 'G' }, .{}))
|
|
|
|
{
|
2024-02-26 19:43:39 +01:00
|
|
|
active = .btm;
|
|
|
|
for (0..cmd_input.buf.items.len) |_| _ = cmd_input.buf.orderedRemove(0);
|
|
|
|
try cmd_input.update(.{ .key_press = key });
|
|
|
|
cmd_input.cursor_idx = 1;
|
|
|
|
break :keyEvt;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (active) {
|
|
|
|
.top => {
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.down, 'j' }, .{}) and moving) active = .mid;
|
|
|
|
},
|
|
|
|
.mid => midEvt: {
|
|
|
|
if (moving) {
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.up, 'k' }, .{})) active = .top;
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.down, 'j' }, .{})) active = .btm;
|
|
|
|
break :midEvt;
|
|
|
|
}
|
|
|
|
// Change Row
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.up, 'k' }, .{})) demo_tbl.row -|= 1;
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.down, 'j' }, .{})) demo_tbl.row +|= 1;
|
|
|
|
// Change Column
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.left, 'h' }, .{})) demo_tbl.col -|= 1;
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.right, 'l' }, .{})) demo_tbl.col +|= 1;
|
|
|
|
},
|
|
|
|
.btm => {
|
|
|
|
if (key.matchesAny(&.{ vaxis.Key.up, 'k' }, .{}) and moving) active = .mid
|
|
|
|
// Run Command and Clear Command Bar
|
|
|
|
else if (key.matchExact(vaxis.Key.enter, .{})) {
|
|
|
|
const cmd = cmd_input.buf.items;
|
2024-05-22 21:06:26 +02:00
|
|
|
if (mem.eql(u8, ":q", cmd) or
|
2024-02-26 19:43:39 +01:00
|
|
|
mem.eql(u8, ":quit", cmd) or
|
2024-05-22 21:06:26 +02:00
|
|
|
mem.eql(u8, ":exit", cmd)) return;
|
2024-02-26 19:43:39 +01:00
|
|
|
if (mem.eql(u8, "G", cmd)) {
|
|
|
|
demo_tbl.row = user_list.items.len - 1;
|
|
|
|
active = .mid;
|
|
|
|
}
|
|
|
|
if (cmd.len >= 2 and mem.eql(u8, "gg", cmd[0..2])) {
|
|
|
|
const goto_row = fmt.parseInt(usize, cmd[2..], 0) catch 0;
|
|
|
|
demo_tbl.row = goto_row;
|
|
|
|
active = .mid;
|
|
|
|
}
|
|
|
|
for (0..cmd_input.buf.items.len) |_| _ = cmd_input.buf.orderedRemove(0);
|
|
|
|
cmd_input.cursor_idx = 0;
|
2024-05-22 21:06:26 +02:00
|
|
|
} else try cmd_input.update(.{ .key_press = key });
|
2024-02-26 19:43:39 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
moving = false;
|
|
|
|
},
|
2024-05-29 20:13:54 +02:00
|
|
|
.winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws),
|
2024-02-26 19:43:39 +01:00
|
|
|
//else => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sections
|
|
|
|
// - Window
|
|
|
|
const win = vx.window();
|
|
|
|
win.clear();
|
|
|
|
|
|
|
|
// - Top
|
|
|
|
const top_div = 6;
|
|
|
|
const top_bar = win.initChild(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
.{ .limit = win.width },
|
|
|
|
.{ .limit = win.height / top_div },
|
|
|
|
);
|
|
|
|
for (title_segs[0..]) |*title_seg|
|
|
|
|
title_seg.*.style.bg = if (active == .top) selected_bg else other_bg;
|
2024-05-22 21:06:26 +02:00
|
|
|
top_bar.fill(.{ .style = .{
|
|
|
|
.bg = if (active == .top) selected_bg else other_bg,
|
|
|
|
} });
|
2024-02-26 19:43:39 +01:00
|
|
|
const logo_bar = vaxis.widgets.alignment.center(
|
|
|
|
top_bar,
|
|
|
|
44,
|
|
|
|
top_bar.height - (top_bar.height / 3),
|
|
|
|
);
|
2024-05-22 21:06:26 +02:00
|
|
|
_ = try logo_bar.print(title_segs[0..], .{ .wrap = .word });
|
2024-02-26 19:43:39 +01:00
|
|
|
|
|
|
|
// - Middle
|
|
|
|
const middle_bar = win.initChild(
|
|
|
|
0,
|
|
|
|
win.height / top_div,
|
|
|
|
.{ .limit = win.width },
|
|
|
|
.{ .limit = win.height - (top_bar.height + 1) },
|
|
|
|
);
|
|
|
|
if (user_list.items.len > 0) {
|
|
|
|
demo_tbl.active = active == .mid;
|
|
|
|
try vaxis.widgets.Table.drawTable(
|
|
|
|
event_alloc,
|
|
|
|
middle_bar,
|
|
|
|
&.{ "First", "Last", "Username", "Email", "Phone#" },
|
|
|
|
user_list,
|
|
|
|
&demo_tbl,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// - Bottom
|
|
|
|
const bottom_bar = win.initChild(
|
|
|
|
0,
|
|
|
|
win.height - 1,
|
|
|
|
.{ .limit = win.width },
|
|
|
|
.{ .limit = 1 },
|
|
|
|
);
|
2024-05-22 21:06:26 +02:00
|
|
|
if (active == .btm) bottom_bar.fill(.{ .style = .{ .bg = selected_bg } });
|
2024-02-26 19:43:39 +01:00
|
|
|
cmd_input.draw(bottom_bar);
|
|
|
|
|
|
|
|
// Render the screen
|
2024-05-29 20:13:54 +02:00
|
|
|
try vx.render(tty.anyWriter());
|
2024-02-26 19:43:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// User Struct
|
2024-05-22 21:06:26 +02:00
|
|
|
pub const User = struct {
|
2024-02-26 19:43:39 +01:00
|
|
|
first: []const u8,
|
|
|
|
last: []const u8,
|
|
|
|
user: []const u8,
|
|
|
|
email: ?[]const u8 = null,
|
|
|
|
phone: ?[]const u8 = null,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Users Array
|
|
|
|
const users = [_]User{
|
|
|
|
.{ .first = "Nancy", .last = "Dudley", .user = "angela73", .email = "brian47@rodriguez.biz", .phone = null },
|
|
|
|
.{ .first = "Emily", .last = "Thornton", .user = "mrogers", .email = null, .phone = "(558)888-8604x094" },
|
|
|
|
.{ .first = "Kyle", .last = "Huff", .user = "xsmith", .email = null, .phone = "301.127.0801x12398" },
|
|
|
|
.{ .first = "Christine", .last = "Dodson", .user = "amandabradley", .email = "cheryl21@sullivan.com", .phone = null },
|
|
|
|
.{ .first = "Nathaniel", .last = "Kennedy", .user = "nrobinson", .email = null, .phone = null },
|
|
|
|
.{ .first = "Laura", .last = "Leon", .user = "dawnjones", .email = "fjenkins@patel.com", .phone = "1833013180" },
|
|
|
|
.{ .first = "Patrick", .last = "Landry", .user = "michaelhutchinson", .email = "daniel17@medina-wallace.net", .phone = "+1-634-486-6444x964" },
|
|
|
|
.{ .first = "Tammy", .last = "Hall", .user = "jamessmith", .email = null, .phone = "(926)810-3385x22059" },
|
|
|
|
.{ .first = "Stephanie", .last = "Anderson", .user = "wgillespie", .email = "campbelljaime@yahoo.com", .phone = null },
|
|
|
|
.{ .first = "Jennifer", .last = "Williams", .user = "shawn60", .email = null, .phone = "611-385-4771x97523" },
|
|
|
|
.{ .first = "Elizabeth", .last = "Ortiz", .user = "jennifer76", .email = "johnbradley@delgado.info", .phone = null },
|
|
|
|
.{ .first = "Stacy", .last = "Mays", .user = "scottgonzalez", .email = "kramermatthew@gmail.com", .phone = null },
|
|
|
|
.{ .first = "Jennifer", .last = "Smith", .user = "joseph75", .email = "masseyalexander@hill-moore.net", .phone = null },
|
|
|
|
.{ .first = "Gary", .last = "Hammond", .user = "brittany26", .email = null, .phone = null },
|
|
|
|
.{ .first = "Lisa", .last = "Johnson", .user = "tina28", .email = null, .phone = "850-606-2978x1081" },
|
|
|
|
.{ .first = "Zachary", .last = "Hopkins", .user = "vargasmichael", .email = null, .phone = null },
|
|
|
|
.{ .first = "Joshua", .last = "Kidd", .user = "ghanna", .email = "jbrown@yahoo.com", .phone = null },
|
|
|
|
.{ .first = "Dawn", .last = "Jones", .user = "alisonlindsey", .email = null, .phone = null },
|
|
|
|
.{ .first = "Monica", .last = "Berry", .user = "barbara40", .email = "michael00@hotmail.com", .phone = "(295)346-6453x343" },
|
|
|
|
.{ .first = "Shannon", .last = "Roberts", .user = "krystal37", .email = null, .phone = "980-920-9386x454" },
|
|
|
|
.{ .first = "Thomas", .last = "Mitchell", .user = "williamscorey", .email = "richardduncan@roberts.com", .phone = null },
|
|
|
|
.{ .first = "Nicole", .last = "Shaffer", .user = "rogerstroy", .email = null, .phone = "(570)128-5662" },
|
|
|
|
.{ .first = "Edward", .last = "Bennett", .user = "andersonchristina", .email = null, .phone = null },
|
|
|
|
.{ .first = "Duane", .last = "Howard", .user = "pcarpenter", .email = "griffithwayne@parker.net", .phone = null },
|
|
|
|
.{ .first = "Mary", .last = "Brown", .user = "kimberlyfrost", .email = "perezsara@anderson-andrews.net", .phone = null },
|
|
|
|
.{ .first = "Pamela", .last = "Sloan", .user = "kvelez", .email = "huynhlacey@moore-bell.biz", .phone = "001-359-125-1393x8716" },
|
|
|
|
.{ .first = "Timothy", .last = "Charles", .user = "anthony04", .email = "morrissara@hawkins.info", .phone = "+1-619-369-9572" },
|
|
|
|
.{ .first = "Sydney", .last = "Torres", .user = "scott42", .email = "asnyder@mitchell.net", .phone = null },
|
|
|
|
.{ .first = "John", .last = "Jones", .user = "anthonymoore", .email = null, .phone = "701.236.0571x99622" },
|
|
|
|
.{ .first = "Erik", .last = "Johnson", .user = "allisonsanders", .email = null, .phone = null },
|
|
|
|
.{ .first = "Donna", .last = "Kirk", .user = "laurie81", .email = null, .phone = null },
|
|
|
|
.{ .first = "Karina", .last = "White", .user = "uperez", .email = null, .phone = null },
|
|
|
|
.{ .first = "Jesse", .last = "Schwartz", .user = "ryan60", .email = "latoyawilliams@gmail.com", .phone = null },
|
|
|
|
.{ .first = "Cindy", .last = "Romero", .user = "christopher78", .email = "faulknerchristina@gmail.com", .phone = "780.288.2319x583" },
|
|
|
|
.{ .first = "Tyler", .last = "Sanders", .user = "bennettjessica", .email = null, .phone = "1966269423" },
|
|
|
|
.{ .first = "Pamela", .last = "Carter", .user = "zsnyder", .email = null, .phone = "125-062-9130x58413" },
|
|
|
|
};
|