mod(tui): create own terminal interface framework

This commit is contained in:
2024-11-04 22:27:45 +01:00
parent 0330b3a2f5
commit 14aab9ef50
9 changed files with 559 additions and 96 deletions

View File

@@ -2,37 +2,42 @@ const std = @import("std");
const lib_event = @import("../event.zig");
const widget = @import("../widget.zig");
pub fn Layout(comptime E: type) type {
if (!lib_event.isTaggedUnion(E)) {
@compileError("Provided user event `E` for `Layout(comptime E: type)` is not of type `union(enum)`.");
pub fn Layout(comptime Event: type) type {
if (!lib_event.isTaggedUnion(Event)) {
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
}
return struct {
pub const Event = lib_event.MergeTaggedUnions(lib_event.BuiltinEvent, E);
w: widget.Widget(E) = undefined,
w: widget.Widget(Event) = undefined,
events: std.ArrayList(Event) = undefined,
c: std.ArrayList(u8) = undefined,
pub fn init(allocator: std.mem.Allocator, w: widget.Widget(E)) @This() {
pub fn init(allocator: std.mem.Allocator, w: widget.Widget(Event)) @This() {
return .{
.w = w,
.events = std.ArrayList(Event).init(allocator),
.c = std.ArrayList(u8).init(allocator),
};
}
pub fn deinit(this: *@This()) void {
this.w.deinit();
this.events.deinit();
this.c.deinit();
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) Event {
return this.w.handle(event);
pub fn handle(this: *@This(), event: Event) !*std.ArrayList(Event) {
this.events.clearRetainingCapacity();
if (this.w.handle(event)) |e| {
try this.events.append(e);
}
return &this.events;
}
pub fn content(this: *@This()) *std.ArrayList(u8) {
const widget_content = this.w.content();
pub fn content(this: *@This()) !*std.ArrayList(u8) {
const widget_content = try this.w.content();
this.c.clearRetainingCapacity();
this.c.appendSlice(widget_content.items) catch @panic("OOM");
try this.c.appendSlice(widget_content.items);
return &this.c;
}
};