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.
This commit is contained in:
scawful
2025-09-25 09:48:57 -04:00
parent e930789f4b
commit 163aa9e121
2 changed files with 24 additions and 25 deletions

View File

@@ -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();
}

View File

@@ -2,6 +2,7 @@
#define YAZE_APP_EDITOR_SYSTEM_COMMAND_MANAGER_H
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
@@ -31,38 +32,36 @@ class CommandManager {
CommandInfo() = default;
};
// New command info which supports subsections of commands
struct CommandInfoOrPrefix {
CommandInfo command_info;
std::unordered_map<std::string, CommandInfoOrPrefix> 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<std::string, CommandInfo> 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<std::string, CommandInfoOrPrefix> commands_;
std::unordered_map<std::string, CommandGroup> commands_;
};
} // namespace editor