mudra-cli 0.1.0

A robust, high-performance currency converter with caching and CLI interface
Documentation
use clap::Parser;
use colored::*;
use mudra_cli::{CurrencyConverter, Result};

mod cli;
use cli::{Cli, CliHandler};

#[tokio::main]
async fn main() -> Result<()> {
    // Parse command line arguments
    let cli = Cli::parse();

    // Print banner
    print_banner(cli.verbose);

    // Handle colors
    let use_color = !cli.no_color && supports_color();

    // Initialize converter
    let converter = match CurrencyConverter::from_env() {
        Ok(converter) => converter,
        Err(e) => {
            print_setup_help(&e, use_color);
            return Err(e);
        }
    };

    // Create CLI handler
    let handler = CliHandler::new(converter, cli.verbose, use_color);

    // Handle the command
    if let Err(e) = handler.handle_command(cli.command).await {
        if cli.verbose {
            eprintln!("{} {}", "❌ Error:".red(), e.to_string().red());
        }
        std::process::exit(1);
    }

    Ok(())
}

/// Print application banner
fn print_banner(verbose: bool) {
    if verbose {
        println!("{}", "🪙 Mudra - Currency Converter v0.1.0".bold().blue());
        println!(
            "{}",
            "Real-time currency conversion with exchange rates".dimmed()
        );
        println!();
    }
}

/// Check if terminal supports colors
fn supports_color() -> bool {
    // Simple color support detection
    std::env::var("NO_COLOR").is_err()
        && std::env::var("TERM")
            .map(|term| term != "dumb")
            .unwrap_or(true)
}

/// Print setup help when API key is missing
fn print_setup_help(error: &mudra_cli::CurrencyError, use_color: bool) {
    let colorize = |text: &str, color: &str| -> String {
        if !use_color {
            return text.to_string();
        }

        match color {
            "red" => text.red().to_string(),
            "green" => text.green().to_string(),
            "yellow" => text.yellow().to_string(),
            "blue" => text.blue().to_string(),
            "cyan" => text.cyan().to_string(),
            "bold" => text.bold().to_string(),
            _ => text.to_string(),
        }
    };

    println!("{}", colorize("❌ Setup Required", "red"));
    println!();
    println!("{} {}", colorize("Error:", "red"), error);
    println!();
    println!("{}", colorize("🛠️  Quick Setup:", "bold"));
    println!();
    println!("  {} Get a free API key from:", colorize("1.", "cyan"));
    println!("    {}", colorize("https://exchangerate-api.com", "blue"));
    println!(
        "    {}",
        colorize("(Free tier: 1,500 requests/month)", "yellow")
    );
    println!();
    println!("  {} Set your API key:", colorize("2.", "cyan"));
    println!(
        "    {}",
        colorize("export EXCHANGE_API_KEY=your_api_key_here", "green")
    );
    println!();
    println!("  {} Run the converter:", colorize("3.", "cyan"));
    println!("    {}", colorize("mudra convert 100 USD EUR", "green"));
    println!();
    println!(
        "{}",
        colorize(
            "💡 Your API key is never stored and only used for requests!",
            "yellow"
        )
    );
}