Files
tui-website/src/widget/PopupMenu.zig
Yves Biener a3d0b7af9a
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 29s
mod(node2buffer): add styling to parsed contents
2024-10-13 16:39:51 +02:00

86 lines
2.4 KiB
Zig

//! Pop-up Menu widget to show the available keybindings
const std = @import("std");
const vaxis = @import("vaxis");
const Zmd = @import("zmd").Zmd;
const node2buffer = @import("node2buffer.zig");
const widget = @import("../widget.zig");
const Event = widget.Event;
allocator: std.mem.Allocator = undefined,
unicode: *const vaxis.Unicode = undefined,
pub fn init(allocator: std.mem.Allocator, unicode: *const vaxis.Unicode) @This() {
return .{
.allocator = allocator,
.unicode = unicode,
};
}
pub fn deinit(this: *@This()) void {
this.* = undefined;
}
/// 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) ?Event {
_ = this;
switch (event) {
.key_press => |key| {
if (key.matches('a', .{})) {
// About
return .{ .path = "./doc/about.md" };
}
if (key.matches('h', .{})) {
// Home
return .{ .path = "./doc/home.md" };
}
if (key.matches('t', .{})) {
// test
return .{ .path = "./doc/test.md" };
}
},
else => {},
}
return null;
}
/// 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 {
var view = vaxis.widgets.View.init(this.allocator, this.unicode, .{ .width = win.width, .height = win.height }) catch @panic("OOM");
defer view.deinit();
const msg =
\\# Goto
\\
\\**a** about
\\**h** home
\\**t** test
;
var zmd = Zmd.init(this.allocator);
defer zmd.deinit();
var cells = std.ArrayList(vaxis.Cell).init(this.allocator);
defer cells.deinit();
zmd.parse(msg) catch @panic("failed to parse markdown file");
node2buffer.toBuffer(zmd.nodes.items[0], this.allocator, msg, &cells, .{}, null) catch @panic("failed to transform to cell array");
var col: usize = 0;
var row: usize = 0;
for (cells.items) |cell| {
view.writeCell(col, row, cell);
if (std.mem.eql(u8, cell.char.grapheme, "\n")) {
col = 0;
row += 1;
} else {
col += 1;
}
}
view.draw(win, .{});
}