diff options
Diffstat (limited to 'src/fs/entry.rs')
-rw-r--r-- | src/fs/entry.rs | 62 |
1 files changed, 39 insertions, 23 deletions
diff --git a/src/fs/entry.rs b/src/fs/entry.rs index 30a667b..d0940c3 100644 --- a/src/fs/entry.rs +++ b/src/fs/entry.rs @@ -1,7 +1,7 @@ use std::{fs, io, path}; use crate::{ - config::clean::app::display::DisplayOption, + config::clean::app::{display::DisplayOption, AppConfig}, fs::{FileType, JoshutoMetadata}, }; @@ -11,6 +11,7 @@ use crate::ICONS_T; #[derive(Clone, Debug)] pub struct JoshutoDirEntry { name: String, + ext: Option<String>, label: String, path: path::PathBuf, pub metadata: JoshutoMetadata, @@ -25,6 +26,7 @@ impl JoshutoDirEntry { pub fn from( direntry: &walkdir::DirEntry, base: &path::Path, + config: &AppConfig, options: &DisplayOption, ) -> io::Result<Self> { let path = direntry.path().to_path_buf(); @@ -36,6 +38,18 @@ impl JoshutoDirEntry { .to_string_lossy() .to_string(); + let ext = direntry + .path() + .extension() + .and_then(|s| s.to_str()) + .map(|s| { + if config.case_sensitive_ext { + s.to_string() + } else { + s.to_lowercase() + } + }); + let mut metadata = JoshutoMetadata::from(&path)?; if options.automatically_count_files() && metadata.file_type().is_dir() { @@ -46,7 +60,7 @@ impl JoshutoDirEntry { #[cfg(feature = "devicons")] let label = if options.show_icons() { - create_icon_label(name.as_str(), &metadata) + create_icon_label(name.as_str(), &ext, config, &metadata) } else { name.clone() }; @@ -56,6 +70,7 @@ impl JoshutoDirEntry { Ok(Self { name, + ext, label, path, metadata, @@ -69,6 +84,10 @@ impl JoshutoDirEntry { self.name.as_str() } + pub fn ext(&self) -> Option<&str> { + self.ext.as_deref() + } + pub fn label(&self) -> &str { self.label.as_str() } @@ -100,14 +119,6 @@ impl JoshutoDirEntry { pub fn set_visual_mode_selected(&mut self, visual_mode_selected: bool) { self.visual_mode_selected = visual_mode_selected; } - - pub fn get_ext(&self) -> &str { - let fname = self.file_name(); - match fname.rfind('.') { - Some(pos) => &fname[pos..], - None => "", - } - } } impl std::fmt::Display for JoshutoDirEntry { @@ -142,7 +153,12 @@ impl std::cmp::Ord for JoshutoDirEntry { } #[cfg(feature = "devicons")] -fn create_icon_label(name: &str, metadata: &JoshutoMetadata) -> String { +fn create_icon_label( + name: &str, + ext: &Option<String>, + config: &AppConfig, + metadata: &JoshutoMetadata, +) -> String { let label = { let icon = match metadata.file_type() { FileType::Directory => ICONS_T @@ -150,18 +166,18 @@ fn create_icon_label(name: &str, metadata: &JoshutoMetadata) -> String { .get(name) .cloned() .unwrap_or(ICONS_T.default_dir.clone()), - _ => ICONS_T - .file_exact - .get(name) - .cloned() - .unwrap_or(match name.rsplit_once('.') { - Some((_, ext)) => ICONS_T - .ext - .get(ext) - .unwrap_or(&ICONS_T.default_file) - .to_string(), - None => ICONS_T.default_file.clone(), - }), + _ => ICONS_T.file_exact.get(name).cloned().unwrap_or(match ext { + Some(ext) => { + let icon = if config.case_sensitive_ext { + ICONS_T.ext.get(ext) + } else { + ICONS_T.ext.get(&ext.to_lowercase()) + }; + + icon.unwrap_or(&ICONS_T.default_file).to_string() + } + None => ICONS_T.default_file.clone(), + }), }; format!("{} {}", icon, name) }; |