path-convert: no target converts pwd
This commit is contained in:
parent
67ec5ec90b
commit
c097159c05
2 changed files with 62 additions and 62 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "path-convert"
|
||||
version = "0.0.5"
|
||||
version = "0.0.6"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
|
|
@ -25,81 +25,81 @@ pub fn main() {
|
|||
|
||||
// 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)
|
||||
if args.contains(QUOTED) {
|
||||
if args.contains(QUOTED) { "\"" } // -qq -> "..." (output with double quotes)
|
||||
else { "'" } // -q -> '...' (output with single quotes)
|
||||
} else { "" }; // _ -> ... (output with no quotes)
|
||||
|
||||
// consume simple flags
|
||||
let no_spaces = args.contains(NO_SPACES);
|
||||
let network_path = args.contains(NET_PATH);
|
||||
|
||||
loop {
|
||||
let next = args.subcommand().unwrap();
|
||||
if let Some(arg) = next {
|
||||
let mut output: String;
|
||||
// canonicalize; windows doesn't recognize symlinks
|
||||
let path = Path::new(&arg).canonicalize();
|
||||
if let Ok(target) = path {
|
||||
output = target.to_string_lossy().to_string();
|
||||
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()
|
||||
} else {
|
||||
output = arg;
|
||||
}
|
||||
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 space is found, use short version
|
||||
if let Some(index) = space {
|
||||
// find cut point and make slice
|
||||
let cut = usize::min(len, index).min(6);
|
||||
let replace = &(part[..cut]);
|
||||
// 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 number for shortening
|
||||
let mut index = 0;
|
||||
let children = current.read_dir();
|
||||
if !children.is_ok() { continue; }
|
||||
// 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().into();
|
||||
// matches increment
|
||||
if name.to_lowercase().starts_with(&replace.to_lowercase()) { index += 1; }
|
||||
// ... and break when we hit the target
|
||||
if name == part { break; }
|
||||
}
|
||||
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; }
|
||||
}
|
||||
output = output.replacen(part, &format!("{replace}~{index}"), 1);
|
||||
}
|
||||
current.push(part);
|
||||
output = output.replacen(part, &format!("{replace}~{index}"), 1);
|
||||
}
|
||||
current.push(part);
|
||||
}
|
||||
|
||||
// simple 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}");
|
||||
}
|
||||
}
|
||||
|
||||
// switch out separators
|
||||
output = output.replace("/", "\\");
|
||||
|
||||
// emit to stdout
|
||||
println!("{quotes}{output}{quotes}");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
// 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}");
|
||||
}
|
||||
}
|
||||
|
||||
// switch out separators
|
||||
output = output.replace("/", "\\");
|
||||
|
||||
// emit to stdout
|
||||
println!("{quotes}{output}{quotes}");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue