refactor: move command and EventHandler out of the tui module

This commit is contained in:
CJ van den Berg 2024-10-25 22:39:04 +02:00
parent f41fb97d02
commit 16c5471126
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
45 changed files with 132 additions and 81 deletions

View file

@ -1,6 +1,7 @@
const std = @import("std");
const tp = @import("thespian");
const EventHandler = @import("EventHandler");
const Plane = @import("renderer").Plane;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
@ -22,7 +23,7 @@ pub fn Options(context: type) type {
on_render: *const fn (ctx: *context, button: *State(Context), theme: *const Widget.Theme) bool = on_render_default,
on_layout: *const fn (ctx: *context, button: *State(Context)) Widget.Layout = on_layout_default,
on_receive: *const fn (ctx: *context, button: *State(Context), from: tp.pid_ref, m: tp.message) error{Exit}!bool = on_receive_default,
on_event: ?Widget.EventHandler = null,
on_event: ?EventHandler = null,
pub const Context = context;
pub fn do_nothing(_: *context, _: *State(Context)) void {}

View file

@ -1,175 +0,0 @@
const std = @import("std");
const tp = @import("thespian");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Self = @This();
const EventHandler = Self;
ptr: *anyopaque,
vtable: *const VTable,
pub const VTable = struct {
deinit: *const fn (ctx: *anyopaque) void,
send: *const fn (ctx: *anyopaque, from: tp.pid_ref, m: tp.message) tp.result,
type_name: []const u8,
};
pub fn to_owned(pimpl: anytype) Self {
const impl = @typeInfo(@TypeOf(pimpl));
const child: type = impl.Pointer.child;
return .{
.ptr = pimpl,
.vtable = comptime &.{
.type_name = @typeName(child),
.deinit = struct {
pub fn deinit(ctx: *anyopaque) void {
return child.deinit(@as(*child, @ptrCast(@alignCast(ctx))));
}
}.deinit,
.send = struct {
pub fn receive(ctx: *anyopaque, from_: tp.pid_ref, m: tp.message) tp.result {
_ = try child.receive(@as(*child, @ptrCast(@alignCast(ctx))), from_, m);
}
}.receive,
},
};
}
pub fn to_unowned(pimpl: anytype) Self {
const impl = @typeInfo(@TypeOf(pimpl));
const child: type = impl.Pointer.child;
return .{
.ptr = pimpl,
.vtable = comptime &.{
.type_name = @typeName(child),
.deinit = struct {
pub fn deinit(_: *anyopaque) void {}
}.deinit,
.send = if (@hasDecl(child, "send")) struct {
pub fn send(ctx: *anyopaque, from_: tp.pid_ref, m: tp.message) tp.result {
_ = try child.send(@as(*child, @ptrCast(@alignCast(ctx))), from_, m);
}
}.send else struct {
pub fn receive(ctx: *anyopaque, from_: tp.pid_ref, m: tp.message) tp.result {
_ = try child.receive(@as(*child, @ptrCast(@alignCast(ctx))), from_, m);
}
}.receive,
},
};
}
pub fn bind(pimpl: anytype, comptime f: *const fn (ctx: @TypeOf(pimpl), from: tp.pid_ref, m: tp.message) tp.result) Self {
const impl = @typeInfo(@TypeOf(pimpl));
const child: type = impl.Pointer.child;
return .{
.ptr = pimpl,
.vtable = comptime &.{
.type_name = @typeName(child),
.deinit = struct {
pub fn deinit(_: *anyopaque) void {}
}.deinit,
.send = struct {
pub fn receive(ctx: *anyopaque, from_: tp.pid_ref, m: tp.message) tp.result {
return @call(.auto, f, .{ @as(*child, @ptrCast(@alignCast(ctx))), from_, m });
}
}.receive,
},
};
}
pub fn deinit(self: Self) void {
return self.vtable.deinit(self.ptr);
}
pub fn dynamic_cast(self: Self, comptime T: type) ?*T {
return if (std.mem.eql(u8, self.vtable.type_name, @typeName(T)))
@as(*T, @ptrCast(@alignCast(self.ptr)))
else
null;
}
pub fn msg(self: Self, m: anytype) tp.result {
return self.vtable.send(self.ptr, tp.self_pid(), tp.message.fmt(m));
}
pub fn send(self: Self, from_: tp.pid_ref, m: tp.message) tp.result {
return self.vtable.send(self.ptr, from_, m);
}
pub fn empty(allocator: Allocator) !Self {
const child: type = struct {};
const widget = try allocator.create(child);
widget.* = .{};
return .{
.ptr = widget,
.plane = &widget.plane,
.vtable = comptime &.{
.type_name = @typeName(child),
.deinit = struct {
pub fn deinit(ctx: *anyopaque, allocator_: Allocator) void {
return allocator_.destroy(@as(*child, @ptrCast(@alignCast(ctx))));
}
}.deinit,
.send = struct {
pub fn receive(_: *anyopaque, _: tp.pid_ref, _: tp.message) tp.result {
return false;
}
}.receive,
},
};
}
pub const List = struct {
allocator: Allocator,
list: ArrayList(EventHandler),
recursion_check: bool = false,
pub fn init(allocator: Allocator) List {
return .{
.allocator = allocator,
.list = ArrayList(EventHandler).init(allocator),
};
}
pub fn deinit(self: *List) void {
for (self.list.items) |*i|
i.deinit();
self.list.deinit();
}
pub fn add(self: *List, h: EventHandler) !void {
(try self.list.addOne()).* = h;
}
pub fn remove(self: *List, h: EventHandler) !void {
return self.remove_ptr(h.ptr);
}
pub fn remove_ptr(self: *List, p_: *anyopaque) void {
for (self.list.items, 0..) |*p, i|
if (p.ptr == p_)
self.list.orderedRemove(i).deinit();
}
pub fn msg(self: *const List, m: anytype) tp.result {
return self.send(tp.self_pid(), tp.message.fmt(m));
}
pub fn send(self: *const List, from: tp.pid_ref, m: tp.message) tp.result {
if (self.recursion_check)
@panic("recursive EventHandler call");
const self_nonconst = @constCast(self);
self_nonconst.recursion_check = true;
defer self_nonconst.recursion_check = false;
tp.trace(tp.channel.event, m);
var buf: [tp.max_message_size]u8 = undefined;
@memcpy(buf[0..m.buf.len], m.buf);
const m_: tp.message = .{ .buf = buf[0..m.buf.len] };
var e: ?error{Exit} = null;
for (self.list.items) |*i|
i.send(from, m_) catch |e_| {
e = e_;
};
return if (e) |e_| e_;
}
};

View file

@ -1,8 +1,8 @@
const std = @import("std");
const EventHandler = @import("EventHandler");
const Widget = @import("Widget.zig");
const WidgetList = @import("WidgetList.zig");
const EventHandler = @import("EventHandler.zig");
const Button = @import("Button.zig");
const scrollbar_v = @import("scrollbar_v.zig");

View file

@ -1,9 +1,9 @@
const std = @import("std");
const tp = @import("thespian");
const EventHandler = @import("EventHandler");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
const EventHandler = @import("EventHandler.zig");
const Plane = @import("renderer").Plane;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;

View file

@ -3,10 +3,10 @@ const Allocator = std.mem.Allocator;
const tp = @import("thespian");
const Plane = @import("renderer").Plane;
const EventHandler = @import("EventHandler");
const tui = @import("tui.zig");
pub const Box = @import("Box.zig");
pub const EventHandler = @import("EventHandler.zig");
pub const Theme = @import("theme");
pub const themes = @import("themes").themes;
pub const scopes = @import("themes").scopes;

View file

@ -1,237 +0,0 @@
const std = @import("std");
const tp = @import("thespian");
const log = @import("log");
const tui = @import("tui.zig");
pub const ID = usize;
pub const ID_unknown = std.math.maxInt(ID);
pub const Result = anyerror!void;
pub const Context = struct {
args: tp.message = .{},
pub fn fmt(value: anytype) Context {
return .{ .args = tp.message.fmtbuf(&context_buffer, value) catch @panic("command.Context.fmt failed") };
}
};
threadlocal var context_buffer: [tp.max_message_size]u8 = undefined;
pub const fmt = Context.fmt;
const Vtable = struct {
id: ID = ID_unknown,
name: []const u8,
run: *const fn (self: *Vtable, ctx: Context) tp.result,
meta: Metadata,
};
const Metadata = struct {
description: []const u8 = &[_]u8{},
interactive: bool = true,
};
pub fn Closure(comptime T: type) type {
return struct {
vtbl: Vtable,
f: FunT,
data: T,
const FunT: type = *const fn (T, ctx: Context) Result;
const Self = @This();
pub fn init(f: FunT, data: T, name: []const u8, meta: Metadata) Self {
return .{
.vtbl = .{
.run = run,
.name = name,
.meta = meta,
},
.f = f,
.data = data,
};
}
pub fn register(self: *Self) !void {
if (command_names.get(self.vtbl.name)) |id| {
self.vtbl.id = id;
reAddCommand(&self.vtbl) catch |e| return log.err("cmd", "reAddCommand", e);
// log.print("cmd", "reAddCommand({s}) => {d}", .{ self.vtbl.name, self.vtbl.id });
} else {
self.vtbl.id = try addCommand(&self.vtbl);
command_names.put(self.vtbl.name, self.vtbl.id) catch |e| return log.err("cmd", "addCommand", e);
// log.print("cmd", "addCommand({s}) => {d}", .{ self.vtbl.name, self.vtbl.id });
}
}
pub fn unregister(self: *Self) void {
removeCommand(self.vtbl.id);
}
fn run(vtbl: *Vtable, ctx: Context) tp.result {
const self: *Self = fromVtable(vtbl);
return self.f(self.data, ctx) catch |e| tp.exit_error(e, @errorReturnTrace());
}
fn fromVtable(vtbl: *Vtable) *Self {
return @fieldParentPtr("vtbl", vtbl);
}
};
}
const CommandTable = std.ArrayList(?*Vtable);
pub var commands: CommandTable = CommandTable.init(command_table_allocator);
var command_names: std.StringHashMap(ID) = std.StringHashMap(ID).init(command_table_allocator);
const command_table_allocator = std.heap.c_allocator;
fn addCommand(cmd: *Vtable) !ID {
try commands.append(cmd);
return commands.items.len - 1;
}
fn reAddCommand(cmd: *Vtable) !void {
if (commands.items[cmd.id] != null) return error.DuplicateCommand;
commands.items[cmd.id] = cmd;
}
pub fn removeCommand(id: ID) void {
commands.items[id] = null;
}
pub fn execute(id: ID, ctx: Context) tp.result {
_ = tui.current(); // assert we are in tui thread scope
if (id >= commands.items.len)
return tp.exit_fmt("CommandNotFound: {d}", .{id});
const cmd = commands.items[id];
if (cmd) |p| {
// var buf: [tp.max_message_size]u8 = undefined;
// log.print("cmd", "execute({s}) {s}", .{ p.name, ctx.args.to_json(&buf) catch "" }) catch |e| return tp.exit_error(e, @errorReturnTrace());
return p.run(p, ctx);
} else {
return tp.exit_fmt("CommandNotAvailable: {d}", .{id});
}
}
pub fn getId(name: []const u8) ?ID {
for (commands.items) |cmd| {
if (cmd) |p|
if (std.mem.eql(u8, p.name, name))
return p.id;
}
return null;
}
pub fn get_id_cache(name: []const u8, id: *?ID) ?ID {
for (commands.items) |cmd| {
if (cmd) |p|
if (std.mem.eql(u8, p.name, name)) {
id.* = p.id;
return p.id;
};
}
return null;
}
const suppressed_errors = .{
"enable_fast_scroll",
"disable_fast_scroll",
"clear_diagnostics",
};
pub fn executeName(name: []const u8, ctx: Context) tp.result {
const id = getId(name);
if (id) |id_| return execute(id_, ctx);
inline for (suppressed_errors) |err| if (std.mem.eql(u8, err, name)) return;
return tp.exit_fmt("CommandNotFound: {s}", .{name});
}
fn CmdDef(comptime T: type) type {
return struct {
const Fn = fn (T, Context) anyerror!void;
name: [:0]const u8,
f: *const Fn,
meta: Metadata,
};
}
fn getTargetType(comptime Namespace: type) type {
return @field(Namespace, "Target");
}
fn getCommands(comptime Namespace: type) []const CmdDef(*getTargetType(Namespace)) {
@setEvalBranchQuota(10_000);
comptime switch (@typeInfo(Namespace)) {
.Struct => |info| {
var count = 0;
const Target = getTargetType(Namespace);
// @compileLog(Namespace, Target);
for (info.decls) |decl| {
// @compileLog(decl.name, @TypeOf(@field(Namespace, decl.name)));
if (@TypeOf(@field(Namespace, decl.name)) == CmdDef(*Target).Fn)
count += 1;
}
var cmds: [count]CmdDef(*Target) = undefined;
var i = 0;
for (info.decls) |decl| {
if (@TypeOf(@field(Namespace, decl.name)) == CmdDef(*Target).Fn) {
cmds[i] = .{
.f = &@field(Namespace, decl.name),
.name = decl.name,
.meta = if (@hasDecl(Namespace, decl.name ++ "_meta"))
@field(Namespace, decl.name ++ "_meta")
else
@compileError(decl.name ++ " has no meta"),
};
i += 1;
}
}
const cmds_const = cmds;
return &cmds_const;
},
else => @compileError("expected tuple or struct type"),
};
}
pub fn Collection(comptime Namespace: type) type {
const cmds = comptime getCommands(Namespace);
const Target = getTargetType(Namespace);
const Clsr = Closure(*Target);
var fields_var: [cmds.len]std.builtin.Type.StructField = undefined;
inline for (cmds, 0..) |cmd, i| {
@setEvalBranchQuota(10_000);
fields_var[i] = .{
.name = cmd.name,
.type = Clsr,
.default_value = null,
.is_comptime = false,
.alignment = if (@sizeOf(Clsr) > 0) @alignOf(Clsr) else 0,
};
}
const fields: [cmds.len]std.builtin.Type.StructField = fields_var;
const Fields = @Type(.{
.Struct = .{
.is_tuple = false,
.layout = .auto,
.decls = &.{},
.fields = &fields,
},
});
return struct {
fields: Fields,
const Self = @This();
pub fn init(self: *Self, targetPtr: *Target) !void {
if (cmds.len == 0)
@compileError("no commands found in type " ++ @typeName(Target) ++ " (did you mark them public?)");
inline for (cmds) |cmd| {
@field(self.fields, cmd.name) = Closure(*Target).init(cmd.f, targetPtr, cmd.name, cmd.meta);
try @field(self.fields, cmd.name).register();
}
}
pub fn deinit(self: *Self) void {
inline for (cmds) |cmd|
Closure(*Target).unregister(&@field(self.fields, cmd.name));
}
};
}

View file

@ -16,13 +16,13 @@ const Plane = @import("renderer").Plane;
const Cell = @import("renderer").Cell;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const command = @import("command");
const EventHandler = @import("EventHandler");
const scrollbar_v = @import("scrollbar_v.zig");
const editor_gutter = @import("editor_gutter.zig");
const EventHandler = @import("EventHandler.zig");
const Widget = @import("Widget.zig");
const WidgetList = @import("WidgetList.zig");
const command = @import("command.zig");
const tui = @import("tui.zig");
pub const Cursor = Buffer.Cursor;

View file

@ -10,12 +10,12 @@ const Plane = @import("renderer").Plane;
const style = @import("renderer").style;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Widget = @import("Widget.zig");
const EventHandler = @import("EventHandler.zig");
const MessageFilter = @import("MessageFilter.zig");
const tui = @import("tui.zig");
const command = @import("command.zig");
const ed = @import("editor.zig");
allocator: Allocator,

View file

@ -6,12 +6,12 @@ const Plane = @import("renderer").Plane;
const tp = @import("thespian");
const log = @import("log");
const root = @import("root");
const command = @import("command");
const EventHandler = @import("EventHandler");
const command = @import("command.zig");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
const Menu = @import("Menu.zig");
const EventHandler = @import("EventHandler.zig");
const Button = @import("Button.zig");
const scrollbar_v = @import("scrollbar_v.zig");
const editor = @import("editor.zig");

View file

@ -8,7 +8,8 @@ const Widget = @import("Widget.zig");
const Button = @import("Button.zig");
const Menu = @import("Menu.zig");
const tui = @import("tui.zig");
const command = @import("command.zig");
const command = @import("command");
const fonts = @import("fonts.zig");
allocator: std.mem.Allocator,

View file

@ -6,10 +6,10 @@ const ArrayList = @import("std").ArrayList;
const tp = @import("thespian");
const Plane = @import("renderer").Plane;
const EventHandler = @import("EventHandler");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
const EventHandler = @import("EventHandler.zig");
pub const name = "inputview";

View file

@ -7,10 +7,10 @@ const syntax = @import("syntax");
const Plane = @import("renderer").Plane;
const style = @import("renderer").style;
const EventHandler = @import("EventHandler");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
const EventHandler = @import("EventHandler.zig");
const mainview = @import("mainview.zig");
const ed = @import("editor.zig");
@ -74,7 +74,7 @@ fn clear(self: *Self) void {
fn inspect_location(self: *Self, row: usize, col: usize) void {
const syn = self.editor.syntax orelse return;
const root = (self.editor.buffer orelse return).root;
const col_pos = root.get_line_width_to_pos(row, col,self.editor.metrics) catch return;
const col_pos = root.get_line_width_to_pos(row, col, self.editor.metrics) catch return;
syn.highlights_at_point(self, dump_highlight, .{ .row = @intCast(row), .column = @intCast(col_pos) });
}

View file

@ -11,11 +11,11 @@ const log = @import("log");
const Plane = @import("renderer").Plane;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const command = @import("command");
const tui = @import("tui.zig");
const command = @import("command.zig");
const Box = @import("Box.zig");
const EventHandler = @import("EventHandler.zig");
const EventHandler = @import("EventHandler");
const Widget = @import("Widget.zig");
const WidgetList = @import("WidgetList.zig");
const WidgetStack = @import("WidgetStack.zig");

View file

@ -5,10 +5,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;

View file

@ -4,10 +4,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../../tui.zig");
const command = @import("../../../command.zig");
const EventHandler = @import("../../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;

View file

@ -4,10 +4,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../../tui.zig");
const command = @import("../../../command.zig");
const EventHandler = @import("../../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;

View file

@ -4,10 +4,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../../tui.zig");
const command = @import("../../../command.zig");
const EventHandler = @import("../../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;

View file

@ -5,10 +5,10 @@ const root = @import("root");
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const mod = @import("renderer").input.modifier;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const Self = @This();

View file

@ -5,10 +5,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../../tui.zig");
const command = @import("../../../command.zig");
const EventHandler = @import("../../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;

View file

@ -4,10 +4,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../../tui.zig");
const command = @import("../../../command.zig");
const EventHandler = @import("../../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;

View file

@ -4,10 +4,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../../tui.zig");
const command = @import("../../../command.zig");
const EventHandler = @import("../../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;

View file

@ -9,10 +9,10 @@ const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const project_manager = @import("project_manager");
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const MessageFilter = @import("../../MessageFilter.zig");
const max_complete_paths = 1024;

View file

@ -4,11 +4,11 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const ed = @import("../../editor.zig");
const Allocator = @import("std").mem.Allocator;

View file

@ -4,11 +4,11 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;
const eql = @import("std").mem.eql;

View file

@ -4,11 +4,11 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const Allocator = @import("std").mem.Allocator;

View file

@ -1,10 +1,10 @@
const std = @import("std");
const tp = @import("thespian");
const root = @import("root");
const command = @import("command");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const command = @import("../../command.zig");
pub const Type = @import("file_browser.zig").Create(@This());

View file

@ -1,10 +1,10 @@
const std = @import("std");
const tp = @import("thespian");
const root = @import("root");
const command = @import("command");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const command = @import("../../command.zig");
pub const Type = @import("file_browser.zig").Create(@This());

View file

@ -2,8 +2,7 @@ const std = @import("std");
const cbor = @import("cbor");
const tp = @import("thespian");
const root = @import("root");
const command = @import("../../command.zig");
const command = @import("command");
pub const Type = @import("palette.zig").Create(@This());

View file

@ -10,10 +10,10 @@ const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const project_manager = @import("project_manager");
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const MessageFilter = @import("../../MessageFilter.zig");
const Button = @import("../../Button.zig");
const InputBox = @import("../../InputBox.zig");

View file

@ -9,10 +9,10 @@ const key = @import("renderer").input.key;
const mod = @import("renderer").input.modifier;
const event_type = @import("renderer").input.event_type;
const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8;
const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const command = @import("../../command.zig");
const EventHandler = @import("../../EventHandler.zig");
const Button = @import("../../Button.zig");
const InputBox = @import("../../InputBox.zig");
const Widget = @import("../../Widget.zig");

View file

@ -5,9 +5,9 @@ const tracy = @import("tracy");
const Plane = @import("renderer").Plane;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const EventHandler = @import("EventHandler");
const Widget = @import("Widget.zig");
const EventHandler = @import("EventHandler.zig");
plane: Plane,
pos_scrn: u32 = 0,

View file

@ -1,4 +1,5 @@
const std = @import("std");
const EventHandler = @import("EventHandler");
const status_widget = @import("widget.zig");
const Widget = @import("../Widget.zig");
@ -6,7 +7,7 @@ const WidgetList = @import("../WidgetList.zig");
pub const Style = enum { none, grip };
pub fn create(allocator: std.mem.Allocator, parent: Widget, config: []const u8, style: Style, event_handler: ?Widget.EventHandler) !Widget {
pub fn create(allocator: std.mem.Allocator, parent: Widget, config: []const u8, style: Style, event_handler: ?EventHandler) !Widget {
var w = try WidgetList.createH(allocator, parent, "statusbar", .{ .static = 1 });
if (style == .grip) w.after_render = render_grip;
w.ctx = w;

View file

@ -1,18 +1,19 @@
const std = @import("std");
const tp = @import("thespian");
const Plane = @import("renderer").Plane;
const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
plane: Plane,
layout: Widget.Layout,
on_event: ?Widget.EventHandler,
on_event: ?EventHandler,
const Self = @This();
pub fn Create(comptime layout_: Widget.Layout) @import("widget.zig").CreateFunction {
return struct {
fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self);
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),

View file

@ -3,6 +3,7 @@ const tp = @import("thespian");
const cbor = @import("cbor");
const zeit = @import("zeit");
const EventHandler = @import("EventHandler");
const Plane = @import("renderer").Plane;
const Widget = @import("../Widget.zig");
@ -12,12 +13,12 @@ const tui = @import("../tui.zig");
allocator: std.mem.Allocator,
plane: Plane,
tick_timer: ?tp.Cancellable = null,
on_event: ?Widget.EventHandler,
on_event: ?EventHandler,
tz: zeit.timezone.TimeZone,
const Self = @This();
pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler) @import("widget.zig").CreateError!Widget {
var env = std.process.getEnvMap(allocator) catch |e| return tp.exit_error(e, @errorReturnTrace());
defer env.deinit();
const self: *Self = try allocator.create(Self);

View file

@ -3,10 +3,11 @@ const Allocator = std.mem.Allocator;
const tp = @import("thespian");
const Plane = @import("renderer").Plane;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
const Button = @import("../Button.zig");
const command = @import("../command.zig");
errors: usize = 0,
warnings: usize = 0,
@ -17,7 +18,7 @@ rendered: [:0]const u8 = "",
const Self = @This();
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler) @import("widget.zig").CreateError!Widget {
return Button.create_widget(Self, allocator, parent, .{
.ctx = .{},
.label = "",

View file

@ -7,10 +7,11 @@ const root = @import("root");
const Plane = @import("renderer").Plane;
const style = @import("renderer").style;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
const Button = @import("../Button.zig");
const command = @import("../command.zig");
const tui = @import("../tui.zig");
allocator: Allocator,
@ -35,7 +36,7 @@ eol_mode: Buffer.EolMode = .lf,
const project_icon = "";
const Self = @This();
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler) @import("widget.zig").CreateError!Widget {
const btn = try Button.create(Self, allocator, parent, .{
.ctx = .{
.allocator = allocator,

View file

@ -7,11 +7,11 @@ const Plane = @import("renderer").Plane;
const utils = @import("renderer").input.utils;
const key_ = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
const command = @import("../command.zig");
const tui = @import("../tui.zig");
const EventHandler = @import("../EventHandler.zig");
const history = 8;
@ -31,7 +31,7 @@ const Self = @This();
const idle_msg = "🐶";
pub const width = idle_msg.len + 20;
pub fn create(allocator: Allocator, parent: Plane, _: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler) @import("widget.zig").CreateError!Widget {
const frame_rate = tp.env.get().num("frame-rate");
const self: *Self = try allocator.create(Self);
self.* = .{

View file

@ -4,10 +4,11 @@ const tp = @import("thespian");
const Buffer = @import("Buffer");
const Plane = @import("renderer").Plane;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
const Button = @import("../Button.zig");
const command = @import("../command.zig");
line: usize = 0,
lines: usize = 0,
@ -18,7 +19,7 @@ eol_mode: Buffer.EolMode = .lf,
const Self = @This();
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler) @import("widget.zig").CreateError!Widget {
return Button.create_widget(Self, allocator, parent, .{
.ctx = .{},
.label = "",

View file

@ -2,7 +2,7 @@ const std = @import("std");
const tp = @import("thespian");
const cbor = @import("cbor");
const log = @import("log");
const EventHandler = @import("EventHandler");
const Plane = @import("renderer").Plane;
const Widget = @import("../Widget.zig");
@ -15,7 +15,7 @@ msg: std.ArrayList(u8),
msg_counter: usize = 0,
clear_timer: ?tp.Cancellable = null,
level: Level = .info,
on_event: ?Widget.EventHandler,
on_event: ?EventHandler,
const message_display_time_seconds = 2;
const error_display_time_seconds = 4;
@ -26,7 +26,7 @@ const Level = enum {
err,
};
pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self);
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),

View file

@ -3,14 +3,15 @@ const Allocator = std.mem.Allocator;
const Plane = @import("renderer").Plane;
const style = @import("renderer").style;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
const Button = @import("../Button.zig");
const command = @import("../command.zig");
const tui = @import("../tui.zig");
const CreateError = @import("widget.zig").CreateError;
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) CreateError!Widget {
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler) CreateError!Widget {
return Button.create_widget(void, allocator, parent, .{
.ctx = {},
.label = tui.get_mode(),

View file

@ -7,11 +7,11 @@ const Plane = @import("renderer").Plane;
const key = @import("renderer").input.key;
const event_type = @import("renderer").input.event_type;
const utils = @import("renderer").input.utils;
const command = @import("command");
const EventHandler = @import("EventHandler");
const Widget = @import("../Widget.zig");
const command = @import("../command.zig");
const tui = @import("../tui.zig");
const EventHandler = @import("../EventHandler.zig");
plane: Plane,
ctrl: bool = false,
@ -23,7 +23,7 @@ const Self = @This();
pub const width = 5;
pub fn create(allocator: Allocator, parent: Plane, _: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: Allocator, parent: Plane, _: ?EventHandler) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self);
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),

View file

@ -2,7 +2,7 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const tp = @import("thespian");
const tracy = @import("tracy");
const EventHandler = @import("EventHandler");
const Plane = @import("renderer").Plane;
const Widget = @import("../Widget.zig");
@ -14,11 +14,11 @@ cursels: usize = 0,
selection: ?ed.Selection = null,
buf: [256]u8 = undefined,
rendered: [:0]const u8 = "",
on_event: ?Widget.EventHandler,
on_event: ?EventHandler,
const Self = @This();
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
pub fn create(allocator: Allocator, parent: Plane, event_handler: ?EventHandler) @import("widget.zig").CreateError!Widget {
const self: *Self = try allocator.create(Self);
self.* = .{
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),

View file

@ -1,7 +1,9 @@
const std = @import("std");
const Widget = @import("../Widget.zig");
const EventHandler = @import("EventHandler");
const Plane = @import("renderer").Plane;
const Widget = @import("../Widget.zig");
const widgets = std.static_string_map.StaticStringMap(CreateFunction).initComptime(.{
.{ "mode", @import("modestate.zig").create },
.{ "file", @import("filestate.zig").create },
@ -16,9 +18,9 @@ const widgets = std.static_string_map.StaticStringMap(CreateFunction).initCompti
.{ "clock", @import("clock.zig").create },
});
pub const CreateError = error{ OutOfMemory, Exit };
pub const CreateFunction = *const fn (allocator: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) CreateError!Widget;
pub const CreateFunction = *const fn (allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler) CreateError!Widget;
pub fn create(name: []const u8, allocator: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) CreateError!?Widget {
pub fn create(name: []const u8, allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler) CreateError!?Widget {
const create_ = widgets.get(name) orelse return null;
return try create_(allocator, parent, event_handler);
}

View file

@ -8,11 +8,11 @@ const tracy = @import("tracy");
const builtin = @import("builtin");
pub const renderer = @import("renderer");
const command = @import("command");
const EventHandler = @import("EventHandler");
const command = @import("command.zig");
const Widget = @import("Widget.zig");
const MessageFilter = @import("MessageFilter.zig");
const EventHandler = @import("EventHandler.zig");
const mainview = @import("mainview.zig");
const Allocator = std.mem.Allocator;
@ -67,6 +67,7 @@ pub fn spawn(allocator: Allocator, ctx: *tp.context, eh: anytype, env: ?*const t
}
fn start(args: StartArgs) tp.result {
command.context_check = &context_check;
_ = tp.set_trap(true);
var self = init(args.allocator) catch |e| return tp.exit_error(e, @errorReturnTrace());
errdefer self.deinit();
@ -781,6 +782,10 @@ pub fn current() *Self {
return instance_ orelse @panic("tui call out of context");
}
fn context_check() void {
if (instance_ == null) @panic("tui call out of context");
}
pub fn get_mode() []const u8 {
return if (current().input_mode) |m| m.name else "INI";
}