瀏覽代碼

refactor(keybinding): Centralize command execution in KeyBindingManager (B.1)

Bernardo Magri 1 月之前
父節點
當前提交
383f9d8724
共有 2 個文件被更改,包括 31 次插入24 次删除
  1. 12 11
      include/lumacs/keybinding.hpp
  2. 19 13
      src/keybinding.cpp

+ 12 - 11
include/lumacs/keybinding.hpp

@@ -1,13 +1,15 @@
-#pragma once
-
 #include <string>
 #include <vector>
 #include <map>
 #include <memory>
-#include <functional>
 #include <chrono>
 #include <optional>
 
+// Forward declaration for CommandSystem
+namespace lumacs {
+    class CommandSystem;
+}
+
 namespace lumacs {
 
 /// @brief Represents a single key in a key sequence.
@@ -72,18 +74,17 @@ private:
     std::vector<Key> keys_;
 };
 
-/// @brief Function type for key binding callbacks.
-using KeyBindingFunction = std::function<bool()>;
+
 
 /// @brief Represents a key binding with its associated function.
 struct KeyBinding {
     KeySequence sequence;       ///< The key sequence that triggers this binding.
-    KeyBindingFunction function;///< The function to execute.
+    std::string command_name;   ///< The name of the command to execute.
     std::string description;    ///< Human-readable description.
     
     KeyBinding() = default;
-    KeyBinding(const KeySequence& seq, KeyBindingFunction func, std::string desc = "");
-    KeyBinding(const std::string& key_str, KeyBindingFunction func, std::string desc = "");
+    KeyBinding(const KeySequence& seq, std::string cmd_name, std::string desc = "");
+    KeyBinding(const std::string& key_str, std::string cmd_name, std::string desc = "");
 };
 
 /// @brief Result of processing a key in the binding manager.
@@ -103,12 +104,12 @@ enum class KeyResult {
 /// - Prefix key logic (e.g., C-x waiting for next key).
 class KeyBindingManager {
 public:
-    KeyBindingManager();
+    KeyBindingManager(CommandSystem* command_system);
     ~KeyBindingManager() = default;
     
     /// @brief Bind a key sequence to a function.
-    void bind(const KeySequence& sequence, KeyBindingFunction function, const std::string& description = "");
-    void bind(const std::string& key_str, KeyBindingFunction function, const std::string& description = "");
+    void bind(const KeySequence& sequence, std::string cmd_name, const std::string& description = "");
+    void bind(const std::string& key_str, std::string cmd_name, const std::string& description = "");
     
     /// @brief Unbind a key sequence.
     void unbind(const KeySequence& sequence);

+ 19 - 13
src/keybinding.cpp

@@ -1,4 +1,5 @@
 #include "lumacs/keybinding.hpp"
+#include "lumacs/command_system.hpp" // Include for CommandSystem
 #include <sstream>
 #include <algorithm>
 #include <cctype>
@@ -150,32 +151,32 @@ bool KeySequence::operator<(const KeySequence& other) const {
 // KeyBinding Implementation
 // ============================================================================
 
-KeyBinding::KeyBinding(const KeySequence& seq, KeyBindingFunction func, std::string desc)
-    : sequence(seq), function(std::move(func)), description(std::move(desc)) {
+KeyBinding::KeyBinding(const KeySequence& seq, std::string cmd_name, std::string desc)
+    : sequence(seq), command_name(std::move(cmd_name)), description(std::move(desc)) {
 }
 
-KeyBinding::KeyBinding(const std::string& key_str, KeyBindingFunction func, std::string desc)
-    : sequence(key_str), function(std::move(func)), description(std::move(desc)) {
+KeyBinding::KeyBinding(const std::string& key_str, std::string cmd_name, std::string desc)
+    : sequence(key_str), command_name(std::move(cmd_name)), description(std::move(desc)) {
 }
 
 // ============================================================================
 // KeyBindingManager Implementation
 // ============================================================================
 
-KeyBindingManager::KeyBindingManager() 
-    : sequence_timeout_(std::chrono::milliseconds(1000)) {
+KeyBindingManager::KeyBindingManager(CommandSystem* command_system) 
+    : sequence_timeout_(std::chrono::milliseconds(1000)), command_system_(command_system) {
 }
 
-void KeyBindingManager::bind(const KeySequence& sequence, KeyBindingFunction function, const std::string& description) {
-    if (sequence.empty() || !function) {
+void KeyBindingManager::bind(const KeySequence& sequence, std::string cmd_name, const std::string& description) {
+    if (sequence.empty() || cmd_name.empty()) {
         return;
     }
     
-    bindings_[sequence] = KeyBinding(sequence, std::move(function), description);
+    bindings_[sequence] = KeyBinding(sequence, std::move(cmd_name), description);
 }
 
-void KeyBindingManager::bind(const std::string& key_str, KeyBindingFunction function, const std::string& description) {
-    bind(KeySequence(key_str), std::move(function), description);
+void KeyBindingManager::bind(const std::string& key_str, std::string cmd_name, const std::string& description) {
+    bind(KeySequence(key_str), std::move(cmd_name), description);
 }
 
 void KeyBindingManager::unbind(const KeySequence& sequence) {
@@ -206,8 +207,13 @@ KeyResult KeyBindingManager::process_key(const Key& key) {
         clear_partial_sequence();
         
         try {
-            bool success = exact_binding->function();
-            return success ? KeyResult::Executed : KeyResult::Failed;
+            // Execute the command via the CommandSystem
+            if (command_system_) {
+                CommandResult result = command_system_->execute(exact_binding->command_name, {}); // No args for now
+                // Optionally handle result.success or result.message
+                return result.success ? KeyResult::Executed : KeyResult::Failed;
+            }
+            return KeyResult::Failed; // No command system
         } catch (...) {
             return KeyResult::Failed;
         }