Files
tui-website/src/widget/Header.zig

128 lines
3.9 KiB
Zig

//! 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,
path: ?[]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,
.path = null,
.view = null,
};
}
pub fn deinit(this: *@This()) void {
if (this.view) |*view| {
view.*.deinit();
}
if (this.path) |*path| {
this.allocator.free(path.*);
}
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 + 1, 0, cell);
}
if (this.path) |path| {
for (0..path.len, this.view.?.screen.width / 2 - path.len / 2..) |i, col| {
const cell: vaxis.Cell = .{
.char = .{ .grapheme = path[i .. i + 1] },
.style = .{
.ul_style = .single,
},
};
this.view.?.writeCell(col, 0, cell);
}
}
// github
{
const cell: vaxis.Cell = .{
.link = .{ .uri = "https://github.com/yves-biener" },
.char = .{ .grapheme = "github" },
.style = .{
.fg = .{ .index = 3 },
.ul_style = .single,
},
};
this.view.?.writeCell(this.view.?.screen.width - 9, 0, cell);
}
// mail
{
const cell: vaxis.Cell = .{
.link = .{ .uri = "mailto:yves.biener@gmx.de" },
.char = .{ .grapheme = "mail" },
.style = .{
.fg = .{ .index = 3 },
.ul_style = .single,
},
};
this.view.?.writeCell(this.view.?.screen.width - 16, 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.cols != view.screen.width) {
view.*.deinit();
this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.cols, .height = ws.rows }) catch @panic("OOM");
this.fillView();
}
} else {
this.view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = ws.cols, .height = ws.rows }) catch @panic("OOM");
this.fillView();
}
},
.path => |path| {
// TODO: try to remove the necessary amount of allocations
if (this.path) |*p| {
this.allocator.free(p.*);
}
const p = this.allocator.alloc(u8, path.len) catch @panic("OOM");
@memcpy(p, path);
this.path = p;
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, .{});
}
}