diff options
author | Anas Saeed <saeedanas396@gmail.com> | 2020-10-08 14:14:30 -0700 |
---|---|---|
committer | Anas Saeed <saeedanas396@gmail.com> | 2020-10-08 14:14:30 -0700 |
commit | 7ea182e45dad7aa93481e001873798a16494aa10 (patch) | |
tree | 707dd7193402b1e61df82f94abe1a1326e7c7eb2 | |
parent | 2807cbd6b8a7a5d7e4766e11d7671b8bbf98f290 (diff) |
Added Devicon Support
-rw-r--r-- | Cargo.lock | 67 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/fs/dirlist.rs | 14 | ||||
-rw-r--r-- | src/fs/entry.rs | 56 | ||||
-rw-r--r-- | src/util/devicons.rs | 351 | ||||
-rw-r--r-- | src/util/mod.rs | 1 |
6 files changed, 487 insertions, 3 deletions
@@ -180,6 +180,7 @@ dependencies = [ "lazy_static", "libc", "open", + "phf", "rand", "rustyline", "serde", @@ -270,6 +271,50 @@ dependencies = [ ] [[package]] +name = "phf" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12" +dependencies = [ + "phf_macros", + "phf_shared", + "proc-macro-hack", +] + +[[package]] +name = "phf_generator" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526" +dependencies = [ + "phf_shared", + "rand", +] + +[[package]] +name = "phf_macros" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6fde18ff429ffc8fe78e2bf7f8b7a5a5a6e2a8b58bc5a9ac69198bbda9189c" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "phf_shared" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" +dependencies = [ + "siphasher", +] + +[[package]] name = "ppv-lite86" version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -300,6 +345,12 @@ dependencies = [ ] [[package]] +name = "proc-macro-hack" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99c605b9a0adc77b7211c6b1f722dcb613d68d66859a44f3d485a6da332b0598" + +[[package]] name = "proc-macro2" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -328,6 +379,7 @@ dependencies = [ "rand_chacha", "rand_core", "rand_hc", + "rand_pcg", ] [[package]] @@ -359,6 +411,15 @@ dependencies = [ ] [[package]] +name = "rand_pcg" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" +dependencies = [ + "rand_core", +] + +[[package]] name = "redox_syscall" version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -437,6 +498,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074" [[package]] +name = "siphasher" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7" + +[[package]] name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -28,6 +28,7 @@ unicode-width = "^0" users = "^0" whoami = "^0" xdg = "^2" +phf = { version = "0.8", features = ["macros"] } # fs_extra = "*" # lazy_static = "*" 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, diff --git a/src/util/devicons.rs b/src/util/devicons.rs new file mode 100644 index 0000000..ec0a5f2 --- /dev/null +++ b/src/util/devicons.rs @@ -0,0 +1,351 @@ +use phf::phf_map; + +pub const DEFAULT_DIR: &'static str = ""; +pub const DEFAULT_FILE: &'static str = ""; + +// all those glyphs will show as weird squares if you don't have the correct patched font +// My advice is to use NerdFonts which can be found here: +// https://github.com/ryanoasis/nerd-fonts +pub static FILE_NODE_EXTENSIONS: phf::Map<&'static str, &'static str> = phf_map! { + "7z" => "", + "a" => "", + "ai" => "", + "apk" => "", + "asm" => "", + "asp" => "", + "aup" => "", + "avi" => "", + "awk" => "", + "bash" => "", + "bat" => "", + "bmp" => "", + "bz2" => "", + "c" => "", + "c++" => "", + "cab" => "", + "cbr" => "", + "cbz" => "", + "cc" => "", + "class" => "", + "clj" => "", + "cljc" => "", + "cljs" => "", + "cmake" => "", + "coffee" => "", + "conf" => "", + "cp" => "", + "cpio" => "", + "cpp" => "", + "cs" => "", + "csh" => "", + "css" => "", + "cue" => "", + "cvs" => "", + "cxx" => "", + "d" => "", + "dart" => "", + "db" => "", + "deb" => "", + "diff" => "", + "dll" => "", + "doc" => "", + "docx" => "", + "dump" => "", + "edn" => "", + "eex" => "", + "efi" => "", + "ejs" => "", + "elf" => "", + "elm" => "", + "epub" => "", + "erl" => "", + "ex" => "", + "exe" => "", + "exs" => "", + "f//" => "", + "fifo" => "|", + "fish" => "", + "flac" => "", + "flv" => "", + "fs" => "", + "fsi" => "", + "fsscript" => "", + "fsx" => "", + "gem" => "", + "gemspec" => "", + "gif" => "", + "go" => "", + "gz" => "", + "gzip" => "", + "h" => "", + "haml" => "", + "hbs" => "", + "hh" => "", + "hpp" => "", + "hrl" => "", + "hs" => "", + "htaccess" => "", + "htm" => "", + "html" => "", + "htpasswd" => "", + "hxx" => "", + "ico" => "", + "img" => "", + "ini" => "", + "iso" => "", + "jar" => "", + "java" => "", + "jl" => "", + "jpeg" => "", + "jpg" => "", + "js" => "", + "json" => "", + "jsx" => "", + "key" => "", + "ksh" => "", + "leex" => "", + "less" => "", + "lha" => "", + "lhs" => "", + "log" => "", + "lua" => "", + "lzh" => "", + "lzma" => "", + "m4a" => "", + "m4v" => "", + "markdown" => "", + "md" => "", + "mdx" => "", + "mjs" => "", + "mkv" => "", + "ml" => "λ", + "mli" => "λ", + "mov" => "", + "mp3" => "", + "mp4" => "", + "mpeg" => "", + "mpg" => "", + "msi" => "", + "mustache" => "", + "nix" => "", + "o" => "", + "ogg" => "", + "pdf" => "", + "php" => "", + "pl" => "", + "pm" => "", + "png" => "", + "pp" => "", + "ppt" => "", + "pptx" => "", + "ps1" => "", + "psb" => "", + "psd" => "", + "pub" => "", + "py" => "", + "pyc" => "", + "pyd" => "", + "pyo" => "", + "r" => "ﳒ", + "rake" => "", + "rar" => "", + "rb" => "", + "rc" => "", + "rlib" => "", + "rmd" => "", + "rom" => "", + "rpm" => "", + "rproj" => "鉶", + "rs" => "", + "rss" => "", + "rtf" => "", + "s" => "", + "sass" => "", + "scala" => "", + "scss" => "", + "sh" => "", + "slim" => "", + "sln" => "", + "so" => "", + "sql" => "", + "styl" => "", + "suo" => "", + "swift" => "", + "t" => "", + "tar" => "", + "tex" => "ﭨ", + "tgz" => "", + "toml" => "", + "ts" => "", + "tsx" => "", + "twig" => "", + "vim" => "", + "vimrc" => "", + "vue" => "﵂", + "wav" => "", + "webm" => "", + "webmanifest" => "", + "webp" => "", + "xbps" => "", + "xcplayground" => "", + "xhtml" => "", + "xls" => "", + "xlsx" => "", + "xml" => "", + "xul" => "", + "xz" => "", + "yaml" => "", + "yml" => "", + "zip" => "", + "zsh" => "", +}; + +pub static DIR_NODE_EXACT_MATCHES: phf::Map<&'static str, &'static str> = phf_map! { +// English + ".git" => "", + "Desktop" => "", + "Documents" => "", + "Downloads" => "", + "Dotfiles" => "", + "Dropbox" => "", + "Music" => "", + "Pictures" => "", + "Public" => "", + "Templates" => "", + "Videos" => "", +// Spanish + "Escritorio" => "", + "Documentos" => "", + "Descargas" => "", + "Música" => "", + "Imágenes" => "", + "Público" => "", + "Plantillas" => "", + // "Vídeos" => "", +// French + "Bureau" => "", + "Images" => "", + "Musique" => "", + "Publique" => "", + "Téléchargements" => "", + "Vidéos" => "", +// Portuguese + "Imagens" => "", + "Modelos" => "", + "Vídeos" => "", + "Área de trabalho" => "", +// Italian + "Documenti" => "", + "Immagini" => "", + "Modelli" => "", + "Musica" => "", + "Pubblici" => "", + "Scaricati" => "", + "Scrivania" => "", + "Video" => "", +// German + "Bilder" => "", + "Dokumente" => "", + "Musik" => "", + "Schreibtisch" => "", + "Vorlagen" => "", + "Öffentlich" => "", +// Hungarian + "Dokumentumok" => "", + "Képek" => "", + "Zene" => "", + "Letöltések" => "", + "Számítógép" => "", + "Videók" => "", +}; + +pub static FILE_NODE_EXACT_MATCHES: phf::Map<&'static str, &'static str> = phf_map! { + ".bash_aliases" => "", + ".bash_history" => "", + ".bash_logout" => "", + ".bash_profile" => "", + ".bashprofile" => "", + ".bashrc" => "", + ".dmrc" => "", + ".DS_Store" => "", + ".fasd" => "", + ".fehbg" => "", + ".gitattributes" => "", + ".gitconfig" => "", + ".gitignore" => "", + ".gitlab-ci.yml" => "", + ".gvimrc" => "", + ".inputrc" => "", + ".jack-settings" => "", + ".mime.types" => "", + ".ncmpcpp" => "", + ".nvidia-settings-rc" => "", + ".pam_environment" => "", + ".profile" => "", + ".recently-used" => "", + ".selected_editor" => "", + ".vim" => "", + ".viminfo" => "", + ".vimrc" => "", + ".Xauthority" => "", + ".Xdefaults" => "", + ".xinitrc" => "", + ".xinputrc" => "", + ".Xresources" => "", + ".zshrc" => "", + "_gvimrc" => "", + "_vimrc" => "", + "a.out" => "", + "authorized_keys" => "", + "bspwmrc" => "", + "cmakelists.txt" => "", + "config" => "", + "config.ac" => "", + "config.m4" => "", + "config.mk" => "", + "config.ru" => "", + "configure" => "", + "docker-compose.yml" => "", + "dockerfile" => "", + "Dockerfile" => "", + "dropbox" => "", + "exact-match-case-sensitive-1.txt" => "X1", + "exact-match-case-sensitive-2" => "X2", + "favicon.ico" => "", + "gemfile" => "", + "gruntfile.coffee" => "", + "gruntfile.js" => "", + "gruntfile.ls" => "", + "gulpfile.coffee" => "", + "gulpfile.js" => "", + "gulpfile.ls" => "", + "ini" => "", + "known_hosts" => "", + "ledger" => "", + "license" => "", + "LICENSE" => "", + "LICENSE.md" => "", + "LICENSE.txt" => "", + "Makefile" => "", + "makefile" => "", + "Makefile.ac" => "", + "Makefile.in" => "", + "mimeapps.list" => "", + "mix.lock" => "", + "node_modules" => "", + "package-lock.json" => "", + "package.json" => "", + "playlists" => "", + "procfile" => "", + "Rakefile" => "", + "rakefile" => "", + "react.jsx" => "", + "README" => "", + "README.markdown" => "", + "README.md" => "", + "README.rst" => "", + "README.txt" => "", + "sxhkdrc" => "", + "user-dirs.dirs" => "", + "webpack.config.js" => "", +}; diff --git a/src/util/mod.rs b/src/util/mod.rs index 6ba770d..8955c76 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,4 @@ +pub mod devicons; pub mod event; pub mod format; pub mod key_mapping; |