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"
4#include "absl/strings/str_join.h"
5#include "absl/strings/str_cat.h"
6#include "ftxui/dom/elements.hpp"
7#include "ftxui/dom/table.hpp"
8#include "cli/tui/chat_tui.h"
9
10namespace yaze {
11namespace cli {
12
16
18 auto handlers = handlers::CreateAllCommandHandlers();
19 for (auto& handler : handlers) {
20 commands_[handler->GetName()] = std::move(handler);
21 }
22}
23
24absl::Status ModernCLI::Run(int argc, char* argv[]) {
25 if (argc < 2) {
26 ShowHelp();
27 return absl::OkStatus();
28 }
29
30 std::vector<std::string> args;
31 for (int i = 1; i < argc; ++i) {
32 args.push_back(argv[i]);
33 }
34
35 // Handle --tui flag
36 if (args[0] == "--tui") {
37 Rom rom;
38 // Attempt to load a ROM from the current directory or a well-known path
39 auto rom_status = rom.LoadFromFile("zelda3.sfc");
40 if (!rom_status.ok()) {
41 // Try assets directory as a fallback
42 rom_status = rom.LoadFromFile("assets/zelda3.sfc");
43 }
44
45 tui::ChatTUI chat_tui(rom.is_loaded() ? &rom : nullptr);
46 chat_tui.Run();
47 return absl::OkStatus();
48 }
49
50 if (args[0] == "help") {
51 if (args.size() > 1) {
52 ShowCategoryHelp(args[1]);
53 } else {
54 ShowHelp();
55 }
56 return absl::OkStatus();
57 }
58
59 auto it = commands_.find(args[0]);
60 if (it != commands_.end()) {
61 std::vector<std::string> command_args(args.begin() + 1, args.end());
62 return it->second->Run(command_args, nullptr);
63 }
64
65 return absl::NotFoundError(absl::StrCat("Unknown command: ", args[0]));
66}
67
69 using namespace ftxui;
70 auto banner = text("🎮 Z3ED - CLI for Zelda 3") | bold | center;
71 auto summary = Table({
72 {"Command", "Description", "TODO/Reference"},
73 {"agent", "AI conversational agent", "ref: agent::chat"},
74 {"rom", "ROM info, diff, validate", "todo#101"},
75 {"dungeon", "Dungeon tooling", "todo#202"},
76 {"gfx", "Graphics export/import", "ref: gfx::export"},
77 {"palette", "Palette operations", "todo#305"},
78 {"project", "Project workflows", "ref: project::build"}
79 });
80 summary.SelectAll().Border(LIGHT);
81 summary.SelectRow(0).Decorate(bold);
82
83 auto layout = vbox({
85 banner,
86 separator(),
87 summary.Render(),
88 separator(),
89 text("Try `z3ed --tui` for the animated FTXUI interface") | center,
90 text("Use `--list-commands` for complete breakdown") | dim | center
91 });
92
93 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
94 Render(screen, layout);
95 screen.Print();
96}
97
98void ModernCLI::ShowCategoryHelp(const std::string& category) const {
99 using namespace ftxui;
100 std::vector<std::vector<std::string>> rows;
101 rows.push_back({"Subcommand", "Summary", "TODO/Reference"});
102
103 auto it = commands_.find(category);
104 if (it != commands_.end()) {
105 const auto& metadata = it->second->Describe();
106 for (const auto& entry : metadata.entries) {
107 rows.push_back({entry.name, entry.description, entry.todo_reference});
108 }
109 }
110
111 if (rows.size() == 1) {
112 rows.push_back({"—", "No metadata registered", "—"});
113 }
114
115 Table detail(rows);
116 detail.SelectAll().Border(LIGHT);
117 detail.SelectRow(0).Decorate(bold);
118
119 auto layout = vbox({
120 text(absl::StrCat("Category: ", category)) | bold | center,
121 separator(),
122 detail.Render(),
123 separator(),
124 text("Command handlers can expose TODO references via Describe().") | dim | center
125 });
126
127 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
128 Render(screen, layout);
129 screen.Print();
130}
131
133 using namespace ftxui;
134 std::vector<Element> tiles;
135 for (const auto& [name, handler] : commands_) {
136 const auto summary = handler->Describe();
137 tiles.push_back(
138 vbox({
139 text(summary.display_name.empty() ? name : summary.display_name) | bold,
140 separator(),
141 text(summary.summary),
142 text(absl::StrCat("TODO: ", summary.todo_reference)) | dim
143 }) | borderRounded);
144 }
145
146 auto layout = vbox({
147 text("Z3ED Command Summary") | bold | center,
148 separator(),
149 tiles.empty() ? text("No commands registered.") | dim | center
150 : vbox(tiles),
151 separator(),
152 text("Use `z3ed --tui` for interactive command palette.") | center | dim
153 });
154
155 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(layout));
156 Render(screen, layout);
157 screen.Print();
158}
159
161 const_cast<ModernCLI*>(this)->ShowHelp();
162}
163
164void ModernCLI::PrintCategoryHelp(const std::string& category) const {
165 const_cast<ModernCLI*>(this)->ShowCategoryHelp(category);
166}
167
169 const_cast<ModernCLI*>(this)->ShowCommandSummary();
170}
171
172} // namespace cli
173} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:289
bool is_loaded() const
Definition rom.h:197
absl::Status Run(int argc, char *argv[])
Definition cli.cc:24
void PrintCategoryHelp(const std::string &category) const
Definition cli.cc:164
void PrintCommandSummary() const
Definition cli.cc:168
void ShowCategoryHelp(const std::string &category) const
Definition cli.cc:98
void ShowCommandSummary() const
Definition cli.cc:132
void SetupCommands()
Definition cli.cc:17
std::map< std::string, std::unique_ptr< resources::CommandHandler > > commands_
Definition cli.h:45
void PrintTopLevelHelp() const
Definition cli.cc:160
void ShowHelp()
Definition cli.cc:68
Definition cli.h:21
std::vector< std::unique_ptr< resources::CommandHandler > > CreateAllCommandHandlers()
Factory function to create all command handlers (CLI + agent)
std::string GetColoredLogo()
Main namespace for the application.