2024-06-05 14:12:42 -04:00
|
|
|
use std::path::{ Path, PathBuf };
|
2024-05-30 09:03:46 -04:00
|
|
|
|
|
|
|
use pico_args::Arguments;
|
|
|
|
|
2024-06-11 10:05:41 -04:00
|
|
|
// - HELPER -
|
2024-05-30 09:03:46 -04:00
|
|
|
const DRIVE: &str = "/mnt/c/";
|
|
|
|
|
2024-06-11 10:05:41 -04:00
|
|
|
// - ENV -
|
|
|
|
const DISTRO: &str = "WSL_DISTRO_NAME";
|
|
|
|
|
|
|
|
// - FLAGS -
|
2024-06-05 14:12:42 -04:00
|
|
|
const HELP: [&str;2] = [ "-h", "--help" ];
|
2024-06-11 10:12:18 -04:00
|
|
|
const NET_PATH: [&str;2] = [ "-n", "--network" ];
|
2024-06-05 14:12:42 -04:00
|
|
|
const NO_SPACES: [&str;2] = [ "-s", "--no-space" ];
|
|
|
|
const QUOTED: [&str;2] = [ "-q", "--quotes" ];
|
2024-06-05 09:57:29 -04:00
|
|
|
|
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 =
|
2024-06-17 08:45:44 -04:00
|
|
|
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-06-05 09:57:29 -04:00
|
|
|
|
2024-06-11 10:05:41 -04:00
|
|
|
// consume simple flags
|
2024-06-05 14:12:42 -04:00
|
|
|
let no_spaces = args.contains(NO_SPACES);
|
2024-06-11 10:05:41 -04:00
|
|
|
let network_path = args.contains(NET_PATH);
|
2024-06-05 14:12:42 -04:00
|
|
|
|
2024-06-17 08:45:44 -04:00
|
|
|
let mut targets = args.finish();
|
|
|
|
// for empty args, just do cwd
|
|
|
|
if targets.len() == 0 { targets.push(".".into()) ; }
|
|
|
|
for target in targets {
|
|
|
|
let target = target.to_string_lossy().to_string();
|
|
|
|
|
|
|
|
// canonicalize; windows doesn't recognize symlinks
|
|
|
|
let path = Path::new(&target).canonicalize();
|
|
|
|
let mut output =
|
|
|
|
if let Ok(canonical) = path {
|
|
|
|
canonical.to_string_lossy().to_string()
|
2024-05-30 10:13:37 -04:00
|
|
|
} else {
|
2024-06-17 08:45:44 -04:00
|
|
|
target
|
|
|
|
};
|
|
|
|
|
|
|
|
// handle no-space flag
|
|
|
|
if no_spaces {
|
|
|
|
let tmp = output.clone();
|
|
|
|
let parts = tmp.split('/');
|
|
|
|
let mut current = PathBuf::new();
|
|
|
|
current.push("/");
|
|
|
|
for part in parts {
|
|
|
|
if part.is_empty() { continue; }
|
|
|
|
let space = part.chars().position(|c| c == ' ');
|
|
|
|
let len = part.len();
|
|
|
|
// if a space is found, use short version
|
|
|
|
if let Some(index) = space {
|
|
|
|
// find cut point and slice
|
|
|
|
let cut = usize::min(len, index).min(6);
|
|
|
|
let replace = &(part[..cut]);
|
|
|
|
|
|
|
|
// determine shortened index
|
|
|
|
let mut index = 0;
|
|
|
|
let children = current.read_dir();
|
|
|
|
if !children.is_ok() { continue; }
|
|
|
|
|
|
|
|
for child in children.unwrap() {
|
|
|
|
if let Ok(child) = child {
|
|
|
|
let name: String = child.file_name().to_string_lossy().to_string();
|
|
|
|
// matching names increment index
|
|
|
|
if name.to_lowercase().starts_with(&replace.to_lowercase()) { index += 1; }
|
|
|
|
// ... and break when we hit the target
|
|
|
|
if name == part { break; }
|
2024-06-05 14:12:42 -04:00
|
|
|
}
|
|
|
|
}
|
2024-06-17 08:45:44 -04:00
|
|
|
output = output.replacen(part, &format!("{replace}~{index}"), 1);
|
2024-06-05 14:12:42 -04:00
|
|
|
}
|
2024-06-17 08:45:44 -04:00
|
|
|
current.push(part);
|
2024-06-05 14:12:42 -04:00
|
|
|
}
|
2024-06-17 08:45:44 -04:00
|
|
|
}
|
2024-06-05 14:12:42 -04:00
|
|
|
|
2024-06-17 08:45:44 -04:00
|
|
|
// very basic C-drive substitution
|
|
|
|
if output.starts_with(DRIVE) {
|
|
|
|
output = output.replace(DRIVE, "C:\\");
|
|
|
|
} else if network_path {
|
|
|
|
let name = std::env::var(DISTRO);
|
|
|
|
if let Ok(name) = name {
|
|
|
|
output = format!("//wsl.localhost/{name}{output}");
|
2024-05-30 10:13:37 -04:00
|
|
|
}
|
2024-06-17 08:45:44 -04:00
|
|
|
}
|
2024-05-30 10:13:37 -04:00
|
|
|
|
2024-06-17 08:45:44 -04:00
|
|
|
// switch out separators
|
|
|
|
output = output.replace("/", "\\");
|
2024-05-30 10:13:37 -04:00
|
|
|
|
2024-06-17 08:45:44 -04:00
|
|
|
// emit to stdout
|
|
|
|
println!("{quotes}{output}{quotes}");
|
2024-05-30 09:03:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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:
|
2024-06-11 10:12:18 -04:00
|
|
|
<paths...> One or more paths to convert
|
2024-06-05 10:04:44 -04:00
|
|
|
|
|
|
|
flags:
|
2024-06-11 10:12:18 -04:00
|
|
|
-h, --help Show this help text
|
|
|
|
-n, --network Add the network path where appropriate
|
|
|
|
-q, --quotes Surround the output strings with quotes (-qq for double quotes)
|
|
|
|
-s, --no-space Don't allow paths to have spaces
|
2024-06-05 10:04:44 -04:00
|
|
|
", env!("CARGO_PKG_VERSION"));
|
|
|
|
}
|
2024-05-30 09:03:46 -04:00
|
|
|
|