this repo has no description
13
fork

Configure Feed

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

examples: remove pause_tui

-139
-139
examples/pause_tui.zig
··· 1 - const std = @import("std"); 2 - const mem = std.mem; 3 - const process = std.process; 4 - const vaxis = @import("vaxis"); 5 - const Cell = vaxis.Cell; 6 - const TextInput = vaxis.widgets.TextInput; 7 - const border = vaxis.widgets.border; 8 - 9 - const log = std.log.scoped(.main); 10 - 11 - // Our Event. This can contain internal events as well as Vaxis events. 12 - // Internal events can be posted into the same queue as vaxis events to allow 13 - // for a single event loop with exhaustive switching. Booya 14 - const Event = union(enum) { 15 - key_press: vaxis.Key, 16 - mouse: vaxis.Mouse, 17 - winsize: vaxis.Winsize, 18 - focus_in, 19 - focus_out, 20 - foo: u8, 21 - }; 22 - 23 - pub fn main() !void { 24 - var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 25 - defer { 26 - const deinit_status = gpa.deinit(); 27 - //fail test; can't try in defer as defer is executed after we return 28 - if (deinit_status == .leak) { 29 - log.err("memory leak", .{}); 30 - } 31 - } 32 - const alloc = gpa.allocator(); 33 - 34 - var tty = try vaxis.Tty.init(); 35 - defer tty.deinit(); 36 - 37 - var vx = try vaxis.init(alloc, .{}); 38 - defer vx.deinit(alloc, tty.anyWriter()); 39 - 40 - var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx }; 41 - try loop.init(); 42 - 43 - try loop.start(); 44 - defer loop.stop(); 45 - 46 - // We'll adjust the color index every keypress for the border 47 - var color_idx: u8 = 0; 48 - 49 - // init our text input widget. The text input widget needs an allocator to 50 - // store the contents of the input 51 - var text_input = TextInput.init(alloc); 52 - defer text_input.deinit(); 53 - 54 - // Sends queries to terminal to detect certain features. This should 55 - // _always_ be called, but is left to the application to decide when 56 - try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s); 57 - 58 - try vx.setMouseMode(true); 59 - 60 - // The main event loop. Vaxis provides a thread safe, blocking, buffered 61 - // queue which can serve as the primary event queue for an application 62 - while (true) { 63 - // nextEvent blocks until an event is in the queue 64 - const event = vx.nextEvent(); 65 - log.debug("event: {}", .{event}); 66 - // exhaustive switching ftw. Vaxis will send events if your Event 67 - // enum has the fields for those events (ie "key_press", "winsize") 68 - switch (event) { 69 - .key_press => |key| { 70 - color_idx = switch (color_idx) { 71 - 255 => 0, 72 - else => color_idx + 1, 73 - }; 74 - if (key.matches('c', .{ .ctrl = true })) { 75 - break; 76 - } else if (key.matches('l', .{ .ctrl = true })) { 77 - vx.queueRefresh(); 78 - } else if (key.matches('z', .{ .ctrl = true })) { 79 - try openDirVim(alloc, &vx, "examples"); 80 - } else { 81 - try text_input.update(.{ .key_press = key }); 82 - } 83 - }, 84 - 85 - // winsize events are sent to the application to ensure that all 86 - // resizes occur in the main thread. This lets us avoid expensive 87 - // locks on the screen. All applications must handle this event 88 - // unless they aren't using a screen (IE only detecting features) 89 - // 90 - // This is the only call that the core of Vaxis needs an allocator 91 - // for. The allocations are because we keep a copy of each cell to 92 - // optimize renders. When resize is called, we allocated two slices: 93 - // one for the screen, and one for our buffered screen. Each cell in 94 - // the buffered screen contains an ArrayList(u8) to be able to store 95 - // the grapheme for that cell Each cell is initialized with a size 96 - // of 1, which is sufficient for all of ASCII. Anything requiring 97 - // more than one byte will incur an allocation on the first render 98 - // after it is drawn. Thereafter, it will not allocate unless the 99 - // screen is resized 100 - .winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws), 101 - else => {}, 102 - } 103 - 104 - // vx.window() returns the root window. This window is the size of the 105 - // terminal and can spawn child windows as logical areas. Child windows 106 - // cannot draw outside of their bounds 107 - const win = vx.window(); 108 - 109 - // Clear the entire space because we are drawing in immediate mode. 110 - // vaxis double buffers the screen. This new frame will be compared to 111 - // the old and only updated cells will be drawn 112 - win.clear(); 113 - const child = win.initChild( 114 - win.width / 2 - 20, 115 - win.height / 2 - 3, 116 - .{ .limit = 40 }, 117 - .{ .limit = 3 }, 118 - ); 119 - // draw the text_input using a bordered window 120 - const style: vaxis.Style = .{ 121 - .fg = .{ .index = color_idx }, 122 - }; 123 - text_input.draw(border.all(child, style)); 124 - 125 - // Render the screen 126 - try vx.render(tty.anyWriter()); 127 - } 128 - } 129 - 130 - /// Open the provided Directory in a temp Vim session, pausing the TUI. 131 - fn openDirVim(alloc: mem.Allocator, vx: anytype, path: []const u8) !void { 132 - try vx.exitAltScreen(); 133 - vx.stopReadThread(); 134 - var dir_look = process.Child.init(&.{ "vim", "-R", path }, alloc); 135 - _ = try dir_look.spawnAndWait(); 136 - try vx.startReadThread(); 137 - try vx.enterAltScreen(); 138 - vx.queueRefresh(); 139 - }