add(header): header with configurable title
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 31s

This commit is contained in:
2024-10-07 22:04:29 +02:00
parent 6fea65a359
commit 5038c92ab4
3 changed files with 194 additions and 34 deletions

View File

@@ -3,15 +3,13 @@ const std = @import("std");
const vaxis = @import("vaxis");
const zlog = @import("zlog");
const widget = @import("widget.zig");
const TextInput = vaxis.widgets.TextInput;
const Event = widget.Event;
pub const std_options = zlog.std_options;
const Event = union(enum) {
key_press: vaxis.Key,
winsize: vaxis.Winsize,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
@@ -60,15 +58,18 @@ pub fn main() !void {
var text_input = TextInput.init(alloc, &vx.unicode);
defer text_input.deinit();
var header = try widget.Header.init(alloc, &vx.unicode);
defer header.deinit();
// Sends queries to terminal to detect certain features. This should always
// be called after entering the alt screen, if you are using the alt screen
try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
while (true) {
// nextEvent blocks until an event is in the queue
const event = loop.nextEvent();
// exhaustive switching ftw. Vaxis will send events if your Event enum
// has the fields for those events (ie "key_press", "winsize")
// update widgets
header.update(event);
switch (event) {
.key_press => |key| {
color_idx = switch (color_idx) {
@@ -79,37 +80,32 @@ pub fn main() !void {
break;
} else if (key.matches('l', .{ .ctrl = true })) {
vx.queueRefresh();
} else if (key.matches(vaxis.Key.enter, .{})) {
const title = alloc.alloc(u8, text_input.buf.buffer.len) catch @panic("OOM");
@memcpy(title, text_input.buf.buffer);
if (loop.tryPostEvent(.{ .title = title })) {
text_input.clearAndFree();
}
} else {
try text_input.update(.{ .key_press = key });
}
},
// 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)
//
// 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
.winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws),
.title => |*title| {
alloc.free(title.*);
},
}
// 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();
var root_window = vx.window();
root_window.clear();
try vx.render(tty.anyWriter()); // re-draw after clear!
// 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();
header.draw(root_window.child(.{
.x_off = 0,
.y_off = 0,
.height = .{ .limit = 2 },
.border = .{ .where = .bottom },
}));
// Create a style
const style: vaxis.Style = .{
@@ -117,9 +113,9 @@ pub fn main() !void {
};
// Create a bordered child window
const child = win.child(.{
.x_off = win.width / 2 - 20,
.y_off = win.height / 2 - 3,
const child = root_window.child(.{
.x_off = root_window.width / 2 - 20,
.y_off = root_window.height / 2 - 3,
.width = .{ .limit = 40 },
.height = .{ .limit = 3 },
.border = .{