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