use crate::cli::{AliasManager, CliResult};
use colored::Colorize;
pub async fn list() -> CliResult<()> {
let manager = AliasManager::new().map_err(|e| format!("Failed to load aliases: {}", e))?;
let aliases = manager.list_aliases();
if aliases.is_empty() {
println!("{}", "No aliases configured.".yellow());
println!("\nUse 'oxirs alias add <name> <command>' to create an alias.");
return Ok(());
}
println!("{}", "Configured Aliases:".bold());
println!();
let _max_len = aliases.keys().map(|k| k.len()).max().unwrap_or(10);
let mut sorted_aliases: Vec<_> = aliases.iter().collect();
sorted_aliases.sort_by_key(|(name, _)| *name);
for (name, command) in sorted_aliases {
println!(
" {} {} {}",
name.cyan().bold(),
"→".dimmed(),
command.white()
);
}
println!();
println!("{}", format!("Total: {} aliases", aliases.len()).dimmed());
Ok(())
}
pub async fn show(name: String) -> CliResult<()> {
let manager = AliasManager::new().map_err(|e| format!("Failed to load aliases: {}", e))?;
match manager.get_alias(&name) {
Some(command) => {
println!(
"{} {} {}",
name.cyan().bold(),
"→".dimmed(),
command.white()
);
Ok(())
}
None => Err(format!("Alias '{}' not found", name).into()),
}
}
pub async fn add(name: String, command: String) -> CliResult<()> {
let mut manager = AliasManager::new().map_err(|e| format!("Failed to load aliases: {}", e))?;
let is_update = manager.get_alias(&name).is_some();
manager
.add_alias(name.clone(), command.clone())
.map_err(|e| format!("Failed to add alias: {}", e))?;
if is_update {
println!(
"{} {} {} {}",
"Updated alias".green(),
name.cyan().bold(),
"→".dimmed(),
command.white()
);
} else {
println!(
"{} {} {} {}",
"Added alias".green(),
name.cyan().bold(),
"→".dimmed(),
command.white()
);
}
println!("\n{}", "You can now use this alias in commands.".dimmed());
Ok(())
}
pub async fn remove(name: String) -> CliResult<()> {
let mut manager = AliasManager::new().map_err(|e| format!("Failed to load aliases: {}", e))?;
let removed = manager
.remove_alias(&name)
.map_err(|e| format!("Failed to remove alias: {}", e))?;
if removed {
println!("{} {}", "Removed alias".green(), name.cyan().bold());
Ok(())
} else {
Err(format!("Alias '{}' not found", name).into())
}
}
pub async fn reset() -> CliResult<()> {
let mut manager = AliasManager::new().map_err(|e| format!("Failed to load aliases: {}", e))?;
manager
.reset_to_defaults()
.map_err(|e| format!("Failed to reset aliases: {}", e))?;
println!("{}", "Reset aliases to defaults".green());
println!("\nDefault aliases:");
list().await?;
Ok(())
}