Skip to content

Commit

Permalink
Refactored according to suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
void-inject committed Jan 11, 2025
1 parent 98cb859 commit ceda6b3
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions library/src/interactor_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -778,22 +778,7 @@ interactor& interactor_impl::initCommands()
const std::string& aliasName = args[0];
// Combine all remaining arguments into the alias command
std::string aliasCommand;

for (size_t i = 1; i < args.size(); ++i)
{
if (i > 1)
{
aliasCommand += " ";
}
aliasCommand += args[i];
}

// Prevent recursion
if (aliasName == aliasCommand)
{
throw interactor_impl::invalid_args_exception(
"Alias cannot reference itself: " + aliasName);
}
aliasCommand = std::accumulate(args.begin() + 1, args.end(), " ");

// Add alias to the map
this->Internals->AliasMap[aliasName] = aliasCommand;
Expand Down Expand Up @@ -838,6 +823,14 @@ std::vector<std::string> interactor_impl::getCommandActions() const
bool interactor_impl::triggerCommand(std::string_view command)
{
log::debug("Command: ", command);

// Resolve Alias Before Tokenizing
auto aliasIt = AliasMap.find(std::string(command));
if (aliasIt != AliasMap.end())
{
command = aliasIt->second;
}

std::vector<std::string> tokens;
try
{
Expand All @@ -856,16 +849,6 @@ bool interactor_impl::triggerCommand(std::string_view command)

std::string action = tokens[0];

// Resolve Alias
auto aliasIt = AliasMap.find(action);
if (aliasIt != AliasMap.end())
{
std::vector<std::string> aliasTokens = utils::tokenize(aliasIt->second);
tokens.erase(tokens.begin());
tokens.insert(tokens.begin(), aliasTokens.begin(), aliasTokens.end());
action = tokens[0];
}

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

0 comments on commit ceda6b3

Please sign in to comment.