From 06cb708a80f3fd57d04aeb71bffdc595ab75a296 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 11 Jul 2023 18:43:47 +0800 Subject: [PATCH] feat: show full name relative to cwd on searching --- src/core/files/file.rs | 6 ++---- src/core/files/files.rs | 2 +- src/misc/fns.rs | 9 ++++++++- src/ui/header/layout.rs | 6 +++--- src/ui/manager/folder.rs | 7 ++++--- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/core/files/file.rs b/src/core/files/file.rs index 97e95087..213d3ed4 100644 --- a/src/core/files/file.rs +++ b/src/core/files/file.rs @@ -5,7 +5,6 @@ use tokio::fs; #[derive(Clone, Debug)] pub struct File { - pub name: String, pub path: PathBuf, pub meta: Metadata, pub length: Option, @@ -27,9 +26,8 @@ impl File { meta = fs::metadata(&path).await.unwrap_or(meta); } - let name = path.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); let length = if meta.is_dir() { None } else { Some(meta.len()) }; - let is_hidden = name.starts_with('.'); - File { name, path: path.to_path_buf(), meta, length, is_link, is_hidden, is_selected: false } + 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 } } } diff --git a/src/core/files/files.rs b/src/core/files/files.rs index 3fa0c05c..fdae11cb 100644 --- a/src/core/files/files.rs +++ b/src/core/files/files.rs @@ -45,7 +45,7 @@ impl Files { let reverse = self.sort.reverse; match self.sort.by { - SortBy::Alphabetical => self.items.sort_by(|_, a, _, b| cmp(&a.name, &b.name, reverse)), + SortBy::Alphabetical => self.items.sort_by(|_, a, _, b| cmp(&a.path, &b.path, reverse)), SortBy::Created => self.items.sort_by(|_, a, _, b| { if let (Ok(a), Ok(b)) = (a.meta.created(), b.meta.created()) { return cmp(a, b, reverse); diff --git a/src/misc/fns.rs b/src/misc/fns.rs index 875f4d33..fdae4e7c 100644 --- a/src/misc/fns.rs +++ b/src/misc/fns.rs @@ -31,7 +31,14 @@ pub fn absolute_path(p: &Path) -> PathBuf { p.to_path_buf() } -pub fn readable_path(p: &Path) -> String { +pub fn readable_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 { if let Ok(home) = env::var("HOME") { if let Ok(p) = p.strip_prefix(home) { return format!("~/{}", p.display()); diff --git a/src/ui/header/layout.rs b/src/ui/header/layout.rs index d9658a49..5856d790 100644 --- a/src/ui/header/layout.rs +++ b/src/ui/header/layout.rs @@ -1,7 +1,7 @@ use ratatui::{buffer::Buffer, layout::{self, Constraint, Direction, Rect}, style::{Color, Style}, widgets::{Paragraph, Widget}}; use super::tabs::Tabs; -use crate::{misc::readable_path, ui::Ctx}; +use crate::{misc::readable_home, ui::Ctx}; pub struct Layout<'a> { cx: &'a Ctx, @@ -20,9 +20,9 @@ impl<'a> Widget for Layout<'a> { let current = &self.cx.manager.current(); let location = if current.in_search { - format!("{} (search)", readable_path(¤t.cwd)) + format!("{} (search)", readable_home(¤t.cwd)) } else { - format!("{}", readable_path(¤t.cwd)) + format!("{}", readable_home(¤t.cwd)) }; Paragraph::new(location).style(Style::default().fg(Color::Cyan)).render(chunks[0], buf); diff --git a/src/ui/manager/folder.rs b/src/ui/manager/folder.rs index 445daefa..22e84961 100644 --- a/src/ui/manager/folder.rs +++ b/src/ui/manager/folder.rs @@ -1,6 +1,6 @@ use ratatui::{buffer::Buffer, layout::Rect, style::{Color, Modifier, Style}, widgets::{List, ListItem, Widget}}; -use crate::{config::THEME, core}; +use crate::{config::THEME, core, misc::readable_path}; pub struct Folder<'a> { folder: &'a core::manager::Folder, @@ -41,10 +41,11 @@ impl<'a> Widget for Folder<'a> { .map(|x| x.display.as_ref()) .unwrap_or(""); + let name = readable_path(&v.path, &self.folder.cwd); let item = ListItem::new(if v.is_selected { - format!("> {} {}", icon, v.name) + format!("> {} {}", icon, name) } else { - format!("{} {}", icon, v.name) + format!("{} {}", icon, name) }); let mut style = Style::default();