add(main): main executable with zig build run support
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (pull_request) Failing after 5m10s
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (pull_request) Failing after 5m10s
The main executable contains a simple `Element` implementation example showing how interaction through and outside of the event loop can be implemented to impact the rendered contents.
This commit is contained in:
27
build.zig
27
build.zig
@@ -17,6 +17,19 @@ pub fn build(b: *std.Build) void {
|
|||||||
});
|
});
|
||||||
lib.addImport("code_point", zg.module("code_point"));
|
lib.addImport("code_point", zg.module("code_point"));
|
||||||
|
|
||||||
|
// main executable (usually used for testing)
|
||||||
|
const exe = b.addExecutable(.{
|
||||||
|
.name = "zterm",
|
||||||
|
.root_source_file = b.path("src/main.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
exe.root_module.addImport("zterm", lib);
|
||||||
|
|
||||||
|
// TODO: add example execution through optional argument to `zig run` to run
|
||||||
|
// an example application instead of the main executable
|
||||||
|
|
||||||
|
// example applications (usually used for documentation and demonstrations)
|
||||||
const container = b.addExecutable(.{
|
const container = b.addExecutable(.{
|
||||||
.name = "container",
|
.name = "container",
|
||||||
.root_source_file = b.path("examples/container.zig"),
|
.root_source_file = b.path("examples/container.zig"),
|
||||||
@@ -26,7 +39,19 @@ pub fn build(b: *std.Build) void {
|
|||||||
container.root_module.addImport("zterm", lib);
|
container.root_module.addImport("zterm", lib);
|
||||||
b.installArtifact(container);
|
b.installArtifact(container);
|
||||||
|
|
||||||
// testing
|
// zig build run
|
||||||
|
const run_cmd = b.addRunArtifact(exe);
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
// Allow additional arguments, like this: `zig build run -- arg1 arg2 etc`
|
||||||
|
if (b.args) |args| run_cmd.addArgs(args);
|
||||||
|
|
||||||
|
// This creates a build step. It will be visible in the `zig build --help` menu,
|
||||||
|
// and can be selected like this: `zig build run`
|
||||||
|
// This will evaluate the `run` step rather than the default, which is "install".
|
||||||
|
const run_step = b.step("run", "Run the app");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
// zig build test
|
||||||
const lib_unit_tests = b.addTest(.{
|
const lib_unit_tests = b.addTest(.{
|
||||||
.root_source_file = b.path("src/zterm.zig"),
|
.root_source_file = b.path("src/zterm.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
|
|||||||
171
src/main.zig
Normal file
171
src/main.zig
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const zterm = @import("zterm");
|
||||||
|
|
||||||
|
const App = zterm.App(union(enum) {});
|
||||||
|
const Key = zterm.Key;
|
||||||
|
|
||||||
|
const log = std.log.scoped(.default);
|
||||||
|
|
||||||
|
pub const HelloWorldText = packed struct {
|
||||||
|
const text = "Hello World";
|
||||||
|
|
||||||
|
text_color: zterm.Color = .black,
|
||||||
|
|
||||||
|
// example function to create the interface instance for this `Element` implementation
|
||||||
|
pub fn element(this: *@This()) App.Element {
|
||||||
|
return .{
|
||||||
|
.ptr = this,
|
||||||
|
.vtable = &.{
|
||||||
|
.handle = handle,
|
||||||
|
.content = content,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// example function to render contents for a `Container`
|
||||||
|
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(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 occured
|
||||||
|
const row = size.rows / 2;
|
||||||
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
|
|
||||||
|
for (0.., text) |idx, char| {
|
||||||
|
cells[(row * size.cols) + col + idx].style.fg = this.text_color;
|
||||||
|
cells[(row * size.cols) + col + idx].cp = char;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// example function to handle events for a `Container`
|
||||||
|
fn handle(ctx: *anyopaque, event: App.Event) !void {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
|
switch (event) {
|
||||||
|
.init => log.debug(".init event", .{}),
|
||||||
|
.key => |key| {
|
||||||
|
if (key.matches(.{ .cp = zterm.Key.space })) {
|
||||||
|
var next_color_idx = @intFromEnum(this.text_color);
|
||||||
|
next_color_idx += 1;
|
||||||
|
next_color_idx %= 17; // iterate over the first 16 colors (but exclude `.default` == 0)
|
||||||
|
if (next_color_idx == 0) next_color_idx += 1;
|
||||||
|
this.text_color = @enumFromInt(next_color_idx);
|
||||||
|
log.debug("Next color: {s}", .{@tagName(this.text_color)});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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 container = try App.Container.init(allocator, .{
|
||||||
|
.border = .{
|
||||||
|
.separator = .{
|
||||||
|
.enabled = true,
|
||||||
|
.line = .double,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
.layout = .{
|
||||||
|
.gap = 2,
|
||||||
|
.padding = .all(5),
|
||||||
|
.direction = .vertical,
|
||||||
|
},
|
||||||
|
}, .{});
|
||||||
|
var box = try App.Container.init(allocator, .{
|
||||||
|
.rectangle = .{ .fill = .blue },
|
||||||
|
.layout = .{
|
||||||
|
.gap = 1,
|
||||||
|
.direction = .vertical,
|
||||||
|
.padding = .vertical(1),
|
||||||
|
},
|
||||||
|
}, .{});
|
||||||
|
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 },
|
||||||
|
}, .{}));
|
||||||
|
try container.append(box);
|
||||||
|
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.matches(.{ .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.matches(.{ .cp = zterm.Key.escape })) element_wrapper.text_color = .black;
|
||||||
|
|
||||||
|
if (key.matches(.{ .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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user