Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alias command #1815

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions library/private/interactor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class interactor_impl : public interactor
std::function<void(const std::vector<std::string>&)> callback) override;
interactor& removeCommand(const std::string& action) override;
std::vector<std::string> getCommandActions() const override;
std::unordered_map<std::string, std::string> aliasMap;
void alias(const std::string& action, const std::string& value);
bool triggerCommand(std::string_view command) override;

interactor& initBindings() override;
Expand Down
28 changes: 27 additions & 1 deletion library/src/interactor_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ using mod_t = interaction_bind_t::ModifierKeys;

class interactor_impl::internals
{
//-------------------------------------------------------------------
private:
// Map to store aliases
std::map<std::string, std::string> aliasMap;
//-------------------------------------------------------------------
public:
struct BindingCommands
{
Expand Down Expand Up @@ -753,6 +758,18 @@ std::vector<std::string> interactor_impl::getCommandActions() const
return actions;
}

//----------------------------------------------------------------------------
void interactor_impl::alias(const std::string& action, const std::string& value)
{
if (action.empty() || value.empty())
{
log::error("Alias action or value cannot be empty.");
return;
}
aliasMap[action] = value;
log::error("Alias added: " + action + " -> " + value);
}

//----------------------------------------------------------------------------
bool interactor_impl::triggerCommand(std::string_view command)
{
Expand All @@ -773,7 +790,16 @@ bool interactor_impl::triggerCommand(std::string_view command)
return true;
}

const std::string& action = tokens[0];
std::string action = tokens[0];

// Check if action is an alias
auto aliasIt = aliasMap.find(action);
if (aliasIt != aliasMap.end())
{
action = aliasIt->second;
log::error("Alias resolved: " + action);
}

try
{
// Find the right command to call
Expand Down
Loading