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

@@ -1,7 +1,7 @@
//! Dynamic dispatch for layout implementations.
//! Each layout should at last implement these functions:
//! - handle(this: *@This(), event: Event) Event {}
//! - content(this: *@This()) *std.ArrayList(u8) {}
//! - handle(this: *@This(), event: Event) anyerror!*std.ArrayList(Event) {}
//! - content(this: *@This()) anyerror!*std.ArrayList(u8) {}
//! - deinit(this: *@This()) void {}
//!
//! Create a `Layout` using `createFrom(object: anytype)` and use them through
@@ -14,18 +14,17 @@
const std = @import("std");
const lib_event = @import("event.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);
const LayoutType = @This();
const Ptr = usize;
const VTable = struct {
handle: *const fn (this: *LayoutType, event: Event) Event,
content: *const fn (this: *LayoutType) *std.ArrayList(u8),
handle: *const fn (this: *LayoutType, event: Event) anyerror!*std.ArrayList(Event),
content: *const fn (this: *LayoutType) anyerror!*std.ArrayList(u8),
deinit: *const fn (this: *LayoutType) void,
};
@@ -33,13 +32,13 @@ pub fn Layout(comptime E: type) type {
vtable: *const VTable = undefined,
// Handle the provided `Event` for this `Widget`.
pub fn handle(this: *LayoutType, event: Event) Event {
return this.vtable.handle(this, event);
pub fn handle(this: *LayoutType, event: Event) !*std.ArrayList(Event) {
return try this.vtable.handle(this, event);
}
// Return the entire content of this `Widget`.
pub fn content(this: *LayoutType) *std.ArrayList(u8) {
return this.vtable.content(this);
pub fn content(this: *LayoutType) !*std.ArrayList(u8) {
return try this.vtable.content(this);
}
pub fn deinit(this: *LayoutType) void {
@@ -53,16 +52,16 @@ pub fn Layout(comptime E: type) type {
.vtable = &.{
.handle = struct {
// Handle the provided `Event` for this `Widget`.
fn handle(this: *LayoutType, event: Event) Event {
fn handle(this: *LayoutType, event: Event) !*std.ArrayList(Event) {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
return layout.handle(event);
return try layout.handle(event);
}
}.handle,
.content = struct {
// Return the entire content of this `Widget`.
fn content(this: *LayoutType) *std.ArrayList(u8) {
fn content(this: *LayoutType) !*std.ArrayList(u8) {
const layout: @TypeOf(object) = @ptrFromInt(this.object);
return layout.content();
return try layout.content();
}
}.content,
.deinit = struct {
@@ -76,6 +75,6 @@ pub fn Layout(comptime E: type) type {
}
// import and export of `Layout` implementations
pub const Pane = @import("layout/Pane.zig").Layout(E);
pub const Pane = @import("layout/Pane.zig").Layout(Event);
};
}