yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
cli.cc
Go to the documentation of this file.
1#include "cli/cli.h"
2
3#include <iostream>
4
5#include "absl/strings/str_cat.h"
6#include "absl/strings/str_join.h"
9#ifndef __EMSCRIPTEN__
10#include "ftxui/dom/elements.hpp"
11#include "ftxui/dom/table.hpp"
12#endif
13#include "cli/z3ed_ascii_logo.h"
14
15namespace yaze {
16namespace cli {
17
18// Forward declaration
19namespace handlers {
20absl::Status HandleAgentCommand(const std::vector<std::string>& args);
21}
22
24 // Commands are now managed by CommandRegistry singleton
25}
26
27absl::Status ModernCLI::Run(int argc, char* argv[]) {
28 if (argc < 2) {
29 ShowHelp();
30 return absl::OkStatus();
31 }
32
33 std::vector<std::string> args;
34 for (int i = 1; i < argc; ++i) {
35 args.push_back(argv[i]);
36 }
37
38 if (args[0] == "help") {
39 if (args.size() > 1) {
40 const std::string& target = args[1];
41 auto& registry = CommandRegistry::Instance();
42 if (target == "all") {
43 std::cout << registry.GenerateCompleteHelp() << "\n";
44 } else if (registry.HasCommand(target)) {
45 std::cout << registry.GenerateHelp(target) << "\n";
46 } else {
47 ShowCategoryHelp(target);
48 }
49 } else {
50 ShowHelp();
51 }
52 return absl::OkStatus();
53 }
54
55 // Special case: "agent" command routes to HandleAgentCommand
56 if (args[0] == "agent") {
57 std::vector<std::string> agent_args(args.begin() + 1, args.end());
58 return handlers::HandleAgentCommand(agent_args);
59 }
60
61 // Use CommandRegistry for unified command execution
62 auto& registry = CommandRegistry::Instance();
63
64 std::string command_name = args[0];
65 std::vector<std::string> command_args(args.begin() + 1, args.end());
66
67 if (registry.HasCommand(command_name)) {
68 return registry.Execute(command_name, command_args, nullptr);
69 }
70
71 return absl::NotFoundError(absl::StrCat("Unknown command: ", command_name));
72}
73
75 auto& registry = CommandRegistry::Instance();
76 auto categories = registry.GetCategories();
77
78#ifndef __EMSCRIPTEN__
79 using namespace ftxui;
80
81 auto banner = text("🎮 Z3ED - AI-Powered ROM Editor CLI") | bold | center;
82
83 std::vector<std::vector<std::string>> rows;
84 rows.push_back({"Category", "Commands", "Description"});
85
86 // Add special "agent" category first
87 rows.push_back({"agent", "chat, learn, todo, emulator-*",
88 "AI conversational agent + debugging tools"});
89
90 // Add registry categories
91 for (const auto& category : categories) {
92 auto commands = registry.GetCommandsInCategory(category);
93 std::string cmd_list = commands.size() > 3
94 ? absl::StrCat(commands.size(), " commands")
95 : absl::StrJoin(commands, ", ");
96
97 std::string desc;
98 if (category == "resource")
99 desc = "ROM resource inspection";
100 else if (category == "dungeon")
101 desc = "Dungeon editing";
102 else if (category == "overworld")
103 desc = "Overworld editing";
104 else if (category == "emulator")
105 desc = "Emulator debugging";
106 else if (category == "graphics")
107 desc = "Graphics/palette/sprites";
108 else if (category == "game")
109 desc = "Messages/dialogue/music";
110 else
111 desc = category + " commands";
112
113 rows.push_back({category, cmd_list, desc});
114 }
115
116 Table summary(rows);
117 summary.SelectAll().Border(LIGHT);
118 summary.SelectRow(0).Decorate(bold);
119
120 auto layout = vbox(
121 {text(yaze::cli::GetColoredLogo()), banner, separator(), summary.Render(),
122 separator(),
123 text(absl::StrFormat("Total: %zu commands across %zu categories",
124 registry.Count(), categories.size() + 1)) |
125 center | dim,
126 text("Try `z3ed agent simple-chat` for AI-powered ROM inspection") |
127 center,
128 text("Use `z3ed --list-commands` for complete list") | dim | center});
129
130 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
131 Render(screen, layout);
132 screen.Print();
133#else
134 // Simple text output for WASM builds
135 std::cout << yaze::cli::GetColoredLogo() << "\n";
136 std::cout << "Z3ED - AI-Powered ROM Editor CLI\n\n";
137 std::cout << "Categories:\n";
138 std::cout << " agent - AI conversational agent + debugging tools\n";
139 for (const auto& category : categories) {
140 auto commands = registry.GetCommandsInCategory(category);
141 std::cout << " " << category << " - " << commands.size() << " commands\n";
142 }
143 std::cout << "\nTotal: " << registry.Count() << " commands\n";
144 std::cout << "Use 'help <category>' for more details.\n";
145#endif
146}
147
148void ModernCLI::ShowCategoryHelp(const std::string& category) const {
149 auto& registry = CommandRegistry::Instance();
150 auto commands = registry.GetCommandsInCategory(category);
151
152#ifndef __EMSCRIPTEN__
153 using namespace ftxui;
154
155 std::vector<std::vector<std::string>> rows;
156 rows.push_back({"Command", "Description", "Requirements"});
157
158 for (const auto& cmd_name : commands) {
159 auto* metadata = registry.GetMetadata(cmd_name);
160 if (metadata) {
161 std::string requirements;
162 if (metadata->requires_rom)
163 requirements += "ROM ";
164 if (metadata->requires_grpc)
165 requirements += "gRPC ";
166 if (requirements.empty())
167 requirements = "—";
168
169 rows.push_back({cmd_name, metadata->description, requirements});
170 }
171 }
172
173 if (rows.size() == 1) {
174 rows.push_back({"—", "No commands in this category", "—"});
175 }
176
177 Table detail(rows);
178 detail.SelectAll().Border(LIGHT);
179 detail.SelectRow(0).Decorate(bold);
180
181 auto layout =
182 vbox({text(absl::StrCat("Category: ", category)) | bold | center,
183 separator(), detail.Render(), separator(),
184 text("Commands are auto-registered from CommandRegistry") | dim |
185 center});
186
187 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
188 Render(screen, layout);
189 screen.Print();
190#else
191 // Simple text output for WASM builds
192 std::cout << "Category: " << category << "\n\n";
193 for (const auto& cmd_name : commands) {
194 auto* metadata = registry.GetMetadata(cmd_name);
195 if (metadata) {
196 std::cout << " " << cmd_name << " - " << metadata->description << "\n";
197 }
198 }
199 if (commands.empty()) {
200 std::cout << " No commands in this category.\n";
201 }
202#endif
203}
204
206 auto& registry = CommandRegistry::Instance();
207 auto categories = registry.GetCategories();
208
209#ifndef __EMSCRIPTEN__
210 using namespace ftxui;
211
212 std::vector<std::vector<std::string>> rows;
213 rows.push_back({"Command", "Category", "Description"});
214
215 for (const auto& category : categories) {
216 auto commands = registry.GetCommandsInCategory(category);
217 for (const auto& cmd_name : commands) {
218 auto* metadata = registry.GetMetadata(cmd_name);
219 if (metadata) {
220 rows.push_back({cmd_name, metadata->category, metadata->description});
221 }
222 }
223 }
224
225 if (rows.size() == 1) {
226 rows.push_back({"—", "—", "No commands registered"});
227 }
228
229 Table command_table(rows);
230 command_table.SelectAll().Border(LIGHT);
231 command_table.SelectRow(0).Decorate(bold);
232
233 auto layout =
234 vbox({text("Z3ED Command Summary") | bold | center, separator(),
235 command_table.Render(), separator(),
236 text(absl::StrFormat("Total: %zu commands across %zu categories",
237 registry.Count(), categories.size())) |
238 center | dim,
239 text("Use `z3ed --tui` for interactive command palette.") | center |
240 dim});
241
242 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
243 Render(screen, layout);
244 screen.Print();
245#else
246 // Simple text output for WASM builds
247 std::cout << "Z3ED Command Summary\n\n";
248 for (const auto& category : categories) {
249 auto commands = registry.GetCommandsInCategory(category);
250 for (const auto& cmd_name : commands) {
251 auto* metadata = registry.GetMetadata(cmd_name);
252 if (metadata) {
253 std::cout << " " << cmd_name << " [" << metadata->category << "] - "
254 << metadata->description << "\n";
255 }
256 }
257 }
258 std::cout << "\nTotal: " << registry.Count() << " commands across "
259 << categories.size() << " categories\n";
260#endif
261}
262
264 const_cast<ModernCLI*>(this)->ShowHelp();
265}
266
267void ModernCLI::PrintCategoryHelp(const std::string& category) const {
268 const_cast<ModernCLI*>(this)->ShowCategoryHelp(category);
269}
270
272 const_cast<ModernCLI*>(this)->ShowCommandSummary();
273}
274
275} // namespace cli
276} // namespace yaze
static CommandRegistry & Instance()
absl::Status Run(int argc, char *argv[])
Definition cli.cc:27
void PrintCategoryHelp(const std::string &category) const
Definition cli.cc:267
void PrintCommandSummary() const
Definition cli.cc:271
void ShowCategoryHelp(const std::string &category) const
Definition cli.cc:148
void ShowCommandSummary() const
Definition cli.cc:205
void PrintTopLevelHelp() const
Definition cli.cc:263
void ShowHelp()
Definition cli.cc:74
Definition cli.h:17
absl::Status HandleAgentCommand(const std::vector< std::string > &args)
Unified agent command handler using CommandRegistry.
Definition agent.cc:110
std::string GetColoredLogo()