From e7915b5dd7a1ea0159136b1a53a1809f27f7c0de Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Mon, 29 Apr 2024 12:35:02 -0500 Subject: [PATCH] examples: fix api for main, pathological, table --- examples/main.zig | 10 ++++++---- examples/pathological.zig | 10 ++++++---- examples/table.zig | 19 +++++++++---------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/examples/main.zig b/examples/main.zig index b820d3f..9bff3bc 100644 --- a/examples/main.zig +++ b/examples/main.zig @@ -15,13 +15,15 @@ pub fn main() !void { const alloc = gpa.allocator(); // Initialize Vaxis - var vx = try vaxis.init(Event, .{}); + var vx = try vaxis.init(.{}); defer vx.deinit(alloc); + var loop: vaxis.Loop(Event) = .{ .vaxis = &vx }; + // Start the read loop. This puts the terminal in raw mode and begins // reading user input - try vx.startReadThread(); - defer vx.stopReadThread(); + try loop.run(); + defer loop.stop(); // Optionally enter the alternate screen try vx.enterAltScreen(); @@ -34,7 +36,7 @@ pub fn main() !void { // queue which can serve as the primary event queue for an application while (true) { // nextEvent blocks until an event is in the queue - const event = vx.nextEvent(); + const event = loop.nextEvent(); log.debug("event: {}", .{event}); // exhaustive switching ftw. Vaxis will send events if your Event // enum has the fields for those events (ie "key_press", "winsize") diff --git a/examples/pathological.zig b/examples/pathological.zig index f4354f0..2e9d664 100644 --- a/examples/pathological.zig +++ b/examples/pathological.zig @@ -11,16 +11,18 @@ const Event = union(enum) { pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gpa.allocator(); - var vx = try vaxis.init(Event, .{}); + var vx = try vaxis.init(.{}); errdefer vx.deinit(alloc); - try vx.startReadThread(); - defer vx.stopReadThread(); + var loop: vaxis.Loop(Event) = .{ .vaxis = &vx }; + + try loop.run(); + defer loop.stop(); try vx.enterAltScreen(); try vx.queryTerminal(); while (true) { - const event = vx.nextEvent(); + const event = loop.nextEvent(); switch (event) { .winsize => |ws| { try vx.resize(alloc, ws); diff --git a/examples/table.zig b/examples/table.zig index 870efc2..ccbb926 100644 --- a/examples/table.zig +++ b/examples/table.zig @@ -25,17 +25,16 @@ pub fn main() !void { const user_list = std.ArrayList(User).fromOwnedSlice(alloc, users_buf); defer user_list.deinit(); - var vx = try vaxis.init( - union(enum) { - key_press: vaxis.Key, - winsize: vaxis.Winsize, - }, - .{}, - ); + var vx = try vaxis.init(.{}); defer vx.deinit(alloc); - try vx.startReadThread(); - defer vx.stopReadThread(); + var loop: vaxis.Loop(union(enum) { + key_press: vaxis.Key, + winsize: vaxis.Winsize, + }) = .{ .vaxis = &vx }; + + try loop.run(); + defer loop.stop(); try vx.enterAltScreen(); try vx.queryTerminal(); @@ -77,7 +76,7 @@ pub fn main() !void { var event_arena = heap.ArenaAllocator.init(alloc); defer event_arena.deinit(); const event_alloc = event_arena.allocator(); - const event = vx.nextEvent(); + const event = loop.nextEvent(); switch (event) { .key_press => |key| keyEvt: {