examples: add vaxis example

This commit is contained in:
Tim Culverhouse 2024-05-24 08:04:54 -05:00
parent 3a3f7d32cf
commit 409e17b883
2 changed files with 84 additions and 0 deletions

View file

@ -46,6 +46,7 @@ pub fn build(b: *std.Build) void {
shell, shell,
table, table,
text_input, text_input,
vaxis,
}; };
const example_option = b.option(Example, "example", "Example to run (default: text_input)") orelse .text_input; const example_option = b.option(Example, "example", "Example to run (default: text_input)") orelse .text_input;
const example_step = b.step("example", "Run example"); const example_step = b.step("example", "Run example");

83
examples/vaxis.zig Normal file
View file

@ -0,0 +1,83 @@
const std = @import("std");
const vaxis = @import("vaxis");
const Cell = vaxis.Cell;
const Event = union(enum) {
key_press: vaxis.Key,
winsize: vaxis.Winsize,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const deinit_status = gpa.deinit();
//fail test; can't try in defer as defer is executed after we return
if (deinit_status == .leak) {
std.log.err("memory leak", .{});
}
}
const alloc = gpa.allocator();
var vx = try vaxis.init(alloc, .{});
defer vx.deinit(alloc);
var loop: vaxis.Loop(Event) = .{ .vaxis = &vx };
try loop.run();
defer loop.stop();
try vx.enterAltScreen();
try vx.queryTerminal();
const lower_limit = 30;
var color_idx: u8 = lower_limit;
var dir: enum {
up,
down,
} = .up;
// block until we get a resize
while (true) {
const event = loop.nextEvent();
switch (event) {
.key_press => |key| if (key.matches('c', .{ .ctrl = true })) return,
.winsize => |ws| {
try vx.resize(alloc, ws);
break;
},
}
}
while (true) {
while (loop.tryEvent()) |event| {
switch (event) {
.key_press => |key| if (key.matches('c', .{ .ctrl = true })) return,
.winsize => |ws| try vx.resize(alloc, ws),
}
}
const win = vx.window();
win.clear();
const style: vaxis.Style = .{ .fg = .{ .rgb = [_]u8{ color_idx, color_idx, color_idx } } };
const segment: vaxis.Segment = .{
.text = vaxis.logo,
.style = style,
};
const center = vaxis.widgets.alignment.center(win, 28, 4);
_ = try center.printSegment(segment, .{ .wrap = .grapheme });
try vx.render();
std.time.sleep(8 * std.time.ns_per_ms);
switch (dir) {
.up => {
color_idx += 1;
if (color_idx == 255) dir = .down;
},
.down => {
color_idx -= 1;
if (color_idx == lower_limit) dir = .up;
},
}
}
}