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] [package]
name = "path-convert" name = "path-convert"
version = "0.0.5" version = "0.0.6"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View file

@ -34,17 +34,20 @@ pub fn main() {
let no_spaces = args.contains(NO_SPACES); let no_spaces = args.contains(NO_SPACES);
let network_path = args.contains(NET_PATH); let network_path = args.contains(NET_PATH);
loop { let mut targets = args.finish();
let next = args.subcommand().unwrap(); // for empty args, just do cwd
if let Some(arg) = next { if targets.len() == 0 { targets.push(".".into()) ; }
let mut output: String; for target in targets {
let target = target.to_string_lossy().to_string();
// canonicalize; windows doesn't recognize symlinks // canonicalize; windows doesn't recognize symlinks
let path = Path::new(&arg).canonicalize(); let path = Path::new(&target).canonicalize();
if let Ok(target) = path { let mut output =
output = target.to_string_lossy().to_string(); if let Ok(canonical) = path {
canonical.to_string_lossy().to_string()
} else { } else {
output = arg; target
} };
// handle no-space flag // handle no-space flag
if no_spaces { if no_spaces {
@ -56,21 +59,21 @@ pub fn main() {
if part.is_empty() { continue; } if part.is_empty() { continue; }
let space = part.chars().position(|c| c == ' '); let space = part.chars().position(|c| c == ' ');
let len = part.len(); let len = part.len();
// if space is found, use short version // if a space is found, use short version
if let Some(index) = space { 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 cut = usize::min(len, index).min(6);
let replace = &(part[..cut]); let replace = &(part[..cut]);
// determine number for shortening // determine shortened index
let mut index = 0; let mut index = 0;
let children = current.read_dir(); let children = current.read_dir();
if !children.is_ok() { continue; } if !children.is_ok() { continue; }
for child in children.unwrap() { for child in children.unwrap() {
if let Ok(child) = child { if let Ok(child) = child {
let name: String = child.file_name().to_string_lossy().into(); let name: String = child.file_name().to_string_lossy().to_string();
// matches increment // matching names increment index
if name.to_lowercase().starts_with(&replace.to_lowercase()) { index += 1; } if name.to_lowercase().starts_with(&replace.to_lowercase()) { index += 1; }
// ... and break when we hit the target // ... and break when we hit the target
if name == part { break; } 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) { if output.starts_with(DRIVE) {
output = output.replace(DRIVE, "C:\\"); output = output.replace(DRIVE, "C:\\");
} else if network_path { } else if network_path {
@ -97,9 +100,6 @@ pub fn main() {
// emit to stdout // emit to stdout
println!("{quotes}{output}{quotes}"); println!("{quotes}{output}{quotes}");
} else {
break;
}
} }
} }