Skip to content

Commit 710c647

Browse files
committed
refactor(make): simplify interactive UX and remove noisy preview flow
2 parents 5d7fa43 + 1e8a23b commit 710c647

1 file changed

Lines changed: 59 additions & 113 deletions

File tree

src/commands/MakeCommand.cpp

Lines changed: 59 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ namespace vix::commands
2929
{
3030
namespace fs = std::filesystem;
3131
namespace ui = vix::cli::util;
32-
using namespace vix::cli::style;
3332
namespace mk = vix::cli::make;
3433

3534
namespace
@@ -94,7 +93,7 @@ namespace vix::commands
9493

9594
std::size_t i = 0;
9695

97-
if (!args.empty() && is_make_colon_kind(args[0]))
96+
if (is_make_colon_kind(args[0]))
9897
{
9998
opt.kind = extract_make_colon_kind(args[0]);
10099
i = 1;
@@ -164,41 +163,32 @@ namespace vix::commands
164163
return opt;
165164
}
166165

167-
void print_result_summary(const mk::MakeOptions &opt,
168-
const mk::MakeResult &result)
166+
void print_files_list(const std::vector<mk::MakeFile> &files)
169167
{
170-
ui::section(std::cout, "Make");
171-
ui::kv(std::cout, "kind", opt.kind.empty() ? "(none)" : opt.kind, 12);
172-
ui::kv(std::cout, "name", opt.name.empty() ? "(none)" : opt.name, 12);
173-
174-
if (!opt.name_space.empty())
175-
ui::kv(std::cout, "namespace", opt.name_space, 12);
176-
177-
if (!opt.in.empty())
178-
ui::kv(std::cout, "in", opt.in, 12);
179-
180-
if (opt.dry_run)
181-
ui::kv(std::cout, "mode", "dry-run", 12);
182-
else if (opt.print_only)
183-
ui::kv(std::cout, "mode", "print", 12);
184-
else
185-
ui::kv(std::cout, "mode", "write", 12);
186-
187-
for (const auto &file : result.files)
188-
ui::kv(std::cout, "file", file.path.string(), 12);
189-
190-
for (const auto &note : result.notes)
191-
ui::warn_line(std::cout, note);
168+
for (const auto &file : files)
169+
ui::kv(std::cout, "file", file.path.string(), 10);
192170
}
193171

194-
void print_preview_if_needed(const mk::MakeResult &result)
172+
void print_preview_output(const mk::MakeResult &result)
195173
{
196-
if (result.preview.empty())
174+
if (!result.preview.empty())
175+
{
176+
std::cout << result.preview;
177+
if (result.preview.back() != '\n')
178+
std::cout << "\n";
197179
return;
180+
}
181+
182+
for (const auto &file : result.files)
183+
{
184+
std::cout << "// " << file.path.string() << "\n";
185+
std::cout << file.content;
186+
187+
if (!file.content.empty() && file.content.back() != '\n')
188+
std::cout << "\n";
198189

199-
std::cout << "\n"
200-
<< BOLD << "Preview" << RESET << "\n\n"
201-
<< result.preview << "\n";
190+
std::cout << "\n";
191+
}
202192
}
203193

204194
[[nodiscard]] bool validate_write_targets(const std::vector<mk::MakeFile> &files,
@@ -246,30 +236,31 @@ namespace vix::commands
246236
{
247237
if (!result.ok)
248238
{
249-
ui::section(std::cout, "Make");
250239
if (!result.error.empty())
251240
ui::err_line(std::cout, result.error);
252241
else
253242
ui::err_line(std::cout, "Generation failed.");
254243

255-
for (const auto &note : result.notes)
256-
ui::warn_line(std::cout, note);
257-
258244
return 1;
259245
}
260246

261-
print_result_summary(opt, result);
262-
print_preview_if_needed(result);
263-
264247
if (opt.print_only)
265248
{
266-
ui::ok_line(std::cout, "Printed");
249+
print_preview_output(result);
267250
return 0;
268251
}
269252

270253
if (opt.dry_run)
271254
{
272-
ui::ok_line(std::cout, "Dry-run complete");
255+
ui::ok_line(std::cout, "Ready");
256+
ui::kv(std::cout, "kind", opt.kind.empty() ? "(none)" : opt.kind, 10);
257+
ui::kv(std::cout, "name", opt.name.empty() ? "(none)" : opt.name, 10);
258+
259+
if (!opt.name_space.empty())
260+
ui::kv(std::cout, "namespace", opt.name_space, 10);
261+
262+
ui::kv(std::cout, "files", std::to_string(result.files.size()), 10);
263+
print_files_list(result.files);
273264
return 0;
274265
}
275266

@@ -287,23 +278,26 @@ namespace vix::commands
287278
}
288279

289280
ui::ok_line(std::cout, "Generated");
290-
ui::kv(std::cout, "count", std::to_string(result.files.size()), 12);
281+
ui::kv(std::cout, "kind", opt.kind.empty() ? "(none)" : opt.kind, 10);
282+
ui::kv(std::cout, "name", opt.name.empty() ? "(none)" : opt.name, 10);
283+
284+
if (!opt.name_space.empty())
285+
ui::kv(std::cout, "namespace", opt.name_space, 10);
286+
287+
ui::kv(std::cout, "files", std::to_string(result.files.size()), 10);
288+
print_files_list(result.files);
291289

292290
return 0;
293291
}
294292

295293
int run_direct(const mk::MakeOptions &opt)
296294
{
297-
return run_make_result(opt, mk::dispatch_make(opt));
295+
const mk::MakeResult result = mk::dispatch_make(opt);
296+
return run_make_result(opt, result);
298297
}
299298

300299
int run_class_prompt(mk::MakeOptions opt)
301300
{
302-
ui::section(std::cout, "Make:Class");
303-
304-
// -------------------------
305-
// NAME
306-
// -------------------------
307301
if (opt.name.empty())
308302
{
309303
while (true)
@@ -332,22 +326,16 @@ namespace vix::commands
332326
}
333327
}
334328

335-
// -------------------------
336-
// NAMESPACE
337-
// -------------------------
338329
if (opt.name_space.empty())
339330
{
340331
opt.name_space = prompt_line("Namespace (optional): ");
341332
}
342333

343-
// -------------------------
344-
// FIELDS COUNT
345-
// -------------------------
346334
int field_count = 0;
347335

348336
while (true)
349337
{
350-
std::string input = prompt_line("How many fields? ");
338+
const std::string input = prompt_line("How many fields? ");
351339

352340
if (input.empty())
353341
{
@@ -369,9 +357,6 @@ namespace vix::commands
369357
}
370358
}
371359

372-
// -------------------------
373-
// FIELDS INPUT
374-
// -------------------------
375360
for (int i = 0; i < field_count; ++i)
376361
{
377362
std::string fname;
@@ -413,21 +398,6 @@ namespace vix::commands
413398
continue;
414399
}
415400

416-
if (ftype.empty())
417-
{
418-
while (true)
419-
{
420-
ftype = mk::trim(prompt_line("Type for '" + fname + "': "));
421-
if (!ftype.empty())
422-
break;
423-
424-
ui::warn_line(std::cout, "Field type cannot be empty.");
425-
}
426-
}
427-
428-
if (ftype == "string")
429-
ftype = "std::string";
430-
431401
bool duplicate = false;
432402
for (const auto &field : opt.class_options.fields)
433403
{
@@ -444,70 +414,47 @@ namespace vix::commands
444414
continue;
445415
}
446416

417+
if (ftype.empty())
418+
{
419+
while (true)
420+
{
421+
ftype = mk::trim(prompt_line("Type for '" + fname + "': "));
422+
423+
if (!ftype.empty())
424+
break;
425+
426+
ui::warn_line(std::cout, "Field type cannot be empty.");
427+
}
428+
}
429+
430+
if (ftype == "string")
431+
ftype = "std::string";
432+
447433
break;
448434
}
449435

