feat(content): scrollable contents; bump zterm dependency
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m23s

With functional increments in `zterm`'s `Scrollable` `Element`, an
implementation (such as `Content`) can provide a size hint for the
minimal required size to dynamically change its dimensions for the
scrollable `Container` in the `Scrollable` `Element`.
This commit is contained in:
2025-11-01 14:44:05 +01:00
parent 8ac6c16289
commit 74bf941820
4 changed files with 47 additions and 10 deletions

View File

@@ -6,8 +6,8 @@
.minimum_zig_version = "0.16.0-dev.463+f624191f9",
.dependencies = .{
.zterm = .{
.url = "git+https://gitea.yves-biener.de/yves-biener/zterm#c645c2efee5ff57fa6fb2b9f9155cf21723bf085",
.hash = "zterm-0.3.0-1xmmEMbzGwD3MiXzgXWwnTEFIjvtWTcQrBLqYS5O00ee",
.url = "git+https://gitea.yves-biener.de/yves-biener/zterm#7cd1fb139fbf45a3d99f8359dda0d198a283249a",
.hash = "zterm-0.3.0-1xmmEPf4GwA0W_Aj0yFGfg4efODoLB3y72LAprcAJwwe",
},
.zlog = .{
.url = "git+https://gitea.yves-biener.de/yves-biener/zlog#411a6dc358a3ef463ab704e2f6b887a019a5decf",

View File

@@ -1,27 +1,62 @@
pub fn Content(App: type) type {
return struct {
page: App.Model.Pages,
pub fn init(allocator: Allocator) @This() {
_ = allocator;
return .{};
return .{
.page = .blog,
};
}
pub fn deinit(this: *@This()) void {
this.container.deinit();
}
pub fn element(this: *@This()) App.Element {
return .{
.ptr = this,
.vtable = &.{
.minSize = minSize,
.handle = handle,
.content = content,
},
};
}
fn minSize(ctx: *anyopaque, size: zterm.Point) zterm.Point {
const this: *const @This() = @ptrCast(@alignCast(ctx));
const text = switch (this.page) {
.about => @embedFile("about"),
.blog => @embedFile("blog"),
};
var index: usize = 0;
var new_size: zterm.Point = .{ .x = size.x };
for (0..text.len) |_| rows: {
for (0..size.x) |_| {
if (index == text.len) break :rows;
const cp = text[index];
index += 1;
switch (cp) {
'\n' => break,
else => {},
}
}
new_size.y += 1;
}
return new_size;
}
fn handle(ctx: *anyopaque, model: *App.Model, event: App.Event) !void {
_ = ctx;
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.about => model.page = .about,
.blog => model.page = .blog,
else => {},
}
this.page = model.page;
}
fn content(ctx: *anyopaque, model: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
@@ -37,7 +72,6 @@ pub fn Content(App: type) type {
for (0..size.y) |row| {
for (0..size.x) |col| {
const cell = row * size.x + col;
assert(cell < cells.len);
if (index == text.len) return;
const cp = text[index];

View File

@@ -59,13 +59,16 @@ pub fn main() !void {
}
// main actual tui_website page content
{
// intermediate container for *padding*
var content: Content = .init(allocator);
var content_container: App.Container = try .init(allocator, .{
const content_container: App.Container = try .init(allocator, .{}, content.element());
var scrollable: App.Scrollable = .init(content_container, .enabled(.green));
// intermediate container for *padding* containing the scrollable `Content`
var scrollable_container: App.Container = try .init(allocator, .{
.layout = .{ .padding = .horizontal(2) },
}, .{});
try content_container.append(try .init(allocator, .{}, content.element()));
try container.append(content_container);
try scrollable_container.append(try .init(allocator, .{}, scrollable.element()));
try container.append(scrollable_container);
}
// footer
{

View File

@@ -1,6 +1,6 @@
page: Pages = .blog,
const Pages = enum {
pub const Pages = enum {
about,
blog,
};