this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

update example

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+39 -18
+37 -16
examples/text_input.zig
··· 5 5 const border = vaxis.widgets.border; 6 6 7 7 const log = std.log.scoped(.main); 8 + 9 + // Our EventType. This can contain internal events as well as Vaxis events. 10 + // Internal events can be posted into the same queue as vaxis events to allow 11 + // for a single event loop with exhaustive switching. Booya 12 + const Event = union(enum) { 13 + key_press: vaxis.Key, 14 + winsize: vaxis.Winsize, 15 + focus_in, 16 + foo: u8, 17 + }; 18 + 8 19 pub fn main() !void { 9 20 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 10 21 defer { ··· 16 27 } 17 28 const alloc = gpa.allocator(); 18 29 19 - // Initialize Vaxis 30 + // Initialize Vaxis with our event type 20 31 var vx = try vaxis.init(Event, .{}); 32 + // deinit takes an optional allocator. If your program is exiting, you can 33 + // choose to pass a null allocator to save some exit time. 21 34 defer vx.deinit(alloc); 22 35 23 36 // Start the read loop. This puts the terminal in raw mode and begins 24 37 // reading user input 25 - try vx.start(); 26 - defer vx.stop(); 38 + try vx.startReadThread(); 39 + defer vx.stopReadThread(); 27 40 28 41 // Optionally enter the alternate screen 29 42 try vx.enterAltScreen(); ··· 31 44 // We'll adjust the color index every keypress for the border 32 45 var color_idx: u8 = 0; 33 46 47 + // init our text input widget. The text input widget needs an allocator to 48 + // store the contents of the input 34 49 var text_input = TextInput.init(alloc); 35 50 defer text_input.deinit(); 36 51 52 + // Sends queries to terminal to detect certain features. This should 53 + // _always_ be called, but is left to the application to decide when 37 54 try vx.queryTerminal(); 38 55 39 56 // The main event loop. Vaxis provides a thread safe, blocking, buffered ··· 59 76 } 60 77 }, 61 78 62 - .winsize => |ws| { 63 - try vx.resize(alloc, ws); 64 - }, 79 + // winsize events are sent to the application to ensure that all 80 + // resizes occur in the main thread. This lets us avoid expensive 81 + // locks on the screen. All applications must handle this event 82 + // unless they aren't using a screen (IE only detecting features) 83 + // 84 + // This is the only call that the core of Vaxis needs an allocator 85 + // for. The allocations are because we keep a copy of each cell to 86 + // optimize renders. When resize is called, we allocated two slices: 87 + // one for the screen, and one for our buffered screen. Each cell in 88 + // the buffered screen contains an ArrayList(u8) to be able to store 89 + // the grapheme for that cell Each cell is initialized with a size 90 + // of 1, which is sufficient for all of ASCII. Anything requiring 91 + // more than one byte will incur an allocation on the first render 92 + // after it is drawn. Thereafter, it will not allocate unless the 93 + // screen is resized 94 + .winsize => |ws| try vx.resize(alloc, ws), 65 95 else => {}, 66 96 } 67 97 ··· 69 99 // terminal and can spawn child windows as logical areas. Child windows 70 100 // cannot draw outside of their bounds 71 101 const win = vx.window(); 102 + 72 103 // Clear the entire space because we are drawing in immediate mode. 73 104 // vaxis double buffers the screen. This new frame will be compared to 74 105 // the old and only updated cells will be drawn ··· 89 120 try vx.render(); 90 121 } 91 122 } 92 - 93 - // Our EventType. This can contain internal events as well as Vaxis events. 94 - // Internal events can be posted into the same queue as vaxis events to allow 95 - // for a single event loop with exhaustive switching. Booya 96 - const Event = union(enum) { 97 - key_press: vaxis.Key, 98 - winsize: vaxis.Winsize, 99 - focus_in, 100 - foo: u8, 101 - };
+2 -2
src/vaxis.zig
··· 109 109 } 110 110 111 111 /// spawns the input thread to start listening to the tty for input 112 - pub fn start(self: *Self) !void { 112 + pub fn startReadThread(self: *Self) !void { 113 113 self.tty = try Tty.init(); 114 114 // run our tty read loop in it's own thread 115 115 const read_thread = try std.Thread.spawn(.{}, Tty.run, .{ &self.tty.?, T, self }); ··· 117 117 } 118 118 119 119 /// stops reading from the tty 120 - pub fn stop(self: *Self) void { 120 + pub fn stopReadThread(self: *Self) void { 121 121 if (self.tty) |_| { 122 122 var tty = &self.tty.?; 123 123 tty.stop();