zellzig/src/main.zig

67 lines
1.9 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");
/// comptime {
/// zz.createPlugin(@This());
/// }
///
/// pub fn init() void {
/// const alloc = std.heap.GeneralPurposeAllocator(.{}){};
/// zz.allocator = alloc.allocator();
/// }
/// pub fn update(event: zz.Event) void {}
/// 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 (Event) void)
@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() void {
if (allocator == null) {
@panic("Got event while allocator is null! Did you forget to set it?");
}
var ev = api.recvObj(types.Event) catch |err| {
std.log.err("Deserialize error: {}", .{err});
return;
};
defer ev.deinit();
Plugin.update(ev.data);
}
};
}