Skip to main content

syncable_cli/handlers/
analyze.rs

1use crate::{
2    analyzer::analyze_monorepo,
3    analyzer::display::{
4        ColorScheme as DisplayColorScheme, DisplayMode, display_analysis_to_string,
5        display_analysis_with_return, init_color_adapter,
6    },
7    cli::{ColorScheme, DisplayFormat},
8};
9
10pub fn handle_analyze(
11    path: std::path::PathBuf,
12    json: bool,
13    detailed: bool,
14    display: Option<DisplayFormat>,
15    _only: Option<Vec<String>>,
16    color_scheme: Option<ColorScheme>,
17    quiet: bool,
18) -> crate::Result<String> {
19    // Initialize color adapter based on user preference
20    if let Some(scheme) = color_scheme {
21        let display_scheme = match scheme {
22            ColorScheme::Auto => {
23                // Let the color adapter auto-detect
24                DisplayColorScheme::Dark // This will be overridden by auto-detection in ColorAdapter::new()
25            }
26            ColorScheme::Dark => DisplayColorScheme::Dark,
27            ColorScheme::Light => DisplayColorScheme::Light,
28        };
29
30        // Only initialize if not auto - auto-detection happens by default
31        if !matches!(scheme, ColorScheme::Auto) {
32            init_color_adapter(display_scheme);
33        }
34    }
35
36    if !quiet {
37        println!("🔍 Analyzing project: {}", path.display());
38    }
39
40    let monorepo_analysis = analyze_monorepo(&path)?;
41
42    let mode = if json {
43        DisplayMode::Json
44    } else if detailed {
45        // Legacy flag for backward compatibility
46        DisplayMode::Detailed
47    } else {
48        match display {
49            Some(DisplayFormat::Matrix) | None => DisplayMode::Matrix,
50            Some(DisplayFormat::Detailed) => DisplayMode::Detailed,
51            Some(DisplayFormat::Summary) => DisplayMode::Summary,
52        }
53    };
54
55    let output = if quiet {
56        display_analysis_to_string(&monorepo_analysis, mode)
57    } else {
58        display_analysis_with_return(&monorepo_analysis, mode)
59    };
60
61    Ok(output)
62}