2024-05-24 15:04:54 +02:00
|
|
|
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();
|
|
|
|
|
2024-05-29 20:13:54 +02:00
|
|
|
var tty = try vaxis.Tty.init();
|
2024-05-24 15:04:54 +02:00
|
|
|
var vx = try vaxis.init(alloc, .{});
|
2024-05-29 20:13:54 +02:00
|
|
|
defer vx.deinit(alloc, tty.anyWriter());
|
2024-05-24 15:04:54 +02:00
|
|
|
|
2024-05-29 20:13:54 +02:00
|
|
|
var loop: vaxis.Loop(Event) = .{ .tty = &tty, .vaxis = &vx };
|
|
|
|
try loop.init();
|
2024-05-24 15:04:54 +02:00
|
|
|
|
2024-05-29 20:13:54 +02:00
|
|
|
try loop.start();
|
2024-05-24 15:04:54 +02:00
|
|
|
defer loop.stop();
|
|
|
|
|
2024-05-29 20:13:54 +02:00
|
|
|
try vx.enterAltScreen(tty.anyWriter());
|
|
|
|
try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
|
2024-05-24 15:04:54 +02:00
|
|
|
|
|
|
|
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| {
|
2024-05-29 20:13:54 +02:00
|
|
|
try vx.resize(alloc, tty.anyWriter(), ws);
|
2024-05-24 15:04:54 +02:00
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
while (loop.tryEvent()) |event| {
|
|
|
|
switch (event) {
|
|
|
|
.key_press => |key| if (key.matches('c', .{ .ctrl = true })) return,
|
2024-05-29 20:13:54 +02:00
|
|
|
.winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws),
|
2024-05-24 15:04:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
2024-05-29 20:13:54 +02:00
|
|
|
try vx.render(tty.anyWriter());
|
2024-05-24 15:04:54 +02:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|