Compare commits

...

11 commits

9 changed files with 168 additions and 36 deletions

View file

@ -34,6 +34,7 @@
["alt+p", "goto_prev_file_or_diagnostic"],
["alt+l", "toggle_panel"],
["alt+i", "toggle_inputview"],
["alt+k", "toggle_keybindview"],
["alt+x", "open_command_palette"],
["alt+f3", "toggle_auto_find"],
["f4", "toggle_input_mode"],
@ -43,7 +44,6 @@
["f10", "theme_next"],
["f11", "toggle_panel"],
["alt+f11", "toggle_color_scheme"],
["f12", "toggle_inputview"],
["shift+alt+f9", "hint_window_next_widget_style"],
["alt+!", "run_task"],
["ctrl+tab", "next_tab"],
@ -412,16 +412,14 @@
["ctrl+shift+p", "palette_menu_down"],
["ctrl+shift+q", "quit_without_saving"],
["ctrl+shift+w", "close_file_without_saving"],
["ctrl+shift+l", "overlay_toggle_panel"],
["ctrl+shift+i", "overlay_toggle_inputview"],
["alt+shift+p", "palette_menu_down"],
["alt+p", "palette_menu_up"],
["alt+l", "toggle_panel"],
["alt+i", "toggle_inputview"],
["alt+k", "toggle_keybindview"],
["f9", "theme_prev"],
["f10", "theme_next"],
["f11", "toggle_panel"],
["f12", "toggle_inputview"],
["escape", "palette_menu_cancel"],
["up", "palette_menu_up"],
["down", "palette_menu_down"],

View file

