this repo has no description
13
fork

Configure Feed

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

examples: add readline example

+76
+1
build.zig
··· 43 43 main, 44 44 nvim, 45 45 pathological, 46 + readline, 46 47 shell, 47 48 table, 48 49 text_input,
+75
examples/readline.zig
··· 1 + const std = @import("std"); 2 + const vaxis = @import("vaxis"); 3 + const Cell = vaxis.Cell; 4 + const TextInput = vaxis.widgets.TextInput; 5 + const border = vaxis.widgets.border; 6 + 7 + const log = std.log.scoped(.main); 8 + 9 + const Event = union(enum) { 10 + key_press: vaxis.Key, 11 + winsize: vaxis.Winsize, 12 + }; 13 + 14 + pub fn main() !void { 15 + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 16 + defer { 17 + const deinit_status = gpa.deinit(); 18 + //fail test; can't try in defer as defer is executed after we return 19 + if (deinit_status == .leak) { 20 + log.err("memory leak", .{}); 21 + } 22 + } 23 + const alloc = gpa.allocator(); 24 + 25 + var line: ?[]const u8 = null; 26 + defer { 27 + // do this in defer so that vaxis cleans up terminal state before we 28 + // print to stdout 29 + if (line) |_| { 30 + const stdout = std.io.getStdOut().writer(); 31 + stdout.print("\n{s}\n", .{line.?}) catch {}; 32 + alloc.free(line.?); 33 + } 34 + } 35 + var vx = try vaxis.init(alloc, .{}); 36 + defer vx.deinit(alloc); 37 + 38 + var loop: vaxis.Loop(Event) = .{ .vaxis = &vx }; 39 + try loop.run(); 40 + defer loop.stop(); 41 + 42 + var text_input = TextInput.init(alloc, &vx.unicode); 43 + defer text_input.deinit(); 44 + 45 + try vx.queryTerminal(); 46 + 47 + const prompt: vaxis.Segment = .{ .text = "$ " }; 48 + 49 + while (true) { 50 + const event = loop.nextEvent(); 51 + switch (event) { 52 + .key_press => |key| { 53 + if (key.matches('c', .{ .ctrl = true })) { 54 + break; 55 + } else if (key.matches(vaxis.Key.enter, .{})) { 56 + line = try text_input.toOwnedSlice(); 57 + text_input.clearAndFree(); 58 + break; 59 + } else { 60 + try text_input.update(.{ .key_press = key }); 61 + } 62 + }, 63 + .winsize => |ws| try vx.resize(alloc, ws), 64 + } 65 + 66 + const win = vx.window(); 67 + 68 + win.clear(); 69 + _ = try win.printSegment(prompt, .{}); 70 + 71 + const input_win = win.child(.{ .x_off = 2 }); 72 + text_input.draw(input_win); 73 + try vx.render(); 74 + } 75 + }