Skip to content

Commit

Permalink
When listing commands, use alphabetical order
Browse files Browse the repository at this point in the history
  • Loading branch information
slipher committed Jan 24, 2025
1 parent 1d253ad commit 13331e2
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/engine/framework/CommandSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,11 @@ namespace Cmd {
void Run(const Cmd::Args& args) const override {
CommandMap& commands = GetCommandMap();

std::vector<const commandRecord_t*> matches;
std::vector<const std::string*> matchesNames;
std::vector<CommandMap::const_iterator> matches;
unsigned long maxNameLength = 0;

//Find all the matching commands and their names
for (auto it = commands.cbegin(); it != commands.cend(); ++it) {
for (CommandMap::const_iterator it = commands.cbegin(); it != commands.cend(); ++it) {
const commandRecord_t& record = it->second;

// /listCmds's argument is used for prefix matching
Expand All @@ -365,16 +364,19 @@ namespace Cmd {
}

if (record.cmd->GetFlags() & showCmdFlags) {
matches.push_back(&it->second);
matchesNames.push_back(&it->first);
matches.push_back(it);
maxNameLength = std::max<size_t>(maxNameLength, it->first.size());
}
}

// TODO: case insensitive compare function?
std::sort(matches.begin(), matches.end(),
[](CommandMap::const_iterator a, CommandMap::const_iterator b) { return a->first < b->first; });

//Print the matches, keeping the description aligned
for (unsigned i = 0; i < matches.size(); i++) {
int toFill = maxNameLength - matchesNames[i]->size();
Print(" %s%s %s", matchesNames[i]->c_str(), std::string(toFill, ' ').c_str(), matches[i]->description.c_str());
for (CommandMap::const_iterator it : matches) {
int toFill = maxNameLength - it->first.size();
Print(" %s%s %s", it->first, std::string(toFill, ' '), it->second.description);
}

Print("%zu commands", matches.size());
Expand Down

0 comments on commit 13331e2

Please sign in to comment.