@ -24,6 +24,7 @@ const builtin_keybinds = std.StaticStringMap([]const u8).initComptime(.{
.{ "emacs", @embedFile("builtin/emacs.json") },
});
pub var enable_match_events: bool = false;
var integer_argument: ?usize = null;
var mode_flag: KeybindMode = .normal;
@ -673,6 +674,8 @@ const BindingSet = struct {
})) {
const key_event = input.KeyEvent.from_message(event, keypress, keypress_shifted, text, modifiers);
if (self.process_key_event(key_event) catch |e| return tp.exit_error(e, @errorReturnTrace())) |binding| {
if (enable_match_events)
self.send_match_event(key_event, binding);
for (binding.commands) |*cmd| try cmd.execute();
}
} else if (try m.match(.{"F"})) {
@ -771,6 +774,28 @@ const BindingSet = struct {
}
}
fn send_match_event(self: *const @This(), key_event: KeyEvent, binding: *const Binding) void {
var buf: [tp.max_message_size]u8 = undefined;
var stream: std.Io.Writer = .fixed(&buf);
var key_event_buf: [256]u8 = undefined;
var key_event_str: std.Io.Writer = .fixed(&key_event_buf);
key_event_str.print("{f}", .{key_event}) catch return;
cbor.writeArrayHeader(&stream, 5) catch return;
cbor.writeValue(&stream, "K") catch return;
cbor.writeValue(&stream, get_namespace()) catch return;
cbor.writeValue(&stream, self.config_section) catch return;
cbor.writeValue(&stream, key_event_str.buffered()) catch return;
cbor.writeArrayHeader(&stream, binding.commands.len) catch return;
for (binding.commands) |cmd| {
cbor.writeArrayHeader(&stream, 2) catch return;
cbor.writeValue(&stream, cmd.command) catch return;
stream.writeAll(cmd.args) catch return;
}
_ = tp.self_pid().send_raw(.{ .buf = stream.buffered() }) catch {};
}
fn log_keyhints_message() void {
for (globals.current_sequence.items) |item| switch (item.key) {
input.key.left_control, input.key.right_control => return,

View file

@ -5,6 +5,7 @@ const ArrayList = @import("std").ArrayList;
const Writer = @import("std").Io.Writer;
const tp = @import("thespian");
const cbor = @import("cbor");
const Plane = @import("renderer").Plane;
const EventHandler = @import("EventHandler");
@ -101,7 +102,7 @@ fn append(self: *Self, json: []const u8) !void {
};
}
pub fn listen(self: *Self, _: tp.pid_ref, m: tp.message) tp.result {
fn listen(self: *Self, _: tp.pid_ref, m: tp.message) tp.result {
if (try m.match(.{ "M", tp.more })) return;
var buf: [4096]u8 = undefined;
const json = m.to_json(&buf) catch |e| return tp.exit_error(e, @errorReturnTrace());

113
src/tui/keybindview.zig Normal file
View file

@ -0,0 +1,113 @@
const eql = @import("std").mem.eql;
const time = @import("std").time;
const Allocator = @import("std").mem.Allocator;
const ArrayList = @import("std").ArrayList;
const Writer = @import("std").Io.Writer;
const tp = @import("thespian");
const cbor = @import("cbor");
const Plane = @import("renderer").Plane;
const input = @import("input");
const tui = @import("tui.zig");
const Widget = @import("Widget.zig");
const MessageFilter = @import("MessageFilter.zig");
pub const name = "keybindview";
allocator: Allocator,
parent: Plane,
plane: Plane,
buffer: Buffer,
const Self = @This();
const Entry = struct {
time: i64,
tdiff: i64,
msg: []const u8,
};
const Buffer = ArrayList(Entry);
pub fn create(allocator: Allocator, parent: Plane) !Widget {
var n = try Plane.init(&(Widget.Box{}).opts_vscroll(@typeName(Self)), parent);
errdefer n.deinit();
const self = try allocator.create(Self);
errdefer allocator.destroy(self);
self.* = .{
.allocator = allocator,
.parent = parent,
.plane = n,
.buffer = .empty,
};
try tui.message_filters().add(MessageFilter.bind(self, keybind_match));
tui.enable_match_events();
return Widget.to(self);
}
pub fn deinit(self: *Self, allocator: Allocator) void {
tui.disable_match_events();
tui.message_filters().remove_ptr(self);
for (self.buffer.items) |item|
self.allocator.free(item.msg);
self.buffer.deinit(self.allocator);
self.plane.deinit();
allocator.destroy(self);
}
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
self.plane.set_base_style(theme.panel);
self.plane.erase();
self.plane.home();
const height = self.plane.dim_y();
var first = true;
const count = self.buffer.items.len;
const begin_at = if (height > count) 0 else count - height;
for (self.buffer.items[begin_at..]) |item| {
if (first) first = false else _ = self.plane.putstr("\n") catch return false;
self.output_tdiff(item.tdiff) catch return false;
_ = self.plane.putstr(item.msg) catch return false;
}
return false;
}
fn output_tdiff(self: *Self, tdiff: i64) !void {
const msi = @divFloor(tdiff, time.us_per_ms);
if (msi == 0) {
const d: f64 = @floatFromInt(tdiff);
const ms = d / time.us_per_ms;
_ = try self.plane.print("{d:6.2}▎", .{ms});
} else {
const ms: u64 = @intCast(msi);
_ = try self.plane.print("{d:6}▎", .{ms});
}
}
fn keybind_match(self: *Self, _: tp.pid_ref, m: tp.message) MessageFilter.Error!bool {
var namespace: []const u8 = undefined;
var section: []const u8 = undefined;
var key_event: []const u8 = undefined;
var cmds: []const u8 = undefined;
if (!(m.match(.{ "K", tp.extract(&namespace), tp.extract(&section), tp.extract(&key_event), tp.extract_cbor(&cmds) }) catch false)) return false;
var result: Writer.Allocating = .init(self.allocator);
defer result.deinit();
const writer = &result.writer;
writer.print("{s}:{s} {s} -> ", .{ namespace, section, key_event }) catch return true;
cbor.toJsonWriter(cmds, writer, .{}) catch return true;
const ts = time.microTimestamp();
const tdiff = if (self.buffer.items.len > 0) ts -| self.buffer.items[self.buffer.items.len - 1].time else 0;
(try self.buffer.addOne(self.allocator)).* = .{
.time = ts,
.tdiff = tdiff,
.msg = result.toOwnedSlice() catch return true,
};
return true;
}
pub fn receive(_: *Self, _: tp.pid_ref, _: tp.message) error{Exit}!bool {
return false;
}

View file

@ -31,6 +31,7 @@ const logview = @import("logview.zig");
const filelist_view = @import("filelist_view.zig");
const info_view = @import("info_view.zig");
const input_view = @import("inputview.zig");
const keybind_view = @import("keybindview.zig");
const Self = @This();
const Commands = command.Collection(cmds);
@ -108,8 +109,10 @@ pub fn create(allocator: std.mem.Allocator) CreateError!Widget {
if (tui.config().bottom_bar.len > 0) {
self.bottom_bar = (try widgets.addP(try @import("status/bar.zig").create(allocator, self.plane, tui.config().bottom_bar, .grip, EventHandler.bind(self, handle_bottom_bar_event)))).*;
}
if (tp.env.get().is("show-input"))
if (tp.env.get().is("show-input")) {
self.toggle_inputview_async();
self.toggle_keybindview_async();
}
if (tp.env.get().is("show-log"))
self.toggle_logview_async();
return w;
@ -823,6 +826,8 @@ const cmds = struct {
try self.toggle_panel_view(info_view, .toggle)
else if (self.is_panel_view_showing(filelist_view))
try self.toggle_panel_view(filelist_view, .toggle)
else if (self.is_panel_view_showing(keybind_view))
try self.toggle_panel_view(keybind_view, .toggle)
else if (self.is_panel_view_showing(input_view))
try self.toggle_panel_view(input_view, .toggle)
else
@ -845,6 +850,11 @@ const cmds = struct {
}
pub const toggle_inputview_meta: Meta = .{ .description = "Toggle raw input log" };
pub fn toggle_keybindview(self: *Self, _: Ctx) Result {
try self.toggle_panel_view(keybind_view, .toggle);
}
pub const toggle_keybindview_meta: Meta = .{ .description = "Toggle keybind log" };
pub fn toggle_inspector_view(self: *Self, _: Ctx) Result {
try self.toggle_panel_view(@import("inspector_view.zig"), .toggle);
}
@ -1644,6 +1654,10 @@ fn toggle_inputview_async(_: *Self) void {
tp.self_pid().send(.{ "cmd", "toggle_inputview" }) catch return;
}
fn toggle_keybindview_async(_: *Self) void {
tp.self_pid().send(.{ "cmd", "toggle_keybindview" }) catch return;
}
fn show_file_async(_: *Self, file_path: []const u8) void {
tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_path } }) catch return;
}

View file

@ -489,16 +489,6 @@ const cmds = struct {
}
pub const overlay_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
pub fn overlay_toggle_panel(self: *Self, _: Ctx) Result {
return self.cmd_async("toggle_panel");
}
pub const overlay_toggle_panel_meta: Meta = .{};
pub fn overlay_toggle_inputview(self: *Self, _: Ctx) Result {
return self.cmd_async("toggle_inputview");
}
pub const overlay_toggle_inputview_meta: Meta = .{};
pub fn overlay_next_widget_style(self: *Self, _: Ctx) Result {
tui.set_next_style(widget_type);
self.do_resize();

View file

@ -604,16 +604,6 @@ pub fn Create(options: type) type {
}
pub const overlay_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
pub fn overlay_toggle_panel(self: *Self, _: Ctx) Result {
return self.cmd_async("toggle_panel");
}
pub const overlay_toggle_panel_meta: Meta = .{};
pub fn overlay_toggle_inputview(self: *Self, _: Ctx) Result {
return self.cmd_async("toggle_inputview");
}
pub const overlay_toggle_inputview_meta: Meta = .{};
pub fn overlay_next_widget_style(self: *Self, _: Ctx) Result {
tui.set_next_style(widget_type);
const padding = tui.get_widget_style(widget_type).padding;

View file

@ -366,16 +366,6 @@ const cmds = struct {
}
pub const overlay_insert_bytes_meta: Meta = .{ .arguments = &.{.string} };
pub fn overlay_toggle_panel(self: *Self, _: Ctx) Result {
return self.cmd_async("toggle_panel");
}
pub const overlay_toggle_panel_meta: Meta = .{};
pub fn overlay_toggle_inputview(self: *Self, _: Ctx) Result {
return self.cmd_async("toggle_inputview");
}
pub const overlay_toggle_inputview_meta: Meta = .{};
pub fn overlay_next_widget_style(self: *Self, _: Ctx) Result {
tui.set_next_style(widget_type);
self.do_resize();

View file

@ -500,6 +500,9 @@ fn receive_safe(self: *Self, from: tp.pid_ref, m: tp.message) !void {
if (try m.match(.{"focus_out"}))
return;
if (try m.match(.{ "K", tp.more }))
return;
if (try self.send_widgets(from, m))
return;
@ -2357,3 +2360,11 @@ pub fn set_last_palette(type_: PaletteType, ctx: command.Context) void {
.ctx = .{ .args = ctx.args.clone(self.allocator) catch return },
};
}
pub fn enable_match_events() void {
keybind.enable_match_events = true;
}
pub fn disable_match_events() void {
keybind.enable_match_events = false;
}