···11+const std = @import("std");
22+const vaxis = @import("vaxis");
33+const Cell = vaxis.Cell;
44+const TextInput = vaxis.widgets.TextInput;
55+const border = vaxis.widgets.border;
66+77+const log = std.log.scoped(.main);
88+pub fn main() !void {
99+ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
1010+ defer {
1111+ const deinit_status = gpa.deinit();
1212+ //fail test; can't try in defer as defer is executed after we return
1313+ if (deinit_status == .leak) {
1414+ log.err("memory leak", .{});
1515+ }
1616+ }
1717+ const alloc = gpa.allocator();
1818+1919+ // Initialize Vaxis
2020+ var vx = try vaxis.init(Event, .{});
2121+ defer vx.deinit(alloc);
2222+2323+ // Start the read loop. This puts the terminal in raw mode and begins
2424+ // reading user input
2525+ try vx.start();
2626+ defer vx.stop();
2727+2828+ // Optionally enter the alternate screen
2929+ try vx.enterAltScreen();
3030+3131+ var text_input: TextInput = .{};
3232+3333+ // The main event loop. Vaxis provides a thread safe, blocking, buffered
3434+ // queue which can serve as the primary event queue for an application
3535+ outer: while (true) {
3636+ // nextEvent blocks until an event is in the queue
3737+ const event = vx.nextEvent();
3838+ log.debug("event: {}\r\n", .{event});
3939+ // exhaustive switching ftw. Vaxis will send events if your EventType
4040+ // enum has the fields for those events (ie "key_press", "winsize")
4141+ switch (event) {
4242+ .key_press => |key| {
4343+ text_input.update(.{ .key_press = key });
4444+ if (key.codepoint == 'c' and key.mods.ctrl) {
4545+ break :outer;
4646+ }
4747+ },
4848+ .winsize => |ws| {
4949+ try vx.resize(alloc, ws);
5050+ },
5151+ else => {},
5252+ }
5353+5454+ // vx.window() returns the root window. This window is the size of the
5555+ // terminal and can spawn child windows as logical areas. Child windows
5656+ // cannot draw outside of their bounds
5757+ const win = vx.window();
5858+ // Clear the entire space because we are drawing in immediate mode.
5959+ // vaxis double buffers the screen. This new frame will be compared to
6060+ // the old and only updated cells will be drawn
6161+ win.clear();
6262+ const child = win.initChild(win.width / 2 - 20, win.height / 2 - 3, .{ .limit = 40 }, .{ .limit = 3 });
6363+ // draw the text_input using a bordered window
6464+ text_input.draw(border.all(child, .{}));
6565+6666+ // Render the screen
6767+ try vx.render();
6868+ }
6969+}
7070+7171+// Our EventType. This can contain internal events as well as Vaxis events.
7272+// Internal events can be posted into the same queue as vaxis events to allow
7373+// for a single event loop with exhaustive switching. Booya
7474+const Event = union(enum) {
7575+ key_press: vaxis.Key,
7676+ winsize: vaxis.Winsize,
7777+ focus_in,
7878+ foo: u8,
7979+};
+3-1
src/Key.zig
···1414/// the unicode codepoint of the key event.
1515codepoint: u21,
16161717-/// the text generated from the key event, if any
1717+/// the text generated from the key event. This will only contain a value if the
1818+/// event generated a multi-codepoint grapheme. If there was only a single
1919+/// codepoint, library users can encode the codepoint directly
1820text: ?[]const u8 = null,
19212022/// the shifted codepoint of this key event. This will only be present if the