test: streamline examples with quit texts
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 24s

Improve some examples to provide visual feedback, i.e. for the button
exmample, with fixes to make them compilable with the `Scrollable`
element changes.
This commit is contained in:
2025-03-01 17:12:28 +01:00
parent af443c6bbf
commit caee008d50
5 changed files with 110 additions and 21 deletions

View File

@@ -6,7 +6,33 @@ const App = zterm.App(union(enum) {});
const log = std.log.scoped(.default);
pub const HelloWorldText = packed struct {
const QuitText = struct {
const text = "Press ctrl+c to quit.";
pub fn element(this: *@This()) App.Element {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
pub fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
_ = ctx;
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
const row = 2;
const col = size.cols / 2 -| (text.len / 2);
const anchor = (row * size.cols) + col;
for (text, 0..) |cp, idx| {
cells[anchor + idx].style.fg = .white;
cells[anchor + idx].style.bg = .black;
cells[anchor + idx].cp = cp;
// NOTE: do not write over the contents of this `Container`'s `Size`
if (anchor + idx == cells.len - 1) break;
}
}
};
const HelloWorldText = packed struct {
const text = "Hello World";
pub fn element(this: *@This()) App.Element {
@@ -52,6 +78,8 @@ pub fn main() !void {
var element_wrapper: HelloWorldText = .{};
const element = element_wrapper.element();
var quit_text: QuitText = .{};
var top_box = try App.Container.init(allocator, .{
.rectangle = .{ .fill = .blue },
.layout = .{
@@ -59,7 +87,6 @@ pub fn main() !void {
.direction = .vertical,
.padding = .vertical(1),
},
.min_size = .{ .rows = 50 },
}, .{});
try top_box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_green },
@@ -85,7 +112,6 @@ pub fn main() !void {
.direction = .vertical,
.padding = .vertical(1),
},
.min_size = .{ .rows = 30 },
}, .{});
try bottom_box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .grey },
@@ -105,17 +131,17 @@ pub fn main() !void {
.enabled = true,
.line = .double,
},
.padding = .all(5),
.padding = .{ .top = 5, .bottom = 3, .left = 3, .right = 3 },
.direction = .vertical,
},
}, .{});
}, quit_text.element());
defer container.deinit();
// place empty container containing the element of the scrollable Container.
var scrollable_top: App.Scrollable = .init(top_box);
var scrollable_top: App.Scrollable = .init(top_box, .{ .rows = 50 });
try container.append(try App.Container.init(allocator, .{}, scrollable_top.element()));
var scrollable_bottom: App.Scrollable = .init(bottom_box);
var scrollable_bottom: App.Scrollable = .init(bottom_box, .{ .rows = 30 });
try container.append(try App.Container.init(allocator, .{}, scrollable_bottom.element()));
try app.start();