2024-01-19 03:17:26 +01:00
|
|
|
const std = @import("std");
|
2024-01-19 17:13:32 +01:00
|
|
|
const vaxis = @import("vaxis");
|
2024-01-19 18:30:00 +01:00
|
|
|
const Cell = vaxis.Cell;
|
2024-01-19 03:17:26 +01:00
|
|
|
|
|
|
|
const log = std.log.scoped(.main);
|
|
|
|
pub fn main() !void {
|
2024-01-19 06:17:57 +01:00
|
|
|
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) {
|
|
|
|
log.err("memory leak", .{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const alloc = gpa.allocator();
|
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
// Initialize Vaxis
|
2024-04-30 00:03:54 +02:00
|
|
|
var vx = try vaxis.init(alloc, .{});
|
2024-01-19 17:13:32 +01:00
|
|
|
defer vx.deinit(alloc);
|
2024-01-19 03:17:26 +01:00
|
|
|
|
2024-04-29 19:35:02 +02:00
|
|
|
var loop: vaxis.Loop(Event) = .{ .vaxis = &vx };
|
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
// Start the read loop. This puts the terminal in raw mode and begins
|
|
|
|
// reading user input
|
2024-04-29 19:35:02 +02:00
|
|
|
try loop.run();
|
|
|
|
defer loop.stop();
|
2024-01-19 03:17:26 +01:00
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
// Optionally enter the alternate screen
|
2024-01-19 17:58:14 +01:00
|
|
|
try vx.enterAltScreen();
|
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
// We'll adjust the color index every keypress
|
2024-01-20 02:43:35 +01:00
|
|
|
var color_idx: u8 = 0;
|
2024-01-19 18:30:00 +01:00
|
|
|
const msg = "Hello, world!";
|
2024-01-20 04:07:16 +01:00
|
|
|
|
|
|
|
// The main event loop. Vaxis provides a thread safe, blocking, buffered
|
|
|
|
// queue which can serve as the primary event queue for an application
|
2024-02-11 19:59:33 +01:00
|
|
|
while (true) {
|
2024-01-20 04:07:16 +01:00
|
|
|
// nextEvent blocks until an event is in the queue
|
2024-04-29 19:35:02 +02:00
|
|
|
const event = loop.nextEvent();
|
2024-02-11 19:59:33 +01:00
|
|
|
log.debug("event: {}", .{event});
|
|
|
|
// exhaustive switching ftw. Vaxis will send events if your Event
|
2024-01-20 04:07:16 +01:00
|
|
|
// enum has the fields for those events (ie "key_press", "winsize")
|
2024-01-19 03:17:26 +01:00
|
|
|
switch (event) {
|
|
|
|
.key_press => |key| {
|
2024-01-20 04:07:16 +01:00
|
|
|
color_idx = switch (color_idx) {
|
|
|
|
255 => 0,
|
|
|
|
else => color_idx + 1,
|
|
|
|
};
|
2024-01-19 03:17:26 +01:00
|
|
|
if (key.codepoint == 'c' and key.mods.ctrl) {
|
2024-02-11 19:59:33 +01:00
|
|
|
break;
|
2024-01-19 03:17:26 +01:00
|
|
|
}
|
|
|
|
},
|
2024-01-19 06:17:57 +01:00
|
|
|
.winsize => |ws| {
|
2024-01-19 19:21:14 +01:00
|
|
|
try vx.resize(alloc, ws);
|
2024-01-19 06:17:57 +01:00
|
|
|
},
|
2024-01-19 03:17:26 +01:00
|
|
|
else => {},
|
|
|
|
}
|
2024-01-19 17:31:23 +01:00
|
|
|
|
2024-01-20 04:07:16 +01:00
|
|
|
// vx.window() returns the root window. This window is the size of the
|
|
|
|
// terminal and can spawn child windows as logical areas. Child windows
|
|
|
|
// cannot draw outside of their bounds
|
2024-01-19 17:31:23 +01:00
|
|
|
const win = vx.window();
|
2024-01-20 04:07:16 +01:00
|
|
|
// Clear the entire space because we are drawing in immediate mode.
|
|
|
|
// vaxis double buffers the screen. This new frame will be compared to
|
|
|
|
// the old and only updated cells will be drawn
|
2024-01-19 20:13:20 +01:00
|
|
|
win.clear();
|
2024-01-20 04:07:16 +01:00
|
|
|
|
|
|
|
// Create some child window. .expand means the height and width will
|
|
|
|
// fill the remaining space of the parent. Child windows do not store a
|
|
|
|
// reference to their parent: this is true immediate mode. Do not store
|
|
|
|
// windows, always create new windows each render cycle
|
2024-01-19 19:21:14 +01:00
|
|
|
const child = win.initChild(win.width / 2 - msg.len / 2, win.height / 2, .expand, .expand);
|
2024-01-20 04:07:16 +01:00
|
|
|
// Loop through the message and print the cells to the screen
|
2024-01-19 18:30:00 +01:00
|
|
|
for (msg, 0..) |_, i| {
|
2024-01-20 02:43:35 +01:00
|
|
|
const cell: Cell = .{
|
2024-01-20 04:07:16 +01:00
|
|
|
// each cell takes a _grapheme_ as opposed to a single
|
|
|
|
// codepoint. This allows Vaxis to handle emoji properly,
|
|
|
|
// particularly with terminals that the Unicode Core extension
|
|
|
|
// (IE Mode 2027)
|
2024-01-20 02:43:35 +01:00
|
|
|
.char = .{ .grapheme = msg[i .. i + 1] },
|
2024-01-20 04:07:16 +01:00
|
|
|
.style = .{
|
|
|
|
.fg = .{ .index = color_idx },
|
|
|
|
},
|
2024-01-20 02:43:35 +01:00
|
|
|
};
|
2024-01-19 19:21:14 +01:00
|
|
|
child.writeCell(i, 0, cell);
|
2024-01-19 18:30:00 +01:00
|
|
|
}
|
2024-01-20 04:07:16 +01:00
|
|
|
// Render the screen
|
2024-01-19 18:30:00 +01:00
|
|
|
try vx.render();
|
2024-01-19 03:17:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-11 19:59:33 +01:00
|
|
|
// Our Event. This can contain internal events as well as Vaxis events.
|
2024-01-20 04:07:16 +01:00
|
|
|
// Internal events can be posted into the same queue as vaxis events to allow
|
|
|
|
// for a single event loop with exhaustive switching. Booya
|
2024-01-19 03:17:26 +01:00
|
|
|
const Event = union(enum) {
|
2024-01-19 17:13:32 +01:00
|
|
|
key_press: vaxis.Key,
|
|
|
|
winsize: vaxis.Winsize,
|
2024-01-20 15:05:22 +01:00
|
|
|
focus_in,
|
2024-01-19 18:30:00 +01:00
|
|
|
foo: u8,
|
2024-01-19 03:17:26 +01:00
|
|
|
};
|