2024-05-30 16:41:45 +02:00
|
|
|
const std = @import("std");
|
|
|
|
const vaxis = @import("vaxis");
|
|
|
|
const xev = @import("xev");
|
|
|
|
const Cell = vaxis.Cell;
|
|
|
|
|
|
|
|
pub const panic = vaxis.panic_handler;
|
|
|
|
|
2024-05-30 18:10:03 +02:00
|
|
|
const App = struct {
|
|
|
|
const lower_limit: u8 = 30;
|
|
|
|
const next_ms: u64 = 8;
|
|
|
|
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
vx: *vaxis.Vaxis,
|
|
|
|
buffered_writer: std.io.BufferedWriter(4096, std.io.AnyWriter),
|
|
|
|
color_idx: u8,
|
|
|
|
dir: enum {
|
|
|
|
up,
|
|
|
|
down,
|
|
|
|
},
|
|
|
|
|
|
|
|
fn draw(self: *App) !void {
|
|
|
|
const style: vaxis.Style = .{ .fg = .{ .rgb = [_]u8{ self.color_idx, self.color_idx, self.color_idx } } };
|
|
|
|
|
|
|
|
const segment: vaxis.Segment = .{
|
|
|
|
.text = vaxis.logo,
|
|
|
|
.style = style,
|
|
|
|
};
|
|
|
|
const win = self.vx.window();
|
|
|
|
win.clear();
|
|
|
|
const center = vaxis.widgets.alignment.center(win, 28, 4);
|
|
|
|
_ = try center.printSegment(segment, .{ .wrap = .grapheme });
|
|
|
|
switch (self.dir) {
|
|
|
|
.up => {
|
|
|
|
self.color_idx += 1;
|
|
|
|
if (self.color_idx == 255) self.dir = .down;
|
|
|
|
},
|
|
|
|
.down => {
|
|
|
|
self.color_idx -= 1;
|
|
|
|
if (self.color_idx == lower_limit) self.dir = .up;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
try self.vx.render(self.buffered_writer.writer().any());
|
|
|
|
try self.buffered_writer.flush();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-30 16:41:45 +02:00
|
|
|
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", .{});
|
|
|
|
}
|
|
|
|
}
|
2024-05-30 18:10:03 +02:00
|
|
|
const alloc = gpa.allocator();
|
2024-05-30 16:41:45 +02:00
|
|
|
|
|
|
|
var tty = try vaxis.Tty.init();
|
2024-06-13 21:31:42 +02:00
|
|
|
defer tty.deinit();
|
2024-05-30 16:41:45 +02:00
|
|
|
|
|
|
|
var vx = try vaxis.init(alloc, .{});
|
|
|
|
defer vx.deinit(alloc, tty.anyWriter());
|
|
|
|
|
2024-06-13 16:04:04 +02:00
|
|
|
var pool = xev.ThreadPool.init(.{});
|
|
|
|
var loop = try xev.Loop.init(.{
|
|
|
|
.thread_pool = &pool,
|
|
|
|
});
|
2024-05-30 16:41:45 +02:00
|
|
|
defer loop.deinit();
|
|
|
|
|
2024-05-30 18:10:03 +02:00
|
|
|
var app: App = .{
|
|
|
|
.allocator = alloc,
|
|
|
|
.buffered_writer = tty.bufferedWriter(),
|
|
|
|
.color_idx = App.lower_limit,
|
|
|
|
.dir = .up,
|
|
|
|
.vx = &vx,
|
|
|
|
};
|
|
|
|
|
|
|
|
var vx_loop: vaxis.xev.TtyWatcher(App) = undefined;
|
|
|
|
try vx_loop.init(&tty, &vx, &loop, &app, eventCallback);
|
2024-05-30 16:41:45 +02:00
|
|
|
|
|
|
|
try vx.enterAltScreen(tty.anyWriter());
|
|
|
|
// send queries asynchronously
|
|
|
|
try vx.queryTerminalSend(tty.anyWriter());
|
|
|
|
|
2024-05-30 18:10:03 +02:00
|
|
|
const timer = try xev.Timer.init();
|
|
|
|
var timer_cmp: xev.Completion = .{};
|
|
|
|
timer.run(&loop, &timer_cmp, App.next_ms, App, &app, timerCallback);
|
|
|
|
|
2024-05-30 16:41:45 +02:00
|
|
|
try loop.run(.until_done);
|
|
|
|
}
|
|
|
|
|
2024-05-30 18:10:03 +02:00
|
|
|
fn eventCallback(
|
|
|
|
ud: ?*App,
|
2024-05-30 16:41:45 +02:00
|
|
|
loop: *xev.Loop,
|
2024-05-30 18:10:03 +02:00
|
|
|
watcher: *vaxis.xev.TtyWatcher(App),
|
2024-05-30 16:41:45 +02:00
|
|
|
event: vaxis.xev.Event,
|
|
|
|
) xev.CallbackAction {
|
2024-05-30 18:10:03 +02:00
|
|
|
const app = ud orelse unreachable;
|
2024-05-30 16:41:45 +02:00
|
|
|
switch (event) {
|
|
|
|
.key_press => |key| {
|
|
|
|
if (key.matches('c', .{ .ctrl = true })) {
|
|
|
|
loop.stop();
|
|
|
|
return .disarm;
|
|
|
|
}
|
|
|
|
},
|
2024-05-30 19:05:18 +02:00
|
|
|
.winsize => |ws| watcher.vx.resize(app.allocator, watcher.tty.anyWriter(), ws) catch @panic("TODO"),
|
2024-05-30 16:41:45 +02:00
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
return .rearm;
|
|
|
|
}
|
2024-05-30 18:10:03 +02:00
|
|
|
|
|
|
|
fn timerCallback(
|
|
|
|
ud: ?*App,
|
|
|
|
l: *xev.Loop,
|
|
|
|
c: *xev.Completion,
|
|
|
|
r: xev.Timer.RunError!void,
|
|
|
|
) xev.CallbackAction {
|
|
|
|
_ = r catch @panic("timer error");
|
|
|
|
|
|
|
|
var app = ud orelse return .disarm;
|
|
|
|
app.draw() catch @panic("couldn't draw");
|
|
|
|
|
|
|
|
const timer = try xev.Timer.init();
|
|
|
|
timer.run(l, c, App.next_ms, App, ud, timerCallback);
|
|
|
|
|
|
|
|
return .disarm;
|
|
|
|
}
|