modify consoleList to use game_console functionality

- also move game_console find_matches & match_compare out of anon namespace
This commit is contained in:
Michael Akopyan 2021-03-11 22:58:11 -08:00
parent 8663a927cd
commit 13bdb043f6
3 changed files with 56 additions and 100 deletions

View File

@ -256,57 +256,10 @@ namespace command
add("consoleList", [](const params& params)
{
std::string input = params.get(1);
// "borrowed" these two functions from game_console
auto match_compare = [](const std::string& input, const std::string& text, bool exact)
{
if (exact && text == input) return true;
if (!exact && text.find(input) != std::string::npos) return true;
return false;
};
auto find_matches = [match_compare](std::string& input, std::vector<std::string>& suggestions,
const bool exact)
{
input = utils::string::to_lower(input);
for (auto i = 0; i < *game::dvarCount; i++)
{
if (game::sortedDvars[i] && game::sortedDvars[i]->name)
{
auto name = utils::string::to_lower(game::sortedDvars[i]->name);
if (match_compare(input, name, exact))
{
suggestions.push_back(game::sortedDvars[i]->name);
}
}
}
auto* cmd = (*game::cmd_functions);
while (cmd)
{
if (cmd->name)
{
auto name = utils::string::to_lower(cmd->name);
if (match_compare(input, name, exact))
{
suggestions.push_back(cmd->name);
}
if (exact && suggestions.size() > 1)
{
return;
}
}
cmd = cmd->next;
}
};
const std::string input = params.get(1);
std::vector<std::string> matches;
find_matches(input, matches, false);
game_console::find_matches(input, matches, false);
for(auto& match : matches)
{

View File

@ -171,57 +171,7 @@ namespace game_console
const auto _y = con.globals.font_height + con.globals.y + (con.globals.font_height * (line + 1)) + 15.0f;
game::R_AddCmdDrawText(text, 0x7FFFFFFF, console_font, con.globals.x + offset, _y, 1.0f, 1.0f, 0.0f, color,
0);
}
bool match_compare(const std::string& input, const std::string& text, const bool exact)
{
if (exact && text == input) return true;
if (!exact && text.find(input) != std::string::npos) return true;
return false;
}
void find_matches(std::string input, std::vector<std::string>& suggestions, const bool exact)
{
input = utils::string::to_lower(input);
for (int i = 0; i < *game::dvarCount; i++)
{
if (game::sortedDvars[i] && game::sortedDvars[i]->name)
{
std::string name = utils::string::to_lower(game::sortedDvars[i]->name);
if (match_compare(input, name, exact))
{
suggestions.push_back(game::sortedDvars[i]->name);
}
if (exact && suggestions.size() > 1)
{
return;
}
}
}
game::cmd_function_s* cmd = (*game::cmd_functions);
while (cmd)
{
if (cmd->name)
{
std::string name = utils::string::to_lower(cmd->name);
if (match_compare(input, name, exact))
{
suggestions.push_back(cmd->name);
}
if (exact && suggestions.size() > 1)
{
return;
}
}
cmd = cmd->next;
}
0);
}
void draw_input()
@ -676,6 +626,56 @@ namespace game_console
return true;
}
bool match_compare(const std::string& input, const std::string& text, const bool exact)
{
if (exact && text == input) return true;
if (!exact && text.find(input) != std::string::npos) return true;
return false;
}
void find_matches(std::string input, std::vector<std::string>& suggestions, const bool exact)
{
input = utils::string::to_lower(input);
for (int i = 0; i < *game::dvarCount; i++)
{
if (game::sortedDvars[i] && game::sortedDvars[i]->name)
{
std::string name = utils::string::to_lower(game::sortedDvars[i]->name);
if (game_console::match_compare(input, name, exact))
{
suggestions.push_back(game::sortedDvars[i]->name);
}
if (exact && suggestions.size() > 1)
{
return;
}
}
}
game::cmd_function_s* cmd = (*game::cmd_functions);
while (cmd)
{
if (cmd->name)
{
std::string name = utils::string::to_lower(cmd->name);
if (game_console::match_compare(input, name, exact))
{
suggestions.push_back(cmd->name);
}
if (exact && suggestions.size() > 1)
{
return;
}
}
cmd = cmd->next;
}
}
class component final : public component_interface
{
public:

View File

@ -14,4 +14,7 @@ namespace game_console
bool console_char_event(int local_client_num, int key);
bool console_key_event(int local_client_num, int key, int down);
bool match_compare(const std::string& input, const std::string& text, const bool exact);
void find_matches(std::string input, std::vector<std::string>& suggestions, const bool exact);
}