use clap::Parser;
use colored::*;
use mudra_cli::{CurrencyConverter, Result};
mod cli;
use cli::{Cli, CliHandler};
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
print_banner(cli.verbose);
let use_color = !cli.no_color && supports_color();
let converter = match CurrencyConverter::from_env() {
Ok(converter) => converter,
Err(e) => {
print_setup_help(&e, use_color);
return Err(e);
}
};
let handler = CliHandler::new(converter, cli.verbose, use_color);
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(())
}
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!();
}
}
fn supports_color() -> bool {
std::env::var("NO_COLOR").is_err()
&& std::env::var("TERM")
.map(|term| term != "dumb")
.unwrap_or(true)
}
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"
)
);
}