Skip to content

Commit

Permalink
Implemented multiword console commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr Akulich committed Jan 11, 2017
1 parent d750f44 commit 77203dc
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/ui/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,41 @@ std::string Console::submitCommand(const std::string& command)
m_HistoryIndex = -1;
m_PendingLine.clear();

std::vector<std::string> args = Utils::split(command, ' ');

outputAdd(" >> " + command);

if(args.empty())
if(command.empty())
return "";

size_t bestMatchSize = 0;
int bestMatchIndex = -1;
for (size_t i = 0; i < m_Commands.size(); ++i)
{
if (m_Commands.at(i) == args[0])
const std::string &candidate = m_Commands.at(i);
if (candidate.size() < bestMatchSize) {
// We already found a better command candidate
continue;
}

if (command.size() == candidate.size() || (command.size() > candidate.size() && command.at(candidate.size()) == ' '))
{
std::string result = m_CommandCallbacks.at(i)(args);
outputAdd(result);
return result;
// it makes sense to compare
if (candidate == command.substr(0, candidate.size() - 1))
{
// Match!
bestMatchSize = candidate.size();
bestMatchIndex = i;
}
}
}

if (bestMatchIndex >= 0)
{
const std::vector<std::string> args = Utils::split(command, ' ');
const std::string result = m_CommandCallbacks.at(bestMatchIndex)(args);
outputAdd(result);
return result;
}

outputAdd(" -- Command not found -- ");

return "NOTFOUND";
Expand Down

0 comments on commit 77203dc

Please sign in to comment.