···55const border = vaxis.widgets.border;
6677const log = std.log.scoped(.main);
88+99+// Our EventType. This can contain internal events as well as Vaxis events.
1010+// Internal events can be posted into the same queue as vaxis events to allow
1111+// for a single event loop with exhaustive switching. Booya
1212+const Event = union(enum) {
1313+ key_press: vaxis.Key,
1414+ winsize: vaxis.Winsize,
1515+ focus_in,
1616+ foo: u8,
1717+};
1818+819pub fn main() !void {
920 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
1021 defer {
···1627 }
1728 const alloc = gpa.allocator();
18291919- // Initialize Vaxis
3030+ // Initialize Vaxis with our event type
2031 var vx = try vaxis.init(Event, .{});
3232+ // deinit takes an optional allocator. If your program is exiting, you can
3333+ // choose to pass a null allocator to save some exit time.
2134 defer vx.deinit(alloc);
22352336 // Start the read loop. This puts the terminal in raw mode and begins
2437 // reading user input
2525- try vx.start();
2626- defer vx.stop();
3838+ try vx.startReadThread();
3939+ defer vx.stopReadThread();
27402841 // Optionally enter the alternate screen
2942 try vx.enterAltScreen();
···3144 // We'll adjust the color index every keypress for the border
3245 var color_idx: u8 = 0;
33464747+ // init our text input widget. The text input widget needs an allocator to
4848+ // store the contents of the input
3449 var text_input = TextInput.init(alloc);
3550 defer text_input.deinit();
36515252+ // Sends queries to terminal to detect certain features. This should
5353+ // _always_ be called, but is left to the application to decide when
3754 try vx.queryTerminal();
38553956 // The main event loop. Vaxis provides a thread safe, blocking, buffered
···5976 }
6077 },
61786262- .winsize => |ws| {
6363- try vx.resize(alloc, ws);
6464- },
7979+ // winsize events are sent to the application to ensure that all
8080+ // resizes occur in the main thread. This lets us avoid expensive
8181+ // locks on the screen. All applications must handle this event
8282+ // unless they aren't using a screen (IE only detecting features)
8383+ //
8484+ // This is the only call that the core of Vaxis needs an allocator
8585+ // for. The allocations are because we keep a copy of each cell to
8686+ // optimize renders. When resize is called, we allocated two slices:
8787+ // one for the screen, and one for our buffered screen. Each cell in
8888+ // the buffered screen contains an ArrayList(u8) to be able to store
8989+ // the grapheme for that cell Each cell is initialized with a size
9090+ // of 1, which is sufficient for all of ASCII. Anything requiring
9191+ // more than one byte will incur an allocation on the first render
9292+ // after it is drawn. Thereafter, it will not allocate unless the
9393+ // screen is resized
9494+ .winsize => |ws| try vx.resize(alloc, ws),
6595 else => {},
6696 }
6797···6999 // terminal and can spawn child windows as logical areas. Child windows
70100 // cannot draw outside of their bounds
71101 const win = vx.window();
102102+72103 // Clear the entire space because we are drawing in immediate mode.
73104 // vaxis double buffers the screen. This new frame will be compared to
74105 // the old and only updated cells will be drawn
···89120 try vx.render();
90121 }
91122}
9292-9393-// Our EventType. This can contain internal events as well as Vaxis events.
9494-// Internal events can be posted into the same queue as vaxis events to allow
9595-// for a single event loop with exhaustive switching. Booya
9696-const Event = union(enum) {
9797- key_press: vaxis.Key,
9898- winsize: vaxis.Winsize,
9999- focus_in,
100100- foo: u8,
101101-};
+2-2
src/vaxis.zig
···109109 }
110110111111 /// spawns the input thread to start listening to the tty for input
112112- pub fn start(self: *Self) !void {
112112+ pub fn startReadThread(self: *Self) !void {
113113 self.tty = try Tty.init();
114114 // run our tty read loop in it's own thread
115115 const read_thread = try std.Thread.spawn(.{}, Tty.run, .{ &self.tty.?, T, self });
···117117 }
118118119119 /// stops reading from the tty
120120- pub fn stop(self: *Self) void {
120120+ pub fn stopReadThread(self: *Self) void {
121121 if (self.tty) |_| {
122122 var tty = &self.tty.?;
123123 tty.stop();