From d6e0c6d34449224ac3687daf551a0acfd15cee32 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 23 Feb 2026 15:11:36 +0900 Subject: refactor(cli): route command output through Console abstraction Replace direct println\!/eprintln\! calls with console.write(), console.info(), and console.write_stdout() across all command handlers to respect verbosity settings. Co-Authored-By: Claude Opus 4.6 --- crates/mozart/src/commands/suggests.rs | 72 +++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 31 deletions(-) (limited to 'crates/mozart/src/commands/suggests.rs') diff --git a/crates/mozart/src/commands/suggests.rs b/crates/mozart/src/commands/suggests.rs index 3fb2f00..1dd898f 100644 --- a/crates/mozart/src/commands/suggests.rs +++ b/crates/mozart/src/commands/suggests.rs @@ -1,5 +1,6 @@ use clap::Args; use mozart_core::console; +use mozart_core::console::Verbosity; use mozart_core::console_format; use std::collections::{BTreeMap, HashMap, HashSet}; use std::path::{Path, PathBuf}; @@ -43,7 +44,7 @@ struct Suggestion { pub async fn execute( args: &SuggestsArgs, cli: &super::Cli, - _console: &console::Console, + console: &console::Console, ) -> anyhow::Result<()> { let working_dir = match &cli.working_dir { Some(dir) => PathBuf::from(dir), @@ -134,26 +135,29 @@ pub async fn execute( let shown = filtered.len(); let diff = total_before_direct_filter.saturating_sub(shown); if diff > 0 { - println!( - "{} by transitive dependencies can be shown with {}", - console_format!("{diff} additional suggestions"), - console_format!("--all"), + console.write_stdout( + &format!( + "{} by transitive dependencies can be shown with {}", + console_format!("{diff} additional suggestions"), + console_format!("--all"), + ), + Verbosity::Normal, ); } } // 6. Render output if args.list { - render_list(&filtered); + render_list(&filtered, console); } else if args.by_suggestion && !args.by_package { - render_by_suggestion(&filtered); + render_by_suggestion(&filtered, console); } else if args.by_package && args.by_suggestion { - render_by_package(&filtered); - println!("{}", "-".repeat(78)); - render_by_suggestion(&filtered); + render_by_package(&filtered, console); + console.write_stdout(&"-".repeat(78), Verbosity::Normal); + render_by_suggestion(&filtered, console); } else { // Default: by-package - render_by_package(&filtered); + render_by_package(&filtered, console); } Ok(()) @@ -431,64 +435,70 @@ fn deduplicate_suggestions(suggestions: Vec) -> Vec { // ─── Rendering ─────────────────────────────────────────────────────────────── -fn render_list(suggestions: &[&Suggestion]) { +fn render_list(suggestions: &[&Suggestion], console: &console::Console) { let mut targets: Vec<&str> = suggestions.iter().map(|s| s.target.as_str()).collect(); targets.sort_unstable(); targets.dedup(); for t in targets { - println!("{}", console_format!("{}", t)); + console.write_stdout(&console_format!("{}", t), Verbosity::Normal); } } -fn render_by_package(suggestions: &[&Suggestion]) { +fn render_by_package(suggestions: &[&Suggestion], console: &console::Console) { // Group by source, preserving insertion order via BTreeMap (sorted) let mut grouped: BTreeMap<&str, Vec<&Suggestion>> = BTreeMap::new(); for s in suggestions { grouped.entry(s.source.as_str()).or_default().push(s); } for (source, items) in &grouped { - println!( - "{}", - console_format!("{} suggests:", source) + console.write_stdout( + &console_format!("{} suggests:", source), + Verbosity::Normal, ); for s in items { let reason = sanitize_reason(&s.reason); if reason.is_empty() { - println!("{}", console_format!(" - {}", &s.target)); + console.write_stdout( + &console_format!(" - {}", &s.target), + Verbosity::Normal, + ); } else { - println!( - "{}", - console_format!(" - {}: {}", &s.target, reason) + console.write_stdout( + &console_format!(" - {}: {}", &s.target, reason), + Verbosity::Normal, ); } } - println!(); + console.write_stdout("", Verbosity::Normal); } } -fn render_by_suggestion(suggestions: &[&Suggestion]) { +fn render_by_suggestion(suggestions: &[&Suggestion], console: &console::Console) { // Group by target let mut grouped: BTreeMap<&str, Vec<&Suggestion>> = BTreeMap::new(); for s in suggestions { grouped.entry(s.target.as_str()).or_default().push(s); } for (target, items) in &grouped { - println!( - "{}", - console_format!("{} is suggested by:", target) + console.write_stdout( + &console_format!("{} is suggested by:", target), + Verbosity::Normal, ); for s in items { let reason = sanitize_reason(&s.reason); if reason.is_empty() { - println!("{}", console_format!(" - {}", &s.source)); + console.write_stdout( + &console_format!(" - {}", &s.source), + Verbosity::Normal, + ); } else { - println!( - "{}", - console_format!(" - {}: {}", &s.source, reason) + console.write_stdout( + &console_format!(" - {}: {}", &s.source, reason), + Verbosity::Normal, ); } } - println!(); + console.write_stdout("", Verbosity::Normal); } } -- cgit v1.3.1