450436
opt.class_options.fields.push_back({fname, ftype});
451437
}
452438

453-
// -------------------------
454-
// OPTIONS
455-
// -------------------------
456439
opt.class_options.interactive = true;
457-
458440
opt.class_options.with_default_ctor =
459441
prompt_yes_no("Generate default constructor?", true);
460-
461442
opt.class_options.with_value_ctor =
462443
prompt_yes_no("Generate value constructor?", true);
463-
464444
opt.class_options.with_getters_setters =
465445
prompt_yes_no("Generate getters/setters?", true);
466-
467446
opt.class_options.with_copy_move =
468447
prompt_yes_no("Generate copy/move?", true);
469-
470448
opt.class_options.with_virtual_destructor =
471449
prompt_yes_no("Use virtual destructor?", true);
472450

473-
// -------------------------
474-
// FILE OPTIONS
475-
// -------------------------
476-
opt.header_only =
477-
prompt_yes_no("Header only?", false);
451+
opt.header_only = prompt_yes_no("Header only?", false);
478452

479453
if (opt.in.empty())
480454
{
481455
opt.in = prompt_line("Target folder (optional): ");
482456
}
483457

484-
// -------------------------
485-
// PREVIEW
486-
// -------------------------
487-
const bool preview_before_write =
488-
(!opt.print_only && !opt.dry_run)
489-
? prompt_yes_no("Preview before write?", true)
490-
: false;
491-
492-
mk::MakeOptions effective = opt;
493-
if (preview_before_write)
494-
effective.print_only = true;
495-
496-
const mk::MakeResult preview_result = mk::dispatch_make(effective);
497-
const int preview_status = run_make_result(effective, preview_result);
498-
if (preview_status != 0)
499-
return preview_status;
500-
501-
if (preview_before_write)
502-
{
503-
const bool proceed = prompt_yes_no("Write these files?", true);
504-
if (!proceed)
505-
{
506-
ui::warn_line(std::cout, "Cancelled.");
507-
return 1;
508-
}
509-
}
510-
511458
return run_direct(opt);
512459
}
513460

@@ -530,7 +477,6 @@ namespace vix::commands
530477
case mk::MakeKind::Module:
531478
case mk::MakeKind::Unknown:
532479
default:
533-
ui::section(std::cout, "Make");
534480
ui::err_line(std::cout,
535481
"Interactive mode is not implemented yet for: " + opt.kind);
536482
return 1;

0 commit comments

Comments
 (0)