2024-05-30 10:13:37 -04:00
|
|
|
use std::path::Path;
|
2024-05-30 09:03:46 -04:00
|
|
|
|
|
|
|
use pico_args::Arguments;
|
|
|
|
|
|
|
|
const DRIVE: &str = "/mnt/c/";
|
|
|
|
|
2024-06-05 10:04:44 -04:00
|
|
|
const HELP: [&str;2] = [ "-h", "--help" ];
|
2024-06-05 09:57:29 -04:00
|
|
|
const QUOTED: [&str;2] = [ "-q", "--quotes" ];
|
|
|
|
|
2024-05-30 09:03:46 -04:00
|
|
|
pub fn main() {
|
|
|
|
let mut args = Arguments::from_env();
|
|
|
|
|
2024-06-05 10:04:44 -04:00
|
|
|
// handle help flag
|
|
|
|
if args.contains(HELP) {
|
|
|
|
help_text();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-05 09:57:29 -04:00
|
|
|
// handle quote flag
|
|
|
|
let quotes: &str =
|
|
|
|
if args.contains(QUOTED) {
|
|
|
|
if args.contains(QUOTED) { "\"" } // -qq -> "..." (output with double quotes)
|
|
|
|
else { "'" } // -q -> '...' (output with single quotes)
|
|
|
|
} else { "" }; // _ -> ... (output with no quotes)
|
|
|
|
|
2024-05-30 09:03:46 -04:00
|
|
|
loop {
|
|
|
|
let next = args.subcommand().unwrap();
|
2024-05-30 10:13:37 -04:00
|
|
|
if let Some(arg) = next {
|
|
|
|
let mut output: String;
|
2024-05-30 15:44:02 -04:00
|
|
|
// canonicalize; windows doesn't recognize symlinks
|
2024-05-30 10:13:37 -04:00
|
|
|
let path = Path::new(&arg).canonicalize();
|
|
|
|
if let Ok(target) = path {
|
|
|
|
output = target.to_string_lossy().to_string();
|
|
|
|
} else {
|
|
|
|
output = arg;
|
2024-05-30 09:03:46 -04:00
|
|
|
}
|
2024-05-30 10:13:37 -04:00
|
|
|
|
2024-05-30 15:44:02 -04:00
|
|
|
// simple C-drive substitution
|
2024-05-30 10:13:37 -04:00
|
|
|
if output.starts_with(DRIVE) {
|
|
|
|
output = output.replace(DRIVE, "C:\\");
|
|
|
|
}
|
|
|
|
|
2024-05-30 15:44:02 -04:00
|
|
|
// switch out separators
|
2024-05-30 10:13:37 -04:00
|
|
|
output = output.replace("/", "\\");
|
|
|
|
|
2024-05-30 15:44:02 -04:00
|
|
|
// emit to stdout
|
2024-06-05 09:57:29 -04:00
|
|
|
println!("{quotes}{output}{quotes}");
|
2024-05-30 09:03:46 -04:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-05 10:04:44 -04:00
|
|
|
pub fn help_text() {
|
|
|
|
println!("path-convert v{}
|
|
|
|
Valerie Wolfe <sleeplessval@gmail.com>
|
|
|
|
Canonicalize and convert Unix paths for DOS programs.
|
|
|
|
|
|
|
|
usage: path-convert [flags] <paths...>
|
|
|
|
|
|
|
|
args:
|
|
|
|
<paths...> one or more paths to convert
|
|
|
|
|
|
|
|
flags:
|
|
|
|
-h, --help show this help text
|
|
|
|
-q, --quotes surround the output strings with quotes (-qq for double quotes)
|
|
|
|
", env!("CARGO_PKG_VERSION"));
|
|
|
|
}
|
2024-05-30 09:03:46 -04:00
|
|
|
|