feat(RawText): simple pager for a static file name

Layout is a simple pane without any restrictions, which should be
implemented next to see if the interfaces are stable and usable enough.
As for example the interface for `Widget.content()` has changed to
return a `[]u8` and not a `*std.ArrayList`.
This commit is contained in:
2024-11-07 21:28:52 +01:00
parent 2e93218b44
commit d9bcbcec7e
7 changed files with 476 additions and 49 deletions

View File

@@ -2,22 +2,23 @@ const std = @import("std");
const terminal = @import("../terminal.zig");
const isTaggedUnion = @import("../event.zig").isTaggedUnion;
const Error = @import("../event.zig").Error;
const Key = @import("../key.zig");
pub fn Layout(comptime Event: type) type {
if (!isTaggedUnion(Event)) {
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
}
const Widget = @import("../widget.zig").Widget(Event);
const Events = std.ArrayList(Event);
return struct {
widget: Widget = undefined,
events: std.ArrayList(Event) = undefined,
events: Events = undefined,
c: std.ArrayList(u8) = undefined,
size: terminal.Size = undefined,
pub fn init(allocator: std.mem.Allocator, widget: Widget) @This() {
return .{
.widget = widget,
.events = std.ArrayList(Event).init(allocator),
.events = Events.init(allocator),
.c = std.ArrayList(u8).init(allocator),
};
}
@@ -29,11 +30,8 @@ pub fn Layout(comptime Event: type) type {
this.* = undefined;
}
pub fn handle(this: *@This(), event: Event) !*std.ArrayList(Event) {
pub fn handle(this: *@This(), event: Event) !*Events {
switch (event) {
.resize => |size| {
this.size = size;
},
else => {},
}
this.events.clearRetainingCapacity();
@@ -46,7 +44,7 @@ pub fn Layout(comptime Event: type) type {
pub fn content(this: *@This()) !*std.ArrayList(u8) {
const widget_content = try this.widget.content();
this.c.clearRetainingCapacity();
try this.c.appendSlice(widget_content.items);
try this.c.appendSlice(widget_content);
return &this.c;
}
};