a modern tui library written in zig
at v0.3.0 101 lines 3.8 kB view raw
1const std = @import("std"); 2const vaxis = @import("vaxis"); 3const Cell = vaxis.Cell; 4 5const log = std.log.scoped(.main); 6pub fn main() !void { 7 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 8 defer { 9 const deinit_status = gpa.deinit(); 10 //fail test; can't try in defer as defer is executed after we return 11 if (deinit_status == .leak) { 12 log.err("memory leak", .{}); 13 } 14 } 15 const alloc = gpa.allocator(); 16 17 var tty = try vaxis.Tty.init(); 18 defer tty.deinit(); 19 20 var vx = try vaxis.init(alloc, .{}); 21 defer vx.deinit(alloc, tty.anyWriter()); 22 23 var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; 24 try loop.init(); 25 26 try loop.start(); 27 defer loop.stop(); 28 29 // Optionally enter the alternate screen 30 try vx.enterAltScreen(tty.anyWriter()); 31 32 // We'll adjust the color index every keypress 33 var color_idx: u8 = 0; 34 const msg = "Hello, world!"; 35 36 // The main event loop. Vaxis provides a thread safe, blocking, buffered 37 // queue which can serve as the primary event queue for an application 38 while (true) { 39 // nextEvent blocks until an event is in the queue 40 const event = loop.nextEvent(); 41 log.debug("event: {}", .{event}); 42 // exhaustive switching ftw. Vaxis will send events if your Event 43 // enum has the fields for those events (ie "key_press", "winsize") 44 switch (event) { 45 .key_press => |key| { 46 color_idx = switch (color_idx) { 47 255 => 0, 48 else => color_idx + 1, 49 }; 50 if (key.codepoint == 'c' and key.mods.ctrl) { 51 break; 52 } 53 }, 54 .winsize => |ws| { 55 try vx.resize(alloc, tty.anyWriter(), ws); 56 }, 57 else => {}, 58 } 59 60 // vx.window() returns the root window. This window is the size of the 61 // terminal and can spawn child windows as logical areas. Child windows 62 // cannot draw outside of their bounds 63 const win = vx.window(); 64 // Clear the entire space because we are drawing in immediate mode. 65 // vaxis double buffers the screen. This new frame will be compared to 66 // the old and only updated cells will be drawn 67 win.clear(); 68 69 // Create some child window. .expand means the height and width will 70 // fill the remaining space of the parent. Child windows do not store a 71 // reference to their parent: this is true immediate mode. Do not store 72 // windows, always create new windows each render cycle 73 const child = win.initChild(win.width / 2 - msg.len / 2, win.height / 2, .expand, .expand); 74 // Loop through the message and print the cells to the screen 75 for (msg, 0..) |_, i| { 76 const cell: Cell = .{ 77 // each cell takes a _grapheme_ as opposed to a single 78 // codepoint. This allows Vaxis to handle emoji properly, 79 // particularly with terminals that the Unicode Core extension 80 // (IE Mode 2027) 81 .char = .{ .grapheme = msg[i .. i + 1] }, 82 .style = .{ 83 .fg = .{ .index = color_idx }, 84 }, 85 }; 86 child.writeCell(i, 0, cell); 87 } 88 // Render the screen 89 try vx.render(tty.anyWriter()); 90 } 91} 92 93// Our Event. 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 96const Event = union(enum) { 97 key_press: vaxis.Key, 98 winsize: vaxis.Winsize, 99 focus_in, 100 foo: u8, 101};