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

103
src/widget/Header.zig Normal file
View File

@@ -0,0 +1,103 @@
//! Header widget, which shows the name of the website and the main navigation entries
const std = @import("std");
const vaxis = @import("vaxis");
const widget = @import("../widget.zig");
const Event = widget.Event;
allocator: std.mem.Allocator = undefined,
unicode: *const vaxis.Unicode = undefined,
title: ?[]const u8 = undefined,
view: ?vaxis.widgets.View = undefined,
pub fn init(allocator: std.mem.Allocator, unicode: *const vaxis.Unicode) !@This() {
return .{
.allocator = allocator,
.unicode = unicode,
.title = null,
.view = null,
};
}
pub fn deinit(this: *@This()) void {
if (this.view) |*view| {
view.*.deinit();
}
if (this.title) |*title| {
this.allocator.free(title.*);
}
this.* = undefined;
}
fn fillView(this: *@This()) void {
this.view.?.clear();
const msg = "Yves Biener";
for (msg, 0..) |_, i| {
const cell: vaxis.Cell = .{
// 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)
.char = .{ .grapheme = msg[i .. i + 1] },
.style = .{
.fg = .{ .index = 6 },
.bold = true,
},
};
this.view.?.writeCell(i, 0, cell);
}
if (this.title) |title| {
// TODO: this could be a static string on the heap (due to the size of `513`)
std.log.debug("this.title := {s}({d}) - width: {d}", .{ title, title.len, this.view.?.screen.width });
for (title, 0.., this.view.?.screen.width / 2..) |_, i, col| {
const cell: vaxis.Cell = .{
.char = .{ .grapheme = title[i .. i + 1] },
.style = .{
.ul_style = .single,
},
};
this.view.?.writeCell(col, 0, cell);
}
}
}
/// Update loop for a given widget to react to the provided `Event`. It may
/// change its internal state, update variables, react to user input, etc.
pub fn update(this: *@This(), event: Event) void {
switch (event) {
.winsize => |ws| {
if (this.view) |*view| {
if (ws.rows != view.screen.width) {
view.*.deinit();
this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.rows, .height = ws.cols }) catch @panic("OOM");
this.fillView();
}
} else {
this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.rows, .height = ws.cols }) catch @panic("OOM");
this.fillView();
}
},
.title => |title| {
if (this.title) |*t| {
this.allocator.free(t.*);
}
const t = this.allocator.alloc(u8, title.len) catch @panic("OOM");
@memcpy(t, title);
this.title = t;
this.fillView();
},
else => {},
}
}
/// Draw a given widget using the provided `vaxis.Window`. The window controls
/// the dimension one widget may take on the screen. The widget itself has no
/// control over this.
pub fn draw(this: *@This(), win: vaxis.Window) void {
if (this.view) |*view| {
view.*.draw(win, .{});
}
}