All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 43s
156 lines
5.1 KiB
Zig
156 lines
5.1 KiB
Zig
const std = @import("std");
|
|
const zterm = @import("zterm");
|
|
|
|
const input = zterm.input;
|
|
const App = zterm.App(union(enum) {});
|
|
|
|
const log = std.log.scoped(.default);
|
|
|
|
pub const HelloWorldText = packed struct {
|
|
const text = "Hello World";
|
|
|
|
pub fn element(this: *@This()) App.Element {
|
|
return .{
|
|
.ptr = this,
|
|
.vtable = &.{ .content = content },
|
|
};
|
|
}
|
|
|
|
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));
|
|
|
|
// NOTE: error should only be returned here in case an in-recoverable exception has occurred
|
|
const row = size.rows / 2;
|
|
const col = size.cols / 2 -| (text.len / 2);
|
|
const anchor = (row * size.rows) + col;
|
|
|
|
for (0.., text) |idx, char| {
|
|
cells[anchor + idx].style.fg = .black;
|
|
cells[anchor + idx].cp = char;
|
|
}
|
|
}
|
|
};
|
|
|
|
pub fn main() !void {
|
|
errdefer |err| log.err("Application Error: {any}", .{err});
|
|
|
|
// TODO: maybe create own allocator as some sort of arena allocator to have consistent memory usage
|
|
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
|
|
defer {
|
|
const deinit_status = gpa.deinit();
|
|
if (deinit_status == .leak) {
|
|
log.err("memory leak", .{});
|
|
}
|
|
}
|
|
const allocator = gpa.allocator();
|
|
|
|
var app: App = .init;
|
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
|
defer renderer.deinit();
|
|
|
|
var element_wrapper: HelloWorldText = .{};
|
|
const element = element_wrapper.element();
|
|
|
|
var box = try App.Container.init(allocator, .{
|
|
.rectangle = .{ .fill = .blue },
|
|
.layout = .{
|
|
.gap = 1,
|
|
.direction = .vertical,
|
|
.padding = .vertical(1),
|
|
},
|
|
.min_size = .{ .rows = 50 },
|
|
}, .{});
|
|
try box.append(try App.Container.init(allocator, .{
|
|
.rectangle = .{ .fill = .light_green },
|
|
}, .{}));
|
|
try box.append(try App.Container.init(allocator, .{
|
|
.rectangle = .{ .fill = .light_green },
|
|
}, element));
|
|
try box.append(try App.Container.init(allocator, .{
|
|
.rectangle = .{ .fill = .light_green },
|
|
}, .{}));
|
|
defer box.deinit();
|
|
|
|
var scrollable: App.Scrollable = .{
|
|
.container = box,
|
|
.vertical = true,
|
|
};
|
|
|
|
var container = try App.Container.init(allocator, .{
|
|
.border = .{
|
|
.separator = .{
|
|
.enabled = true,
|
|
.line = .double,
|
|
},
|
|
},
|
|
.layout = .{
|
|
.gap = 2,
|
|
.padding = .all(5),
|
|
.direction = .horizontal,
|
|
},
|
|
}, .{});
|
|
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
|
|
|
|
try container.append(try App.Container.init(allocator, .{
|
|
.border = .{
|
|
.color = .light_blue,
|
|
.sides = .vertical,
|
|
},
|
|
}, .{}));
|
|
try container.append(try App.Container.init(allocator, .{
|
|
.rectangle = .{ .fill = .blue },
|
|
}, .{}));
|
|
defer container.deinit(); // also de-initializes the children
|
|
|
|
try app.start();
|
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
|
|
|
// event loop
|
|
while (true) {
|
|
const event = app.nextEvent();
|
|
log.debug("received event: {s}", .{@tagName(event)});
|
|
|
|
switch (event) {
|
|
.init => {
|
|
try container.handle(event);
|
|
continue; // do not render
|
|
},
|
|
.quit => break,
|
|
.resize => |size| try renderer.resize(size),
|
|
.key => |key| {
|
|
if (key.eql(.{ .cp = 'q' })) app.quit();
|
|
|
|
// corresponding element could even be changed from the 'outside' (not forced through the event system)
|
|
// event system however allows for cross element communication (i.e. broadcasting messages, etc.)
|
|
if (key.eql(.{ .cp = input.Escape })) element_wrapper.text_color = .black;
|
|
|
|
if (key.eql(.{ .cp = 'n', .mod = .{ .ctrl = true } })) {
|
|
try app.interrupt();
|
|
defer app.start() catch @panic("could not start app event loop");
|
|
var child = std.process.Child.init(&.{"hx"}, allocator);
|
|
_ = child.spawnAndWait() catch |err| app.postEvent(.{
|
|
.err = .{
|
|
.err = err,
|
|
.msg = "Spawning $EDITOR failed",
|
|
},
|
|
});
|
|
}
|
|
},
|
|
// NOTE: errors could be displayed in another container in case one was received, etc. to provide the user with feedback
|
|
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
|
else => {},
|
|
}
|
|
|
|
// NOTE: returned errors should be propagated back to the application
|
|
container.handle(event) catch |err| app.postEvent(.{
|
|
.err = .{
|
|
.err = err,
|
|
.msg = "Container Event handling failed",
|
|
},
|
|
});
|
|
try renderer.render(@TypeOf(container), &container);
|
|
try renderer.flush();
|
|
}
|
|
}
|