Skip to main content

broom/
inspect_command.rs

1use crate::discover::{DiscoverOptions, discover_targets};
2use crate::report::{OutputFormat, format_bytes};
3use anyhow::Result;
4use runemark::{ColorMode, Console, Tone};
5use std::io::Write;
6use std::path::Path;
7
8pub fn run_inspect(
9    root_path: &Path,
10    output_format: OutputFormat,
11    color_mode: ColorMode,
12    out: &mut dyn Write,
13) -> Result<()> {
14    let options = DiscoverOptions::default();
15    let targets = discover_targets(root_path, &options)?;
16
17    if output_format == OutputFormat::Json {
18        let json = serde_json::to_string_pretty(&targets)?;
19        writeln!(out, "{}", json)?;
20        return Ok(());
21    }
22
23    let console = Console::new(color_mode, true);
24    let title = console.paint(
25        Tone::Title,
26        format!("cargo-broom Inspect — {}", root_path.display()),
27    );
28    writeln!(out, "{}", title)?;
29
30    let total_size: u64 = targets.iter().map(|t| t.size_bytes).sum();
31    let summary_msg = console.paint(
32        Tone::Muted,
33        format!(
34            "Found {} Rust target directories occupying {}",
35            targets.len(),
36            format_bytes(total_size)
37        ),
38    );
39    writeln!(out, "{}", summary_msg)?;
40    writeln!(out)?;
41
42    for t in &targets {
43        let override_note = if t.has_target_override {
44            " [Target Override]"
45        } else {
46            ""
47        };
48        writeln!(
49            out,
50            " • {:<25} {:>10}  {}{}",
51            t.project_name,
52            format_bytes(t.size_bytes),
53            t.target_path.display(),
54            override_note
55        )?;
56    }
57
58    Ok(())
59}