This commit is contained in:
sxyazi 2023-10-10 02:42:54 +08:00
parent bc9fb8e302
commit 0d4bb60922
No known key found for this signature in database
4 changed files with 20 additions and 33 deletions

1
Cargo.lock generated
View file

@ -1542,6 +1542,7 @@ dependencies = [
"libc",
"parking_lot",
"ratatui",
"regex",
"tokio",
]

View file

@ -1 +1 @@
{"version":"0.2","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp"," Überzug"," Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","backstack","natsort","natsort"],"language":"en"}
{"flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp"," Überzug"," Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","backstack","natsort","natsort","USERPROFILE"],"language":"en","version":"0.2"}

View file

@ -10,4 +10,5 @@ futures = "^0"
libc = "^0"
parking_lot = "^0"
ratatui = { version = "^0" }
regex = "^1"
tokio = { version = "^1", features = [ "parking_lot", "macros", "rt-multi-thread", "sync", "time", "fs" ] }

View file

@ -5,41 +5,26 @@ use tokio::fs;
use crate::Url;
pub fn expand_path(p: impl AsRef<Path>) -> PathBuf {
let mut p = p.as_ref();
let p = p.as_ref().to_string_lossy();
// expand the environment variable by calling the "echo" command, in linux case,
// this also expands the '~' path
#[cfg(target_os = "windows")]
let expanded_path = match std::process::Command::new("cmd").args(&["/C", "echo"]).arg(p).output() {
Ok(output) if output.status.success() => Some(
String::from_utf8_lossy(&output.stdout)
.trim_end()
.trim_matches('"')
.replace("\\\"", "\"")
.to_string(),
),
_ => None,
};
#[cfg(not(target_os = "windows"))]
let expanded_path = match std::process::Command::new("sh")
.arg("-c")
.arg(format!("echo \"{}\"", p.to_string_lossy().replace("\"", "\\\"")))
.output()
{
Ok(output) if output.status.success() => {
Some(String::from_utf8_lossy(&output.stdout).trim_end().to_string())
}
_ => None,
};
#[cfg(unix)]
let re = regex::Regex::new(r"\$([a-zA-Z0-9_]+)").unwrap();
#[cfg(windows)]
let re = regex::Regex::new(r"%([^%]+)%").unwrap();
if let Some(s) = &expanded_path {
p = Path::new(s);
}
let expanded = re.replace_all(&p, |caps: &regex::Captures| {
env::var(caps.get(1).unwrap().as_str())
.unwrap_or_else(|_| caps.get(0).unwrap().as_str().to_string())
});
// support '~' on Windows
#[cfg(target_os = "windows")]
let p = Path::new(expanded.as_ref());
if let Ok(p) = p.strip_prefix("~") {
if let Ok(home) = env::var("USERPROFILE") {
#[cfg(unix)]
if let Some(home) = env::var_os("HOME") {
return Path::new(&home).join(p);
}
#[cfg(windows)]
if let Some(home) = env::var_os("USERPROFILE") {
return Path::new(&home).join(p);
}
}
@ -81,7 +66,7 @@ pub fn short_path<'a>(p: &'a Path, base: &Path) -> ShortPath<'a> {
}
pub fn readable_path(p: &Path) -> String {
if let Ok(home) = env::var("HOME") {
if let Some(home) = env::var_os("HOME") {
if let Ok(p) = p.strip_prefix(home) {
return format!("~/{}", p.display());
}