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"
5#include "absl/strings/str_join.h"
6#include "absl/strings/str_cat.h"
7#include "ftxui/dom/elements.hpp"
8#include "ftxui/dom/table.hpp"
9#include "cli/tui/chat_tui.h"
10
11namespace yaze {
12namespace cli {
13
14// Forward declaration
15namespace handlers {
16absl::Status HandleAgentCommand(const std::vector<std::string>& args);
17}
18
20 // Commands are now managed by CommandRegistry singleton
21}
22
23absl::Status ModernCLI::Run(int argc, char* argv[]) {
24 if (argc < 2) {
25 ShowHelp();
26 return absl::OkStatus();
27 }
28
29 std::vector<std::string> args;
30 for (int i = 1; i < argc; ++i) {
31 args.push_back(argv[i]);
32 }
33
34 // Handle --tui flag
35 if (args[0] == "--tui") {
36 Rom rom;
37 // Attempt to load a ROM from the current directory or a well-known path
38 auto rom_status = rom.LoadFromFile("zelda3.sfc");
39 if (!rom_status.ok()) {
40 // Try assets directory as a fallback
41 rom_status = rom.LoadFromFile("assets/zelda3.sfc");
42 }
43
44 tui::ChatTUI chat_tui(rom.is_loaded() ? &rom : nullptr);
45 chat_tui.Run();
46 return absl::OkStatus();
47 }
48
49 if (args[0] == "help") {
50 if (args.size() > 1) {
51 ShowCategoryHelp(args[1]);
52 } else {
53 ShowHelp();
54 }
55 return absl::OkStatus();
56 }
57
58 // Special case: "agent" command routes to HandleAgentCommand
59 if (args[0] == "agent") {
60 std::vector<std::string> agent_args(args.begin() + 1, args.end());
61 return handlers::HandleAgentCommand(agent_args);
62 }
63
64 // Use CommandRegistry for unified command execution
65 auto& registry = CommandRegistry::Instance();
66
67 std::string command_name = args[0];
68 std::vector<std::string> command_args(args.begin() + 1, args.end());
69
70 if (registry.HasCommand(command_name)) {
71 return registry.Execute(command_name, command_args, nullptr);
72 }
73
74 return absl::NotFoundError(absl::StrCat("Unknown command: ", command_name));
75}
76
78 using namespace ftxui;
79 auto& registry = CommandRegistry::Instance();
80 auto categories = registry.GetCategories();
81
82 auto banner = text("🎮 Z3ED - AI-Powered ROM Editor CLI") | bold | center;
83
84 std::vector<std::vector<std::string>> rows;
85 rows.push_back({"Category", "Commands", "Description"});
86
87 // Add special "agent" category first
88 rows.push_back({"agent", "chat, learn, todo, emulator-*", "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") desc = "ROM resource inspection";
99 else if (category == "dungeon") desc = "Dungeon editing";
100 else if (category == "overworld") desc = "Overworld editing";
101 else if (category == "emulator") desc = "Emulator debugging";
102 else if (category == "graphics") desc = "Graphics/palette/sprites";
103 else if (category == "game") desc = "Messages/dialogue/music";
104 else desc = category + " commands";
105
106 rows.push_back({category, cmd_list, desc});
107 }
108
109 Table summary(rows);
110 summary.SelectAll().Border(LIGHT);
111 summary.SelectRow(0).Decorate(bold);
112
113 auto layout = vbox({
115 banner,
116 separator(),
117 summary.Render(),
118 separator(),
119 text(absl::StrFormat("Total: %zu commands across %zu categories",
120 registry.Count(), categories.size() + 1)) | center | dim,
121 text("Try `z3ed agent simple-chat` for AI-powered ROM inspection") | center,
122 text("Use `z3ed --list-commands` for complete list") | dim | center
123 });
124
125 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
126 Render(screen, layout);
127 screen.Print();
128}
129
130void ModernCLI::ShowCategoryHelp(const std::string& category) const {
131 using namespace ftxui;
132 auto& registry = CommandRegistry::Instance();
133
134 std::vector<std::vector<std::string>> rows;
135 rows.push_back({"Command", "Description", "Requirements"});
136
137 auto commands = registry.GetCommandsInCategory(category);
138 for (const auto& cmd_name : commands) {
139 auto* metadata = registry.GetMetadata(cmd_name);
140 if (metadata) {
141 std::string requirements;
142 if (metadata->requires_rom) requirements += "ROM ";
143 if (metadata->requires_grpc) requirements += "gRPC ";
144 if (requirements.empty()) requirements = "—";
145
146 rows.push_back({cmd_name, metadata->description, requirements});
147 }
148 }
149
150 if (rows.size() == 1) {
151 rows.push_back({"—", "No commands in this category", "—"});
152 }
153
154 Table detail(rows);
155 detail.SelectAll().Border(LIGHT);
156 detail.SelectRow(0).Decorate(bold);
157
158 auto layout = vbox({
159 text(absl::StrCat("Category: ", category)) | bold | center,
160 separator(),
161 detail.Render(),
162 separator(),
163 text("Commands are auto-registered from CommandRegistry") | dim | center
164 });
165
166 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
167 Render(screen, layout);
168 screen.Print();
169}
170
172 using namespace ftxui;
173 auto& registry = CommandRegistry::Instance();
174
175 std::vector<std::vector<std::string>> rows;
176 rows.push_back({"Command", "Category", "Description"});
177
178 auto categories = registry.GetCategories();
179 for (const auto& category : categories) {
180 auto commands = registry.GetCommandsInCategory(category);
181 for (const auto& cmd_name : commands) {
182 auto* metadata = registry.GetMetadata(cmd_name);
183 if (metadata) {
184 rows.push_back({cmd_name, metadata->category, metadata->description});
185 }
186 }
187 }
188
189 if (rows.size() == 1) {
190 rows.push_back({"—", "—", "No commands registered"});
191 }
192
193 Table command_table(rows);
194 command_table.SelectAll().Border(LIGHT);
195 command_table.SelectRow(0).Decorate(bold);
196
197 auto layout = vbox({
198 text("Z3ED Command Summary") | bold | center,
199 separator(),
200 command_table.Render(),
201 separator(),
202 text(absl::StrFormat("Total: %zu commands across %zu categories",
203 registry.Count(), categories.size())) | center | dim,
204 text("Use `z3ed --tui` for interactive command palette.") | center | dim
205 });
206
207 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
208 Render(screen, layout);
209 screen.Print();
210}
211
213 const_cast<ModernCLI*>(this)->ShowHelp();
214}
215
216void ModernCLI::PrintCategoryHelp(const std::string& category) const {
217 const_cast<ModernCLI*>(this)->ShowCategoryHelp(category);
218}
219
221 const_cast<ModernCLI*>(this)->ShowCommandSummary();
222}
223
224} // namespace cli
225} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:74
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:292
bool is_loaded() const
Definition rom.h:200
static CommandRegistry & Instance()
absl::Status Run(int argc, char *argv[])
Definition cli.cc:23
void PrintCategoryHelp(const std::string &category) const
Definition cli.cc:216
void PrintCommandSummary() const
Definition cli.cc:220
void ShowCategoryHelp(const std::string &category) const
Definition cli.cc:130
void ShowCommandSummary() const
Definition cli.cc:171
void PrintTopLevelHelp() const
Definition cli.cc:212
void ShowHelp()
Definition cli.cc:77
Definition cli.h:17
absl::Status HandleAgentCommand(const std::vector< std::string > &args)
Unified agent command handler using CommandRegistry.
Definition agent.cc:109
std::string GetColoredLogo()
Main namespace for the application.
Definition controller.cc:20