From 3544578a1203b67f7af8088384fd370df5ebc8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Fri, 8 Sep 2023 23:25:14 +0800 Subject: [PATCH] fix: respect symlink paths without canonicalizing them (#126) --- app/src/app.rs | 4 ++-- app/src/header/layout.rs | 6 +++--- app/src/manager/folder.rs | 4 ++-- config/src/boot/boot.rs | 5 ++--- config/src/preview/preview.rs | 4 ++-- config/src/theme/theme.rs | 4 ++-- config/src/xdg.rs | 4 ++-- shared/src/fns.rs | 12 ++++++------ 8 files changed, 21 insertions(+), 22 deletions(-) diff --git a/app/src/app.rs b/app/src/app.rs index 54328bf0..3a399f87 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -4,7 +4,7 @@ use std::ffi::OsString; use anyhow::{Ok, Result}; use config::{keymap::{Control, Key, KeymapLayer}, BOOT}; use crossterm::event::KeyEvent; -use shared::{absolute_url, Term}; +use shared::{expand_url, Term}; use tokio::sync::oneshot; use crate::{Ctx, Executor, Logs, Root, Signals}; @@ -121,7 +121,7 @@ impl App { match event { Event::Cd(url) => { futures::executor::block_on(async { - manager.active_mut().cd(absolute_url(url)).await; + manager.active_mut().cd(expand_url(url)).await; }); } Event::Refresh => { diff --git a/app/src/header/layout.rs b/app/src/header/layout.rs index a98ddec8..15695166 100644 --- a/app/src/header/layout.rs +++ b/app/src/header/layout.rs @@ -1,5 +1,5 @@ use ratatui::{layout, prelude::{Buffer, Constraint, Direction, Rect}, style::{Color, Style}, widgets::{Paragraph, Widget}}; -use shared::readable_home; +use shared::readable_path; use super::Tabs; use crate::Ctx; @@ -21,9 +21,9 @@ impl<'a> Widget for Layout<'a> { let current = &self.cx.manager.current(); let location = if current.cwd.is_search() { - format!("{} (search)", readable_home(¤t.cwd)) + format!("{} (search)", readable_path(¤t.cwd)) } else { - readable_home(¤t.cwd) + readable_path(¤t.cwd) }; Paragraph::new(location).style(Style::new().fg(Color::Cyan)).render(chunks[0], buf); diff --git a/app/src/manager/folder.rs b/app/src/manager/folder.rs index 99290990..582f9324 100644 --- a/app/src/manager/folder.rs +++ b/app/src/manager/folder.rs @@ -2,7 +2,7 @@ use core::files::File; use config::{MANAGER, THEME}; use ratatui::{buffer::Buffer, layout::Rect, style::Style, widgets::{List, ListItem, Widget}}; -use shared::readable_path; +use shared::short_path; use crate::Ctx; @@ -87,7 +87,7 @@ impl<'a> Widget for Folder<'a> { self.file_style(f) }; - let mut path = format!(" {icon} {}", readable_path(f.url(), &self.folder.cwd)); + let mut path = format!(" {icon} {}", short_path(f.url(), &self.folder.cwd)); if let Some(link_to) = f.link_to() { if MANAGER.show_symlink { path.push_str(&format!(" -> {}", link_to.display())); diff --git a/config/src/boot/boot.rs b/config/src/boot/boot.rs index 19fa9271..3f81ccae 100644 --- a/config/src/boot/boot.rs +++ b/config/src/boot/boot.rs @@ -1,7 +1,7 @@ use std::{env, fs, path::PathBuf, process}; use clap::{command, Parser}; -use shared::absolute_path; +use shared::expand_path; use crate::{Xdg, PREVIEW}; @@ -38,8 +38,7 @@ impl Default for Boot { fn default() -> Self { let args = Args::parse(); - let cwd = - args.cwd.map(absolute_path).filter(|p| p.is_dir()).or_else(|| env::current_dir().ok()); + let cwd = args.cwd.map(expand_path).filter(|p| p.is_dir()).or_else(|| env::current_dir().ok()); let boot = Self { cwd: cwd.unwrap_or("/".into()), diff --git a/config/src/preview/preview.rs b/config/src/preview/preview.rs index 5480fbdf..6bde131e 100644 --- a/config/src/preview/preview.rs +++ b/config/src/preview/preview.rs @@ -2,7 +2,7 @@ use std::{path::{Path, PathBuf}, time::{self, SystemTime}}; use md5::{Digest, Md5}; use serde::Deserialize; -use shared::absolute_path; +use shared::expand_path; use super::PreviewAdaptor; use crate::{xdg::Xdg, MERGED_YAZI}; @@ -36,7 +36,7 @@ impl Default for Preview { let preview = toml::from_str::(&MERGED_YAZI).unwrap().preview; let cache_dir = - preview.cache_dir.filter(|p| !p.is_empty()).map_or_else(Xdg::cache_dir, absolute_path); + preview.cache_dir.filter(|p| !p.is_empty()).map_or_else(Xdg::cache_dir, expand_path); Preview { adaptor: Default::default(), diff --git a/config/src/theme/theme.rs b/config/src/theme/theme.rs index 68088d16..4ae4cd09 100644 --- a/config/src/theme/theme.rs +++ b/config/src/theme/theme.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use serde::Deserialize; -use shared::absolute_path; +use shared::expand_path; use validator::Validate; use super::{ColorGroup, Filetype, Icon, Style}; @@ -78,7 +78,7 @@ impl Default for Theme { check_validation(theme.tab.validate()); - theme.preview.syntect_theme = absolute_path(&theme.preview.syntect_theme); + theme.preview.syntect_theme = expand_path(&theme.preview.syntect_theme); theme } diff --git a/config/src/xdg.rs b/config/src/xdg.rs index 4f213244..48edcd62 100644 --- a/config/src/xdg.rs +++ b/config/src/xdg.rs @@ -1,13 +1,13 @@ use std::{env, path::PathBuf}; -use shared::absolute_path; +use shared::expand_path; pub(super) struct Xdg; impl Xdg { pub(super) fn config_dir() -> Option { if let Some(s) = env::var_os("YAZI_CONFIG_HOME").filter(|s| !s.is_empty()) { - return Some(absolute_path(s)); + return Some(expand_path(s)); } #[cfg(target_os = "windows")] diff --git a/shared/src/fns.rs b/shared/src/fns.rs index 11fff35d..63b3cf96 100644 --- a/shared/src/fns.rs +++ b/shared/src/fns.rs @@ -4,30 +4,30 @@ use tokio::fs; use crate::Url; -pub fn absolute_path(p: impl AsRef) -> PathBuf { +pub fn expand_path(p: impl AsRef) -> PathBuf { let p = p.as_ref(); if let Ok(p) = p.strip_prefix("~") { if let Some(home) = env::var_os("HOME") { return PathBuf::from_iter([&home, p.as_os_str()]); } } - std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()) + p.to_path_buf() } #[inline] -pub fn absolute_url(mut u: Url) -> Url { - u.set_path(absolute_path(&u)); +pub fn expand_url(mut u: Url) -> Url { + u.set_path(expand_path(&u)); u } -pub fn readable_path(p: &Path, base: &Path) -> String { +pub fn short_path(p: &Path, base: &Path) -> String { if let Ok(p) = p.strip_prefix(base) { return p.display().to_string(); } p.display().to_string() } -pub fn readable_home(p: &Path) -> String { +pub fn readable_path(p: &Path) -> String { if let Ok(home) = env::var("HOME") { if let Ok(p) = p.strip_prefix(home) { return format!("~/{}", p.display());