From 632cf8cfb3ab6beea483d4aed6630d989a4bcec1 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 1 Dec 2025 21:37:40 +0100 Subject: [PATCH] refactor: add mode.current_bindings function --- src/keybind/keybind.zig | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/keybind/keybind.zig b/src/keybind/keybind.zig index 200b2dc..f5e5b0f 100644 --- a/src/keybind/keybind.zig +++ b/src/keybind/keybind.zig @@ -177,9 +177,13 @@ pub const Mode = struct { self.initialized = false; } - pub fn current_key_event_sequence_bindings(self: *const Mode, allocator: std.mem.Allocator) error{OutOfMemory}![]const Binding { + pub fn current_bindings(self: *const Mode, allocator: std.mem.Allocator, select_mode: SelectMode) error{OutOfMemory}![]const Binding { + return self.bindings.get_bindings(allocator, select_mode); + } + + pub fn current_key_event_sequence_bindings(self: *const Mode, allocator: std.mem.Allocator, select_mode: SelectMode) error{OutOfMemory}![]const Binding { if (globals.current_sequence.items.len == 0) return &.{}; - return self.bindings.get_matches_for_key_event_sequence(allocator, globals.current_sequence.items, .no_keypad); + return self.bindings.get_matches_for_key_event_sequence(allocator, globals.current_sequence.items, select_mode); } }; @@ -777,7 +781,7 @@ const BindingSet = struct { } } - /// Retreive bindings that will match a key event sequence + /// Retrieve bindings that will match a key event sequence pub fn get_matches_for_key_event_sequence( self: *const @This(), allocator: std.mem.Allocator, @@ -794,9 +798,22 @@ const BindingSet = struct { }; return matches.toOwnedSlice(allocator); } + + /// Retrieve possibly filtered bindings + pub fn get_bindings( + self: *const @This(), + allocator: std.mem.Allocator, + select_mode: SelectMode, + ) error{OutOfMemory}![]const Binding { + var matches: std.ArrayListUnmanaged(Binding) = .{}; + for (self.press.items) |*binding| if (select(select_mode, binding)) { + (try matches.addOne(allocator)).* = binding.*; + }; + return matches.toOwnedSlice(allocator); + } }; -const SelectMode = enum { all, no_keypad }; +pub const SelectMode = enum { all, no_keypad }; fn select(select_mode: SelectMode, binding: *const Binding) bool { return switch (select_mode) {