feat: show symlink path (#67)

This commit is contained in:
XYenon 2023-08-17 14:58:46 +08:00 committed by sxyazi
parent d3a0d56ce6
commit 1135c13d0f
No known key found for this signature in database
5 changed files with 22 additions and 4 deletions

View File

@ -1,6 +1,6 @@
use core::files::File;
use config::THEME;
use config::{MANAGER, THEME};
use ratatui::{buffer::Buffer, layout::Rect, style::Style, widgets::{List, ListItem, Widget}};
use shared::readable_path;
@ -80,7 +80,14 @@ impl<'a> Widget for Folder<'a> {
self.file_style(v)
};
ListItem::new(format!(" {icon} {}", readable_path(k, &self.folder.cwd))).style(style)
let mut path = format!(" {icon} {}", readable_path(k, &self.folder.cwd));
if let Some(ref link_to) = v.link_to {
if MANAGER.show_symlink {
path.push_str(&format!(" -> {}", link_to.display()));
}
}
ListItem::new(path).style(style)
})
.collect::<Vec<_>>();

View File

@ -24,6 +24,11 @@
- `true`: Show
- `false`: Do not show
- show_symlink: Show the path of the symlink file point to, after the filename
- `true`: Show
- `false`: Do not show
## preview
- tab_size: Tab width

View File

@ -3,6 +3,7 @@ sort_by = "modified"
sort_reverse = true
sort_dir_first = true
show_hidden = false
show_symlink = true
[preview]
tab_size = 2

View File

@ -11,7 +11,8 @@ pub struct Manager {
pub sort_dir_first: bool,
// Display
pub show_hidden: bool,
pub show_hidden: bool,
pub show_symlink: bool,
}
impl Default for Manager {

View File

@ -8,6 +8,7 @@ pub struct File {
pub path: PathBuf,
pub meta: Metadata,
pub length: Option<u64>,
pub link_to: Option<PathBuf>,
pub is_link: bool,
pub is_hidden: bool,
pub is_selected: bool,
@ -22,13 +23,16 @@ impl File {
pub async fn from_meta(path: &Path, mut meta: Metadata) -> File {
let is_link = meta.is_symlink();
let mut link_to = None;
if is_link {
meta = fs::metadata(&path).await.unwrap_or(meta);
link_to = fs::read_link(&path).await.ok();
}
let length = if meta.is_dir() { None } else { Some(meta.len()) };
let is_hidden = path.file_name().map(|s| s.to_string_lossy().starts_with('.')).unwrap_or(false);
File { path: path.to_path_buf(), meta, length, is_link, is_hidden, is_selected: false }
File { path: path.to_path_buf(), meta, length, link_to, is_link, is_hidden, is_selected: false }
}
}