mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-18 22:31:35 +03:00
feat: support environment variable in cd
path (#241)
This commit is contained in:
parent
47e6fba677
commit
df5fd4c964
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1542,6 +1542,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"ratatui",
|
"ratatui",
|
||||||
|
"regex",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -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"}
|
||||||
|
@ -10,4 +10,5 @@ futures = "^0"
|
|||||||
libc = "^0"
|
libc = "^0"
|
||||||
parking_lot = "^0"
|
parking_lot = "^0"
|
||||||
ratatui = { version = "^0" }
|
ratatui = { version = "^0" }
|
||||||
|
regex = "^1"
|
||||||
tokio = { version = "^1", features = [ "parking_lot", "macros", "rt-multi-thread", "sync", "time", "fs" ] }
|
tokio = { version = "^1", features = [ "parking_lot", "macros", "rt-multi-thread", "sync", "time", "fs" ] }
|
||||||
|
@ -5,12 +5,32 @@ use tokio::fs;
|
|||||||
use crate::Url;
|
use crate::Url;
|
||||||
|
|
||||||
pub fn expand_path(p: impl AsRef<Path>) -> PathBuf {
|
pub fn expand_path(p: impl AsRef<Path>) -> PathBuf {
|
||||||
let p = p.as_ref();
|
// ${HOME} or $HOME
|
||||||
|
#[cfg(unix)]
|
||||||
|
let re = regex::Regex::new(r"\$(?:\{([^}]+)\}|([a-zA-Z\d_]+))").unwrap();
|
||||||
|
|
||||||
|
// %USERPROFILE%
|
||||||
|
#[cfg(windows)]
|
||||||
|
let re = regex::Regex::new(r"%([^%]+)%").unwrap();
|
||||||
|
|
||||||
|
let s = p.as_ref().to_string_lossy();
|
||||||
|
let s = re.replace_all(&s, |caps: ®ex::Captures| {
|
||||||
|
let name = caps.get(2).or_else(|| caps.get(1)).unwrap();
|
||||||
|
env::var(name.as_str()).unwrap_or_else(|_| caps.get(0).unwrap().as_str().to_owned())
|
||||||
|
});
|
||||||
|
|
||||||
|
let p = Path::new(s.as_ref());
|
||||||
if let Ok(p) = p.strip_prefix("~") {
|
if let Ok(p) = p.strip_prefix("~") {
|
||||||
|
#[cfg(unix)]
|
||||||
if let Some(home) = env::var_os("HOME") {
|
if let Some(home) = env::var_os("HOME") {
|
||||||
return PathBuf::from_iter([&home, p.as_os_str()]);
|
return Path::new(&home).join(p);
|
||||||
|
}
|
||||||
|
#[cfg(windows)]
|
||||||
|
if let Some(home) = env::var_os("USERPROFILE") {
|
||||||
|
return Path::new(&home).join(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.is_absolute() {
|
if p.is_absolute() {
|
||||||
return p.to_path_buf();
|
return p.to_path_buf();
|
||||||
}
|
}
|
||||||
@ -48,7 +68,7 @@ pub fn short_path<'a>(p: &'a Path, base: &Path) -> ShortPath<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn readable_path(p: &Path) -> String {
|
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) {
|
if let Ok(p) = p.strip_prefix(home) {
|
||||||
return format!("~/{}", p.display());
|
return format!("~/{}", p.display());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user