this repo has no description
13
fork

Configure Feed

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

examples: add vaxis example

+84
+1
build.zig
··· 46 46 shell, 47 47 table, 48 48 text_input, 49 + vaxis, 49 50 }; 50 51 const example_option = b.option(Example, "example", "Example to run (default: text_input)") orelse .text_input; 51 52 const example_step = b.step("example", "Run example");
+83
examples/vaxis.zig
··· 1 + const std = @import("std"); 2 + const vaxis = @import("vaxis"); 3 + const Cell = vaxis.Cell; 4 + 5 + const Event = union(enum) { 6 + key_press: vaxis.Key, 7 + winsize: vaxis.Winsize, 8 + }; 9 + 10 + pub fn main() !void { 11 + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 12 + defer { 13 + const deinit_status = gpa.deinit(); 14 + //fail test; can't try in defer as defer is executed after we return 15 + if (deinit_status == .leak) { 16 + std.log.err("memory leak", .{}); 17 + } 18 + } 19 + const alloc = gpa.allocator(); 20 + 21 + var vx = try vaxis.init(alloc, .{}); 22 + defer vx.deinit(alloc); 23 + 24 + var loop: vaxis.Loop(Event) = .{ .vaxis = &vx }; 25 + 26 + try loop.run(); 27 + defer loop.stop(); 28 + 29 + try vx.enterAltScreen(); 30 + try vx.queryTerminal(); 31 + 32 + const lower_limit = 30; 33 + var color_idx: u8 = lower_limit; 34 + var dir: enum { 35 + up, 36 + down, 37 + } = .up; 38 + 39 + // block until we get a resize 40 + while (true) { 41 + const event = loop.nextEvent(); 42 + switch (event) { 43 + .key_press => |key| if (key.matches('c', .{ .ctrl = true })) return, 44 + .winsize => |ws| { 45 + try vx.resize(alloc, ws); 46 + break; 47 + }, 48 + } 49 + } 50 + 51 + while (true) { 52 + while (loop.tryEvent()) |event| { 53 + switch (event) { 54 + .key_press => |key| if (key.matches('c', .{ .ctrl = true })) return, 55 + .winsize => |ws| try vx.resize(alloc, ws), 56 + } 57 + } 58 + 59 + const win = vx.window(); 60 + win.clear(); 61 + 62 + const style: vaxis.Style = .{ .fg = .{ .rgb = [_]u8{ color_idx, color_idx, color_idx } } }; 63 + 64 + const segment: vaxis.Segment = .{ 65 + .text = vaxis.logo, 66 + .style = style, 67 + }; 68 + const center = vaxis.widgets.alignment.center(win, 28, 4); 69 + _ = try center.printSegment(segment, .{ .wrap = .grapheme }); 70 + try vx.render(); 71 + std.time.sleep(8 * std.time.ns_per_ms); 72 + switch (dir) { 73 + .up => { 74 + color_idx += 1; 75 + if (color_idx == 255) dir = .down; 76 + }, 77 + .down => { 78 + color_idx -= 1; 79 + if (color_idx == lower_limit) dir = .up; 80 + }, 81 + } 82 + } 83 + }