summaryrefslogtreecommitdiffstats
path: root/src/fs
diff options
context:
space:
mode:
authorAnas Saeed <saeedanas396@gmail.com>2020-10-08 14:14:30 -0700
committerAnas Saeed <saeedanas396@gmail.com>2020-10-08 14:14:30 -0700
commit7ea182e45dad7aa93481e001873798a16494aa10 (patch)
tree707dd7193402b1e61df82f94abe1a1326e7c7eb2 /src/fs
parent2807cbd6b8a7a5d7e4766e11d7671b8bbf98f290 (diff)
Added Devicon Support
Diffstat (limited to 'src/fs')
-rw-r--r--src/fs/dirlist.rs14
-rw-r--r--src/fs/entry.rs56
2 files changed, 67 insertions, 3 deletions
diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs
index 81c6ecc..c7557d4 100644
--- a/src/fs/dirlist.rs
+++ b/src/fs/dirlist.rs
@@ -150,3 +150,17 @@ where
.collect();
Ok(results)
}
+
+fn read_dir_list_icons<F>(
+ path: &path::Path,
+ filter_func: F,
+) -> std::io::Result<Vec<JoshutoDirEntry>>
+where
+ F: Fn(&Result<fs::DirEntry, std::io::Error>) -> bool,
+{
+ let results: Vec<JoshutoDirEntry> = fs::read_dir(path)?
+ .filter(filter_func)
+ .filter_map(|res| JoshutoDirEntry::from(&res.ok()?).ok())
+ .collect();
+ Ok(results)
+}
diff --git a/src/fs/entry.rs b/src/fs/entry.rs
index 4e6c69f..1dfff8c 100644
--- a/src/fs/entry.rs
+++ b/src/fs/entry.rs
@@ -2,7 +2,8 @@ use std::{fs, path};
use tui::style::{Modifier, Style};
-use crate::fs::JoshutoMetadata;
+use crate::fs::{FileType, JoshutoMetadata};
+use crate::util::devicons::*;
use crate::util::unix;
use crate::THEME_T;
@@ -17,7 +18,33 @@ pub struct JoshutoDirEntry {
}
impl JoshutoDirEntry {
+ // pub fn from(direntry: &fs::DirEntry) -> std::io::Result<Self> {
+ // let name = match direntry.file_name().into_string() {
+ // Ok(s) => s,
+ // Err(_) => {
+ // return Err(std::io::Error::new(
+ // std::io::ErrorKind::Other,
+ // "Failed converting OsString to String",
+ // ));
+ // }
+ // };
+
+ // let path = direntry.path();
+ // let metadata = JoshutoMetadata::from(&path)?;
+
+ // Ok(Self {
+ // name,
+ // path,
+ // metadata,
+ // selected: false,
+ // marked: false,
+ // })
+ // }
+
pub fn from(direntry: &fs::DirEntry) -> std::io::Result<Self> {
+ let path = direntry.path();
+ let metadata = JoshutoMetadata::from(&path)?;
+
let name = match direntry.file_name().into_string() {
Ok(s) => s,
Err(_) => {
@@ -28,8 +55,31 @@ impl JoshutoDirEntry {
}
};
- let path = direntry.path();
- let metadata = JoshutoMetadata::from(&path)?;
+ let icon = match metadata.file_type {
+ FileType::Directory => DIR_NODE_EXACT_MATCHES
+ .get(name.as_str())
+ .cloned()
+ .unwrap_or(DEFAULT_DIR),
+ _ => FILE_NODE_EXACT_MATCHES
+ .get(name.as_str())
+ .cloned()
+ .unwrap_or(match path.extension() {
+ Some(s) => FILE_NODE_EXTENSIONS
+ .get(match s.to_str() {
+ Some(s) => s,
+ None => {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::Other,
+ "Failed converting OsStr to str",
+ ))
+ }
+ })
+ .unwrap_or(&DEFAULT_FILE),
+ None => DEFAULT_FILE,
+ }),
+ };
+
+ let name = format!("{} {}", icon, name);
Ok(Self {
name,