From 6f3802fd9f39c4e5847d130b4417b5cdfb66972d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 22 Feb 2026 22:53:09 +0900 Subject: refactor(console): add console_format! proc macro and migrate all commands Introduce a Symfony Console-style tag macro that replaces verbose patterns like `console::info(&format!("text {name}"))` with `console_format!("text {name}")`. Supports all 6 tag types (info, comment, error, question, highlight, warning) with format argument distribution across multiple tagged segments. Co-Authored-By: Claude Opus 4.6 --- crates/mozart/src/commands/init.rs | 122 ++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 68 deletions(-) (limited to 'crates/mozart/src/commands/init.rs') diff --git a/crates/mozart/src/commands/init.rs b/crates/mozart/src/commands/init.rs index 3ac7976..edb2af4 100644 --- a/crates/mozart/src/commands/init.rs +++ b/crates/mozart/src/commands/init.rs @@ -2,6 +2,7 @@ use anyhow::{Context, bail}; use clap::Args; use colored::Colorize; use mozart_core::console; +use mozart_core::console_format; use mozart_core::package::{ self, RawAuthor, RawAutoload, RawPackageData, RawRepository, Stability, }; @@ -96,9 +97,8 @@ pub async fn execute( console.info(&json); console.info(""); - if !console.confirm(&format!( - "Do you confirm generation [{}]?", - console::comment("yes") + if !console.confirm(&console_format!( + "Do you confirm generation [yes]?" )) { console.error("Command aborted"); bail!("Command aborted"); @@ -124,11 +124,8 @@ pub async fn execute( if console.interactive && working_dir.join(".git").is_dir() { let gitignore_path = working_dir.join(".gitignore"); if !has_vendor_ignore(&gitignore_path) - && console.confirm(&format!( - "Would you like the {} directory added to your {} [{}]?", - console::info("vendor"), - console::info(".gitignore"), - console::comment("yes"), + && console.confirm(&console_format!( + "Would you like the vendor directory added to your .gitignore [yes]?" )) { add_vendor_ignore(&gitignore_path)?; @@ -139,13 +136,11 @@ pub async fn execute( if let Some(ref autoload) = composer.autoload && let Some((ns, path)) = autoload.psr4.iter().next() { - console.info(&format!( - "PSR-4 autoloading configured. Use \"{}\" in {path}", - console::comment(&format!("namespace {ns};")), + console.info(&console_format!( + "PSR-4 autoloading configured. Use \"namespace {ns};\" in {path}" )); - console.info(&format!( - "Include the Composer autoloader with: {}", - console::comment("require 'vendor/autoload.php';"), + console.info(&console_format!( + "Include the Composer autoloader with: require 'vendor/autoload.php';" )); } @@ -213,9 +208,9 @@ async fn build_interactive( .clone() .unwrap_or_else(|| get_default_package_name(working_dir)); let name = console.ask_validated( - &format!( - "Package name (/) [{}]", - mozart_core::console::comment(&default_name), + &console_format!( + "Package name (/) [{}]", + &default_name, ), &default_name, |val| { @@ -233,10 +228,7 @@ async fn build_interactive( // Description let default_desc = args.description.clone().unwrap_or_default(); let description = console.ask( - &format!( - "Description [{}]", - mozart_core::console::comment(&default_desc) - ), + &console_format!("Description [{}]", &default_desc), &default_desc, ); let description = if description.is_empty() { @@ -252,14 +244,11 @@ async fn build_interactive( .or_else(get_default_author) .unwrap_or_default(); let author_input = console.ask( - &format!( - "Author [{}n to skip]", - if !default_author.is_empty() { - format!("{}, ", mozart_core::console::comment(&default_author)) - } else { - String::new() - } - ), + &if !default_author.is_empty() { + console_format!("Author [{}, n to skip]", &default_author) + } else { + "Author [n to skip]".to_string() + }, &default_author, ); let authors = if author_input == "n" || author_input == "no" || author_input.is_empty() { @@ -277,9 +266,9 @@ async fn build_interactive( // Minimum Stability let default_stability = args.stability.clone().unwrap_or_default(); let stability_input = console.ask( - &format!( - "Minimum Stability [{}]", - mozart_core::console::comment(&default_stability), + &console_format!( + "Minimum Stability [{}]", + &default_stability ), &default_stability, ); @@ -297,9 +286,9 @@ async fn build_interactive( // Package Type let default_type = args.r#type.clone().unwrap_or_default(); let type_input = console.ask( - &format!( - "Package Type (e.g. library, project, metapackage, composer-plugin) [{}]", - mozart_core::console::comment(&default_type), + &console_format!( + "Package Type (e.g. library, project, metapackage, composer-plugin) [{}]", + &default_type, ), &default_type, ); @@ -312,10 +301,7 @@ async fn build_interactive( // License let default_license = args.license.clone().unwrap_or_default(); let license_input = console.ask( - &format!( - "License [{}]", - mozart_core::console::comment(&default_license), - ), + &console_format!("License [{}]", &default_license), &default_license, ); let license = if license_input.is_empty() { @@ -331,10 +317,7 @@ async fn build_interactive( .unwrap_or(Stability::Stable); console.info(""); - console.info(&format!( - "{}", - mozart_core::console::info("Define your dependencies.") - )); + console.info(&console_format!("Define your dependencies.")); console.info(""); let mut require = parse_requirements(&args.require)?; @@ -346,9 +329,8 @@ async fn build_interactive( // Dev Dependencies console.info(""); - console.info(&format!( - "{}", - mozart_core::console::info("Define your dev dependencies.") + console.info(&console_format!( + "Define your dev dependencies." )); console.info(""); @@ -368,10 +350,9 @@ async fn build_interactive( let default_autoload = args.autoload.clone().unwrap_or_else(|| "src/".to_string()); let namespace = validation::namespace_from_package_name(&name).unwrap_or_default(); let autoload_input = console.ask( - &format!( - "Add PSR-4 autoload mapping? Maps namespace \"{}\" to the entered relative path. [{}, n to skip]", - namespace, - mozart_core::console::comment(&default_autoload), + &console_format!( + "Add PSR-4 autoload mapping? Maps namespace \"{namespace}\" to the entered relative path. [{}, n to skip]", + &default_autoload, ), &default_autoload, ); @@ -437,7 +418,7 @@ async fn interactive_search_packages( Err(e) => { eprintln!( "{}", - console::warning(&format!("Search failed: {e}. Try again.")) + console_format!("Search failed: {e}. Try again.") ); continue; } @@ -456,9 +437,9 @@ async fn interactive_search_packages( if filtered.is_empty() { eprintln!( "{}", - console::warning(&format!( - "No new packages found for \"{query}\" (total: {total})." - )) + console_format!( + "No new packages found for \"{query}\" (total: {total})." + ) ); continue; } @@ -511,7 +492,10 @@ async fn interactive_search_packages( } else if num <= filtered.len() { filtered[num - 1].name.to_lowercase() } else { - eprintln!("{}", console::warning(&format!("Invalid selection: {num}"))); + eprintln!( + "{}", + console_format!("Invalid selection: {num}") + ); continue; } } else { @@ -523,7 +507,7 @@ async fn interactive_search_packages( match validation::parse_require_string(&package_name) { Ok((n, v)) => (n.to_lowercase(), v), Err(e) => { - eprintln!("{}", console::warning(&format!("Invalid: {e}"))); + eprintln!("{}", console_format!("Invalid: {e}")); continue; } } @@ -531,16 +515,16 @@ async fn interactive_search_packages( if !validation::validate_package_name(&package_name) { eprintln!( "{}", - console::warning(&format!("Invalid package name: \"{package_name}\"")) + console_format!("Invalid package name: \"{package_name}\"") ); continue; } eprintln!( "{}", - console::info(&format!( - "Using version constraint for {package_name} from Packagist..." - )) + console_format!( + "Using version constraint for {package_name} from Packagist..." + ) ); match packagist::fetch_package_versions(&package_name, None).await { @@ -555,17 +539,19 @@ async fn interactive_search_packages( ); eprintln!( "{}", - console::info(&format!("Using version {c} for {package_name}")) + console_format!( + "Using version {c} for {package_name}" + ) ); (package_name, c) } None => { eprintln!( "{}", - console::warning(&format!( - "Could not find a version of \"{package_name}\" matching \ - your minimum-stability. Try specifying it explicitly." - )) + console_format!( + "Could not find a version of \"{package_name}\" matching \ + your minimum-stability. Try specifying it explicitly." + ) ); continue; } @@ -574,9 +560,9 @@ async fn interactive_search_packages( Err(e) => { eprintln!( "{}", - console::warning(&format!( - "Could not fetch versions for \"{package_name}\": {e}" - )) + console_format!( + "Could not fetch versions for \"{package_name}\": {e}" + ) ); continue; } -- cgit v1.3.1