examples: fix api for main, pathological, table

This commit is contained in:
Tim Culverhouse 2024-04-29 12:35:02 -05:00
parent 41f76e8f03
commit e7915b5dd7
3 changed files with 21 additions and 18 deletions

View file

@ -15,13 +15,15 @@ pub fn main() !void {
const alloc = gpa.allocator(); const alloc = gpa.allocator();
// Initialize Vaxis // Initialize Vaxis
var vx = try vaxis.init(Event, .{}); var vx = try vaxis.init(.{});
defer vx.deinit(alloc); defer vx.deinit(alloc);
var loop: vaxis.Loop(Event) = .{ .vaxis = &vx };
// Start the read loop. This puts the terminal in raw mode and begins // Start the read loop. This puts the terminal in raw mode and begins
// reading user input // reading user input
try vx.startReadThread(); try loop.run();
defer vx.stopReadThread(); defer loop.stop();
// Optionally enter the alternate screen // Optionally enter the alternate screen
try vx.enterAltScreen(); try vx.enterAltScreen();
@ -34,7 +36,7 @@ pub fn main() !void {
// queue which can serve as the primary event queue for an application // queue which can serve as the primary event queue for an application
while (true) { while (true) {
// nextEvent blocks until an event is in the queue // nextEvent blocks until an event is in the queue
const event = vx.nextEvent(); const event = loop.nextEvent();
log.debug("event: {}", .{event}); log.debug("event: {}", .{event});
// exhaustive switching ftw. Vaxis will send events if your Event // exhaustive switching ftw. Vaxis will send events if your Event
// enum has the fields for those events (ie "key_press", "winsize") // enum has the fields for those events (ie "key_press", "winsize")

View file

@ -11,16 +11,18 @@ const Event = union(enum) {
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator(); const alloc = gpa.allocator();
var vx = try vaxis.init(Event, .{}); var vx = try vaxis.init(.{});
errdefer vx.deinit(alloc); errdefer vx.deinit(alloc);
try vx.startReadThread(); var loop: vaxis.Loop(Event) = .{ .vaxis = &vx };
defer vx.stopReadThread();
try loop.run();
defer loop.stop();
try vx.enterAltScreen(); try vx.enterAltScreen();
try vx.queryTerminal(); try vx.queryTerminal();
while (true) { while (true) {
const event = vx.nextEvent(); const event = loop.nextEvent();
switch (event) { switch (event) {
.winsize => |ws| { .winsize => |ws| {
try vx.resize(alloc, ws); try vx.resize(alloc, ws);

View file

@ -25,17 +25,16 @@ pub fn main() !void {
const user_list = std.ArrayList(User).fromOwnedSlice(alloc, users_buf); const user_list = std.ArrayList(User).fromOwnedSlice(alloc, users_buf);
defer user_list.deinit(); defer user_list.deinit();
var vx = try vaxis.init( var vx = try vaxis.init(.{});
union(enum) {
key_press: vaxis.Key,
winsize: vaxis.Winsize,
},
.{},
);
defer vx.deinit(alloc); defer vx.deinit(alloc);
try vx.startReadThread(); var loop: vaxis.Loop(union(enum) {
defer vx.stopReadThread(); key_press: vaxis.Key,
winsize: vaxis.Winsize,
}) = .{ .vaxis = &vx };
try loop.run();
defer loop.stop();
try vx.enterAltScreen(); try vx.enterAltScreen();
try vx.queryTerminal(); try vx.queryTerminal();
@ -77,7 +76,7 @@ pub fn main() !void {
var event_arena = heap.ArenaAllocator.init(alloc); var event_arena = heap.ArenaAllocator.init(alloc);
defer event_arena.deinit(); defer event_arena.deinit();
const event_alloc = event_arena.allocator(); const event_alloc = event_arena.allocator();
const event = vx.nextEvent(); const event = loop.nextEvent();
switch (event) { switch (event) {
.key_press => |key| keyEvt: { .key_press => |key| keyEvt: {