use anyhow::Result;
use owo_colors::OwoColorize;
use crate::clipboard;
use crate::converter::{Converter, Direction};
use crate::resolver;
use crate::ui;
#[derive(Debug, Default)]
pub struct ConvertArgs {
pub path: Option<String>,
pub to_windows: bool,
pub mixed: bool,
pub parent: bool,
pub force: bool,
pub verbose: bool,
}
pub mod exit {
pub const OK: i32 = 0;
pub const CONVERT_ERR: i32 = 2;
pub const PATH_NOT_EXIST: i32 = 3;
pub const CLIPBOARD_ERR: i32 = 4;
}
pub fn run(args: ConvertArgs) -> Result<i32> {
let input = match args.path {
Some(ref p) => p.clone(),
None => match clipboard::read_clipboard_path() {
Ok(s) => s,
Err(e) => {
ui::err(format!("{}", e));
return Ok(exit::CLIPBOARD_ERR);
}
},
};
let converter = Converter::new();
if args.to_windows {
let result = match converter.to_windows(&input, args.mixed) {
Ok(r) => r,
Err(e) => {
ui::err(format!("{}", e));
return Ok(exit::CONVERT_ERR);
}
};
if args.verbose {
print_verbose(&result.original, &result.converted, result.direction);
}
println!("{}", result.converted);
return Ok(exit::OK);
}
let result = match converter.to_wsl(&input) {
Ok(r) => r,
Err(e) => {
ui::err(format!("{}", e));
return Ok(exit::CONVERT_ERR);
}
};
if args.verbose {
print_verbose(&result.original, &result.converted, result.direction);
}
let resolved = resolver::resolve_path(&result.converted, args.parent, args.force);
let mut exit_code = exit::OK;
if !resolved.exact && !args.force {
eprintln!(
"{} 路径不存在: {}",
"警告:".yellow().bold(),
resolved.path.display()
);
if !resolved.suggestions.is_empty() {
eprintln!("{}", "可能的目录:".cyan());
for s in &resolved.suggestions {
eprintln!(" {}", s.display());
}
}
exit_code = exit::PATH_NOT_EXIST;
}
println!("{}", resolved.path.display());
Ok(exit_code)
}
fn print_verbose(original: &str, converted: &str, direction: Direction) {
let dir = match direction {
Direction::ToWsl => "Windows → WSL",
Direction::ToWindows => "WSL → Windows",
};
eprintln!("{} {}", "方向:".dimmed(), dir.dimmed());
eprintln!("{} {}", "原始:".dimmed(), original.dimmed());
eprintln!("{} {}", "转换:".dimmed(), converted.green());
eprintln!();
}