All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 39s
158 lines
4.8 KiB
Zig
158 lines
4.8 KiB
Zig
const std = @import("std");
|
|
const zterm = @import("zterm");
|
|
|
|
const App = zterm.App(union(enum) {
|
|
accept: []u21,
|
|
});
|
|
|
|
const log = std.log.scoped(.default);
|
|
|
|
const QuitText = struct {
|
|
const text = "Press ctrl+c to quit.";
|
|
|
|
pub fn element(this: *@This()) App.Element {
|
|
return .{ .ptr = this, .vtable = &.{ .content = content } };
|
|
}
|
|
|
|
fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void {
|
|
_ = ctx;
|
|
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
|
_ = origin;
|
|
|
|
const row = 2;
|
|
const col = size.x / 2 -| (text.len / 2);
|
|
const anchor = (row * size.x) + 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 InputField = struct {
|
|
input: std.ArrayList(u21),
|
|
queue: *App.Queue,
|
|
|
|
pub fn init(allocator: std.mem.Allocator, queue: *App.Queue) @This() {
|
|
return .{
|
|
.input = .init(allocator),
|
|
.queue = queue,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(this: @This()) void {
|
|
this.input.deinit();
|
|
}
|
|
|
|
pub fn element(this: *@This()) App.Element {
|
|
return .{
|
|
.ptr = this,
|
|
.vtable = &.{
|
|
.handle = handle,
|
|
.content = content,
|
|
},
|
|
};
|
|
}
|
|
|
|
fn handle(ctx: *anyopaque, event: App.Event) !void {
|
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
|
switch (event) {
|
|
.key => |key| {
|
|
if (key.isAscii()) try this.input.append(key.cp);
|
|
|
|
if (key.eql(.{ .cp = zterm.input.Enter }) or key.eql(.{ .cp = zterm.input.KpEnter }))
|
|
this.queue.push(.{ .accept = try this.input.toOwnedSlice() });
|
|
|
|
if (key.eql(.{ .cp = zterm.input.Backspace }) or key.eql(.{ .cp = zterm.input.Delete }) or key.eql(.{ .cp = zterm.input.KpDelete }))
|
|
_ = this.input.pop();
|
|
},
|
|
else => {},
|
|
}
|
|
}
|
|
|
|
fn content(ctx: *anyopaque, cells: []zterm.Cell, origin: zterm.Point, size: zterm.Point) !void {
|
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
|
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
|
_ = origin;
|
|
|
|
if (this.input.items.len == 0) return;
|
|
|
|
const row = 1;
|
|
const col = 1;
|
|
const anchor = (row * size.x) + col;
|
|
|
|
for (this.input.items, 0..) |cp, idx| {
|
|
cells[anchor + idx].style.fg = .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;
|
|
}
|
|
}
|
|
};
|
|
|
|
pub fn main() !void {
|
|
errdefer |err| log.err("Application Error: {any}", .{err});
|
|
|
|
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
|
|
defer if (gpa.deinit() == .leak) log.err("memory leak", .{});
|
|
|
|
const allocator = gpa.allocator();
|
|
|
|
var app: App = .init;
|
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
|
defer renderer.deinit();
|
|
|
|
var input_field: InputField = .init(allocator, &app.queue);
|
|
defer input_field.deinit();
|
|
|
|
var quit_text: QuitText = .{};
|
|
|
|
const element = input_field.element();
|
|
|
|
var container = try App.Container.init(allocator, .{
|
|
.rectangle = .{ .fill = .grey },
|
|
.layout = .{ .padding = .all(5) },
|
|
}, quit_text.element());
|
|
defer container.deinit();
|
|
|
|
try container.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = .light_grey } }, element));
|
|
|
|
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 => continue,
|
|
.quit => break,
|
|
.size => |size| try renderer.resize(size),
|
|
.key => |key| if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(),
|
|
.accept => |input| {
|
|
defer allocator.free(input);
|
|
log.info("Accepted input {any}", .{input});
|
|
},
|
|
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
|
else => {},
|
|
}
|
|
|
|
container.handle(event) catch |err| app.postEvent(.{
|
|
.err = .{
|
|
.err = err,
|
|
.msg = "Container Event handling failed",
|
|
},
|
|
});
|
|
|
|
try renderer.render(@TypeOf(container), &container);
|
|
try renderer.flush();
|
|
}
|
|
}
|