From 163aa9e121e441a37b7d94ce87da7fed343db95c Mon Sep 17 00:00:00 2001 From: scawful Date: Thu, 25 Sep 2025 09:48:57 -0400 Subject: [PATCH] Refactor CommandManager to Simplify Command Structure - Updated CommandManager to replace the recursive CommandInfoOrPrefix structure with a simplified CommandGroup structure, enhancing clarity and usability. - Modified ShowWhichKey, SaveKeybindings, and LoadKeybindings methods to accommodate the new command structure, ensuring consistent handling of main commands and their associated subcommands. - Improved code readability and maintainability by streamlining command registration and execution processes. --- src/app/editor/system/command_manager.cc | 22 +++++++++---------- src/app/editor/system/command_manager.h | 27 ++++++++++++------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/app/editor/system/command_manager.cc b/src/app/editor/system/command_manager.cc index 50b18c79..b613d728 100644 --- a/src/app/editor/system/command_manager.cc +++ b/src/app/editor/system/command_manager.cc @@ -39,13 +39,13 @@ void CommandManager::ShowWhichKey() { if (ImGui::BeginTable("CommandsTable", commands_.size(), ImGuiTableFlags_SizingStretchProp)) { - for (const auto &[shortcut, info] : commands_) { - ImGui::TableNextColumn(); - ImGui::TextColored(colors[colorIndex], "%c: %s", - info.command_info.mnemonic, - info.command_info.name.c_str()); - colorIndex = (colorIndex + 1) % numColors; - } + for (const auto &[shortcut, group] : commands_) { + ImGui::TableNextColumn(); + ImGui::TextColored(colors[colorIndex], "%c: %s", + group.main_command.mnemonic, + group.main_command.name.c_str()); + colorIndex = (colorIndex + 1) % numColors; + } ImGui::EndTable(); } ImGui::EndPopup(); @@ -55,9 +55,9 @@ void CommandManager::ShowWhichKey() { void CommandManager::SaveKeybindings(const std::string &filepath) { std::ofstream out(filepath); if (out.is_open()) { - for (const auto &[shortcut, info] : commands_) { - out << shortcut << " " << info.command_info.mnemonic << " " - << info.command_info.name << " " << info.command_info.desc << "\n"; + for (const auto &[shortcut, group] : commands_) { + out << shortcut << " " << group.main_command.mnemonic << " " + << group.main_command.name << " " << group.main_command.desc << "\n"; } out.close(); } @@ -70,7 +70,7 @@ void CommandManager::LoadKeybindings(const std::string &filepath) { std::string shortcut, name, desc; char mnemonic; while (in >> shortcut >> mnemonic >> name >> desc) { - commands_[shortcut].command_info = {nullptr, mnemonic, name, desc}; + commands_[shortcut].main_command = {nullptr, mnemonic, name, desc}; } in.close(); } diff --git a/src/app/editor/system/command_manager.h b/src/app/editor/system/command_manager.h index 07f9576f..0eb91ba0 100644 --- a/src/app/editor/system/command_manager.h +++ b/src/app/editor/system/command_manager.h @@ -2,6 +2,7 @@ #define YAZE_APP_EDITOR_SYSTEM_COMMAND_MANAGER_H #include +#include #include #include @@ -31,38 +32,36 @@ class CommandManager { CommandInfo() = default; }; - // New command info which supports subsections of commands - struct CommandInfoOrPrefix { - CommandInfo command_info; - std::unordered_map subcommands; - CommandInfoOrPrefix(CommandInfo command_info) - : command_info(std::move(command_info)) {} - CommandInfoOrPrefix() = default; + // Simplified command structure without recursive types + struct CommandGroup { + CommandInfo main_command; + std::unordered_map subcommands; + + CommandGroup() = default; + CommandGroup(CommandInfo main) : main_command(std::move(main)) {} }; void RegisterPrefix(const std::string &group_name, const char prefix, const std::string &name, const std::string &desc) { - commands_[group_name].command_info = {nullptr, prefix, name, desc}; + commands_[group_name].main_command = {nullptr, prefix, name, desc}; } void RegisterSubcommand(const std::string &group_name, const std::string &shortcut, const char mnemonic, const std::string &name, const std::string &desc, Command command) { - commands_[group_name].subcommands[shortcut].command_info = { - command, mnemonic, name, desc}; + commands_[group_name].subcommands[shortcut] = {command, mnemonic, name, desc}; } void RegisterCommand(const std::string &shortcut, Command command, char mnemonic, const std::string &name, const std::string &desc) { - commands_[shortcut].command_info = {std::move(command), mnemonic, name, - desc}; + commands_[shortcut].main_command = {std::move(command), mnemonic, name, desc}; } void ExecuteCommand(const std::string &shortcut) { if (commands_.find(shortcut) != commands_.end()) { - commands_[shortcut].command_info.command(); + commands_[shortcut].main_command.command(); } } @@ -72,7 +71,7 @@ class CommandManager { void LoadKeybindings(const std::string &filepath); private: - std::unordered_map commands_; + std::unordered_map commands_; }; } // namespace editor