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]
|
[package]
|
||||||
name = "path-convert"
|
name = "path-convert"
|
||||||
version = "0.0.5"
|
version = "0.0.6"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
|
@ -25,81 +25,81 @@ pub fn main() {
|
||||||
|
|
||||||
// handle quote flag
|
// handle quote flag
|
||||||
let quotes: &str =
|
let quotes: &str =
|
||||||
if args.contains(QUOTED) {
|
if args.contains(QUOTED) {
|
||||||
if args.contains(QUOTED) { "\"" } // -qq -> "..." (output with double quotes)
|
if args.contains(QUOTED) { "\"" } // -qq -> "..." (output with double quotes)
|
||||||
else { "'" } // -q -> '...' (output with single quotes)
|
else { "'" } // -q -> '...' (output with single quotes)
|
||||||
} else { "" }; // _ -> ... (output with no quotes)
|
} else { "" }; // _ -> ... (output with no quotes)
|
||||||
|
|
||||||
// consume simple flags
|
// consume simple flags
|
||||||
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 {
|
||||||
// canonicalize; windows doesn't recognize symlinks
|
let target = target.to_string_lossy().to_string();
|
||||||
let path = Path::new(&arg).canonicalize();
|
|
||||||
if let Ok(target) = path {
|
// canonicalize; windows doesn't recognize symlinks
|
||||||
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 {
|
} else {
|
||||||
output = arg;
|
target
|
||||||
}
|
};
|
||||||
|
|
||||||
// handle no-space flag
|
// handle no-space flag
|
||||||
if no_spaces {
|
if no_spaces {
|
||||||
let tmp = output.clone();
|
let tmp = output.clone();
|
||||||
let parts = tmp.split('/');
|
let parts = tmp.split('/');
|
||||||
let mut current = PathBuf::new();
|
let mut current = PathBuf::new();
|
||||||
current.push("/");
|
current.push("/");
|
||||||
for part in parts {
|
for part in parts {
|
||||||
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; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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