2024-01-22 02:12:46 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const vaxis = @import("vaxis");
|
|
|
|
const Cell = vaxis.Cell;
|
|
|
|
const TextInput = vaxis.widgets.TextInput;
|
|
|
|
const border = vaxis.widgets.border;
|
|
|
|
|
|
|
|
const log = std.log.scoped(.main);
|
2024-01-24 17:20:00 +01:00
|
|
|
|
2024-02-11 19:59:33 +01:00
|
|
|
// Our Event. This can contain internal events as well as Vaxis events.
|
2024-01-24 17:20:00 +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
|
|
|
|
const Event = union(enum) {
|
|
|
|
key_press: vaxis.Key,
|
2024-01-31 19:50:00 +01:00
|
|
|
mouse: vaxis.Mouse,
|
2024-01-24 17:20:00 +01:00
|
|
|
winsize: vaxis.Winsize,
|
|
|
|
focus_in,
|
2024-01-25 01:36:45 +01:00
|
|
|
focus_out,
|
2024-01-24 17:20:00 +01:00
|
|
|
foo: u8,
|
|
|
|
};
|
|
|
|
|
2024-01-22 02:12:46 +01: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) {
|
|
|
|
log.err("memory leak", .{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const alloc = gpa.allocator();
|
|
|
|
|
2024-05-29 18:33:39 +02:00
|
|
|
// Initalize a tty
|
|
|
|
var tty = try vaxis.Tty.init();
|
|
|
|
defer tty.deinit();
|
|
|
|
|
|
|
|
// Use a buffered writer for better performance. There are a lot of writes
|
|
|
|
// in the render loop and this can have a significant savings
|
|
|
|
var buffered_writer = tty.bufferedWriter();
|
|
|
|
const writer = buffered_writer.writer().any();
|
|
|
|
|
2024-04-29 19:26:50 +02:00
|
|
|
// Initialize Vaxis
|
2024-04-29 20:01:04 +02:00
|
|
|
var vx = try vaxis.init(alloc, .{});
|
2024-05-29 18:33:39 +02:00
|
|
|
defer vx.deinit(tty.anyWriter(), alloc);
|
2024-01-22 02:12:46 +01:00
|
|
|
|
2024-04-29 19:26:50 +02:00
|
|
|
var loop: vaxis.Loop(Event) = .{
|
|
|
|
.vaxis = &vx,
|
2024-05-29 18:33:39 +02:00
|
|
|
.tty = &tty,
|
2024-04-29 19:26:50 +02:00
|
|
|
};
|
2024-05-29 18:33:39 +02:00
|
|
|
try loop.init();
|
2024-04-29 19:26:50 +02:00
|
|
|
|
2024-01-22 02:12:46 +01:00
|
|
|
// Start the read loop. This puts the terminal in raw mode and begins
|
|
|
|
// reading user input
|
2024-05-29 18:33:39 +02:00
|
|
|
try loop.start();
|
2024-04-29 19:26:50 +02:00
|
|
|
defer loop.stop();
|
2024-01-22 02:12:46 +01:00
|
|
|
|
|
|
|
// Optionally enter the alternate screen
|
2024-05-29 18:33:39 +02:00
|
|
|
try vx.enterAltScreen(writer);
|
2024-01-22 02:12:46 +01:00
|
|
|
|
2024-01-22 18:19:28 +01:00
|
|
|
// We'll adjust the color index every keypress for the border
|
|
|
|
var color_idx: u8 = 0;
|
|
|
|
|
2024-01-24 17:20:00 +01:00
|
|
|
// init our text input widget. The text input widget needs an allocator to
|
|
|
|
// store the contents of the input
|
2024-04-29 20:01:04 +02:00
|
|
|
var text_input = TextInput.init(alloc, &vx.unicode);
|
2024-01-23 03:09:35 +01:00
|
|
|
defer text_input.deinit();
|
2024-01-22 02:12:46 +01:00
|
|
|
|
2024-01-24 17:20:00 +01:00
|
|
|
// Sends queries to terminal to detect certain features. This should
|
|
|
|
// _always_ be called, but is left to the application to decide when
|
2024-05-29 18:33:39 +02:00
|
|
|
// try vx.queryTerminal();
|
|
|
|
|
|
|
|
try vx.setMouseMode(writer, true);
|
2024-01-23 14:36:44 +01:00
|
|
|
|
2024-05-29 18:33:39 +02:00
|
|
|
try buffered_writer.flush();
|
2024-01-25 01:36:45 +01:00
|
|
|
|
2024-01-22 02:12:46 +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-22 02:12:46 +01:00
|
|
|
// nextEvent blocks until an event is in the queue
|
2024-04-29 19:26:50 +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-22 02:12:46 +01:00
|
|
|
// enum has the fields for those events (ie "key_press", "winsize")
|
|
|
|
switch (event) {
|
|
|
|
.key_press => |key| {
|
2024-01-22 18:21:02 +01:00
|
|
|
color_idx = switch (color_idx) {
|
|
|
|
255 => 0,
|
|
|
|
else => color_idx + 1,
|
|
|
|
};
|
2024-01-23 14:36:44 +01:00
|
|
|
if (key.matches('c', .{ .ctrl = true })) {
|
2024-02-11 19:59:33 +01:00
|
|
|
break;
|
2024-01-24 13:43:34 +01:00
|
|
|
} else if (key.matches('l', .{ .ctrl = true })) {
|
2024-01-23 14:36:44 +01:00
|
|
|
vx.queueRefresh();
|
2024-01-24 20:15:04 +01:00
|
|
|
} else if (key.matches('n', .{ .ctrl = true })) {
|
2024-05-29 18:33:39 +02:00
|
|
|
try vx.notify(tty.anyWriter(), "vaxis", "hello from vaxis");
|
2024-04-30 23:42:10 +02:00
|
|
|
loop.stop();
|
|
|
|
var child = std.process.Child.init(&.{"nvim"}, alloc);
|
|
|
|
_ = try child.spawnAndWait();
|
2024-05-29 18:33:39 +02:00
|
|
|
try loop.start();
|
|
|
|
try vx.enterAltScreen(tty.anyWriter());
|
2024-04-30 23:42:10 +02:00
|
|
|
vx.queueRefresh();
|
2024-03-19 19:24:17 +01:00
|
|
|
} else if (key.matches(vaxis.Key.enter, .{})) {
|
|
|
|
text_input.clearAndFree();
|
2024-01-24 13:43:34 +01:00
|
|
|
} else {
|
|
|
|
try text_input.update(.{ .key_press = key });
|
2024-01-23 14:36:44 +01:00
|
|
|
}
|
2024-01-22 02:12:46 +01:00
|
|
|
},
|
2024-01-24 13:43:34 +01:00
|
|
|
|
2024-01-24 17:20:00 +01:00
|
|
|
// winsize events are sent to the application to ensure that all
|
|
|
|
// resizes occur in the main thread. This lets us avoid expensive
|
|
|
|
// locks on the screen. All applications must handle this event
|
|
|
|
// unless they aren't using a screen (IE only detecting features)
|
|
|
|
//
|
|
|
|
// This is the only call that the core of Vaxis needs an allocator
|
|
|
|
// for. The allocations are because we keep a copy of each cell to
|
|
|
|
// optimize renders. When resize is called, we allocated two slices:
|
|
|
|
// one for the screen, and one for our buffered screen. Each cell in
|
|
|
|
// the buffered screen contains an ArrayList(u8) to be able to store
|
|
|
|
// the grapheme for that cell Each cell is initialized with a size
|
|
|
|
// of 1, which is sufficient for all of ASCII. Anything requiring
|
|
|
|
// more than one byte will incur an allocation on the first render
|
|
|
|
// after it is drawn. Thereafter, it will not allocate unless the
|
|
|
|
// screen is resized
|
2024-05-29 18:33:39 +02:00
|
|
|
.winsize => |ws| try vx.resize(alloc, ws, tty.anyWriter()),
|
2024-01-22 02:12:46 +01:00
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
const win = vx.window();
|
2024-01-24 17:20:00 +01:00
|
|
|
|
2024-01-22 02:12:46 +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
|
|
|
|
win.clear();
|
|
|
|
// draw the text_input using a bordered window
|
2024-01-22 18:19:28 +01:00
|
|
|
const style: vaxis.Style = .{
|
|
|
|
.fg = .{ .index = color_idx },
|
|
|
|
};
|
2024-03-26 17:41:54 +01:00
|
|
|
const child = win.child(.{
|
|
|
|
.x_off = win.width / 2 - 20,
|
|
|
|
.y_off = win.height / 2 - 3,
|
|
|
|
.width = .{ .limit = 40 },
|
|
|
|
.height = .{ .limit = 3 },
|
|
|
|
.border = .{
|
|
|
|
.where = .all,
|
|
|
|
.style = style,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
text_input.draw(child);
|
2024-01-22 02:12:46 +01:00
|
|
|
|
|
|
|
// Render the screen
|
2024-05-29 18:33:39 +02:00
|
|
|
try vx.render(writer);
|
|
|
|
try buffered_writer.flush();
|
2024-01-22 02:12:46 +01:00
|
|
|
}
|
|
|
|
}
|