yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
autocomplete.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cctype>
5
6namespace yaze {
7namespace cli {
8
10 const std::string& cmd, const std::string& desc,
11 const std::vector<std::string>& examples) {
12 CommandDef def;
13 def.name = cmd;
14 def.description = desc;
15 def.examples = examples;
16 commands_.push_back(def);
17}
18
20 const std::string& param, const std::string& desc,
21 const std::vector<std::string>& values) {
22 // TODO: Store parameter definitions
23}
24
25int AutocompleteEngine::FuzzyScore(const std::string& text,
26 const std::string& query) {
27 if (query.empty())
28 return 0;
29
30 std::string t = text, q = query;
31 std::transform(t.begin(), t.end(), t.begin(), ::tolower);
32 std::transform(q.begin(), q.end(), q.begin(), ::tolower);
33
34 if (t == q)
35 return 1000;
36 if (t.find(q) == 0)
37 return 500;
38 if (t.find(q) != std::string::npos)
39 return 250;
40
41 // Fuzzy char matching
42 size_t ti = 0, qi = 0;
43 int score = 0;
44 while (ti < t.length() && qi < q.length()) {
45 if (t[ti] == q[qi]) {
46 score += 10;
47 qi++;
48 }
49 ti++;
50 }
51
52 return (qi == q.length()) ? score : 0;
53}
54
55std::vector<Suggestion> AutocompleteEngine::GetSuggestions(
56 const std::string& input) {
57 std::vector<Suggestion> results;
58
59 for (const auto& cmd : commands_) {
60 int score = FuzzyScore(cmd.name, input);
61 if (score > 0) {
62 Suggestion s;
63 s.text = cmd.name;
64 s.description = cmd.description;
65 s.example = cmd.examples.empty() ? "" : cmd.examples[0];
66 s.score = score;
67 results.push_back(s);
68 }
69 }
70
71 std::sort(results.begin(), results.end(),
72 [](const Suggestion& a, const Suggestion& b) {
73 return a.score > b.score;
74 });
75
76 return results;
77}
78
80 const std::string& partial_cmd) {
81 std::vector<Suggestion> help;
82
83 // Parse command and suggest parameters
84 if (partial_cmd.find("hex-") == 0) {
85 help.push_back({"--address=0xXXXXXX", "ROM address in hex", ""});
86 help.push_back({"--length=16", "Number of bytes", ""});
87 help.push_back({"--format=both", "Output format (hex/ascii/both)", ""});
88 } else if (partial_cmd.find("palette-") == 0) {
89 help.push_back({"--group=0", "Palette group (0-7)", ""});
90 help.push_back({"--palette=0", "Palette index (0-7)", ""});
91 help.push_back({"--format=hex", "Color format (snes/rgb/hex)", ""});
92 } else if (partial_cmd.find("dungeon-") == 0) {
93 help.push_back({"--room_id=5", "Room ID (0-296)", ""});
94 help.push_back({"--include_sprites=true", "Include sprite data", ""});
95 }
96
97 return help;
98}
99
100} // namespace cli
101} // namespace yaze
void RegisterCommand(const std::string &cmd, const std::string &desc, const std::vector< std::string > &examples={})
std::vector< CommandDef > commands_
int FuzzyScore(const std::string &text, const std::string &query)
void RegisterParameter(const std::string &param, const std::string &desc, const std::vector< std::string > &values={})
std::vector< Suggestion > GetContextualHelp(const std::string &partial_cmd)
std::vector< Suggestion > GetSuggestions(const std::string &input)
std::vector< std::string > examples
std::string description