path-convert: no target converts pwd

This commit is contained in:
Valerie Wolfe 2024-06-17 08:45:44 -04:00
parent 67ec5ec90b
commit c097159c05
2 changed files with 62 additions and 62 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "path-convert"
version = "0.0.5"
version = "0.0.6"
edition = "2021"
[dependencies]

View file

@ -34,17 +34,20 @@ pub fn main() {
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;
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(&arg).canonicalize();
if let Ok(target) = path {
output = target.to_string_lossy().to_string();
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 {
@ -56,21 +59,21 @@ pub fn main() {
if part.is_empty() { continue; }
let space = part.chars().position(|c| c == ' ');
let len = part.len();
// if space is found, use short version
// if a space is found, use short version
if let Some(index) = space {
// find cut point and make slice
// find cut point and slice
let cut = usize::min(len, index).min(6);
let replace = &(part[..cut]);
// determine number for shortening
// 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
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; }
@ -82,7 +85,7 @@ pub fn main() {
}
}
// simple C-drive substitution
// very basic C-drive substitution
if output.starts_with(DRIVE) {
output = output.replace(DRIVE, "C:\\");
} else if network_path {
@ -97,9 +100,6 @@ pub fn main() {
// emit to stdout
println!("{quotes}{output}{quotes}");
} else {
break;
}
}
}