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

@ -118,6 +118,21 @@ pub fn build(b: *std.Build) void {
},
});
const command_mod = b.createModule(.{
.root_source_file = b.path("src/command.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "log", .module = log_mod },
},
});
const EventHandler_mod = b.createModule(.{
.root_source_file = b.path("src/EventHandler.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
},
});
const color_mod = b.createModule(.{
.root_source_file = b.path("src/color.zig"),
});
@ -196,6 +211,8 @@ pub fn build(b: *std.Build) void {
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "config", .module = config_mod },
.{ .name = "log", .module = log_mod },
.{ .name = "command", .module = command_mod },
.{ .name = "EventHandler", .module = EventHandler_mod },
.{ .name = "location_history", .module = location_history_mod },
.{ .name = "project_manager", .module = project_manager_mod },
.{ .name = "syntax", .module = syntax_mod },

View file

@ -35,6 +35,25 @@ pub fn to_owned(pimpl: anytype) Self {
};
}
pub fn static(T: type) Self {
return .{
.ptr = &none,
.vtable = comptime &.{
.type_name = @typeName(T),
.deinit = struct {
pub fn deinit(_: *anyopaque) void {}
}.deinit,
.send = struct {
pub fn receive(_: *anyopaque, from_: tp.pid_ref, m: tp.message) tp.result {
_ = try T.receive(from_, m);
}
}.receive,
},
};
}
var none = {};
pub fn to_unowned(pimpl: anytype) Self {
const impl = @typeInfo(@TypeOf(pimpl));
const child: type = impl.Pointer.child;

View file

@ -2,7 +2,7 @@ const std = @import("std");
const tp = @import("thespian");
const log = @import("log");
const tui = @import("tui.zig");
pub var context_check: ?*const fn() void = null;
pub const ID = usize;
pub const ID_unknown = std.math.maxInt(ID);
@ -98,7 +98,7 @@ pub fn removeCommand(id: ID) void {
}
pub fn execute(id: ID, ctx: Context) tp.result {
_ = tui.current(); // assert we are in tui thread scope
if(context_check) |check| check();
if (id >= commands.items.len)
return tp.exit_fmt("CommandNotFound: {d}", .{id});
const cmd = commands.items[id];

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,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

@ -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";
}