zellzig/src/main.zig

75 lines
2.0 KiB
Zig

const std = @import("std");
pub const types = @import("types.zig");
pub const Event = types.Event;
pub const OwnedDeserData = types.OwnedDeserData;
pub const api = @import("api.zig");
/// This is the allocator that will be used by zellzig for communication.
/// This must be set before events can be received.
pub var allocator: ?std.mem.Allocator = null;
test {
std.testing.refAllDecls(@This());
}
/// Creates a plugin that can be called into by zellij. This function should take a
/// struct type with the functions of your plugin. Usage of this function should
/// look like this:
///
/// ```zig
/// // main.zig
/// const zz = @import("zellzig");
///
/// const gpa = std.heap.GeneralPurposeAllocator(.{}){};
///
/// comptime {
/// zz.createPlugin(@This());
/// }
///
/// pub const zellij_version = "0.34.0";
///
/// pub fn init() void {}
/// pub fn update(event: zz.Event) bool {
/// var event = zz.getEvent(gpa.allocator()) catch return;
/// defer event.deinit();
///
/// // use event
///
/// // return true to request render
/// return true;
/// }
/// pub fn render(rows: i32, cols: i32) void {}
/// ```
pub fn createPlugin(comptime Plugin: type) void {
if (@TypeOf(Plugin.init) != fn () void)
@compileError("Function 'init' has invalid signature!");
if (@TypeOf(Plugin.update) != fn () bool)
@compileError("Function 'update' has invalid signature!");
if (@TypeOf(Plugin.render) != fn (i32, i32) void)
@compileError("Function 'render' has invalid signature!");
_ = struct {
export fn _start() void {
Plugin.init();
}
export fn render(rows: i32, cols: i32) void {
Plugin.render(rows, cols);
}
export fn update() bool {
return Plugin.update();
}
export fn plugin_version() void {
std.io.getStdOut().writeAll(Plugin.zellij_version ++ "\n") catch unreachable;
}
};
}
pub fn getEvent(alloc: std.mem.Allocator) !OwnedDeserData(types.Event) {
return try api.recvObj(types.Event, alloc);
}