diff options
author | qkzk <qkzk@users.noreply.github.com> | 2022-09-26 08:32:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-26 08:32:39 +0200 |
commit | ce58858b308e0adbac5ea4d75aad6011b06502af (patch) | |
tree | 833ef51be3ed13c4085abd19a2044dbb033f2dc8 | |
parent | 7408259fc3c6f5bc409a24fbe457e3a4aa9d6690 (diff) | |
parent | 5e7e33c83588f39dc61e02d4b022443d11d88010 (diff) |
Merge pull request #6 from qkzk/file-kind-enum
File kind enum
-rw-r--r-- | readme.md | 4 | ||||
-rw-r--r-- | src/fileinfo.rs | 126 | ||||
-rw-r--r-- | src/main.rs | 58 |
3 files changed, 82 insertions, 106 deletions
@@ -38,6 +38,8 @@ - [x] up / down - [x] links are followed - [x] resize (i guess it's an event like in curse) seems to work already +- [x] dirsymbol for sockets and whatever +- [x] refactor FileInfo, use an enum ## TODO @@ -51,8 +53,6 @@ - [ ] regex - [ ] search - [ ] mark multiple files -- [ ] dirsymbol for sockets and whatever -- [ ] refactor FileInfo, use an enum for FileType and basta ## BUGS diff --git a/src/fileinfo.rs b/src/fileinfo.rs index b5bbfb22..0a713db8 100644 --- a/src/fileinfo.rs +++ b/src/fileinfo.rs @@ -6,6 +6,52 @@ use std::path; use users::get_user_by_uid; +#[derive(Debug, Clone, Copy)] +pub enum FileKind { + NormalFile, + Directory, + BlockDevice, + CharDevice, + Fifo, + Socket, + SymbolicLink, +} + +impl FileKind { + pub fn new(direntry: &DirEntry) -> Self { + if let Ok(meta) = direntry.metadata() { + if meta.file_type().is_dir() { + Self::Directory + } else if meta.file_type().is_block_device() { + Self::BlockDevice + } else if meta.file_type().is_socket() { + Self::Socket + } else if meta.file_type().is_char_device() { + Self::CharDevice + } else if meta.file_type().is_fifo() { + Self::Fifo + } else if meta.file_type().is_symlink() { + Self::SymbolicLink + } else { + Self::NormalFile + } + } else { + Self::NormalFile + } + } + fn extract_dir_symbol(&self) -> String { + match self { + FileKind::Fifo => "p".into(), + FileKind::Socket => "s".into(), + FileKind::Directory => "d".into(), + FileKind::NormalFile => ".".into(), + FileKind::CharDevice => "c".into(), + FileKind::BlockDevice => "b".into(), + FileKind::SymbolicLink => "l".into(), + } + } +} + #[derive(Clone)] pub struct FileInfo { pub path: path::PathBuf, @@ -16,12 +62,7 @@ pub struct FileInfo { pub owner: String, pub system_time: String, pub is_selected: bool, - pub is_dir: bool, - pub is_block: bool, - pub is_char: bool, - pub is_fifo: bool, - pub is_socket: bool, - pub is_symlink: bool, + pub file_kind: FileKind, } impl FileInfo { @@ -29,26 +70,13 @@ impl FileInfo { let path = direntry.path(); let filename = extract_filename(direntry); let file_size = human_size(extract_file_size(direntry)); - let dir_symbol = extract_dir_symbol(direntry); let permissions = extract_permissions_string(direntry); - let owner = extract_username(direntry); + let owner = extract_owner(direntry); let system_time = extract_datetime(direntry); let is_selected = false; - let is_dir = direntry.path().is_dir(); - let mut is_block: bool = false; - let mut is_socket: bool = false; - let mut is_char: bool = false; - let mut is_fifo: bool = false; - let mut is_symlink: bool = false; - - if let Ok(meta) = direntry.metadata() { - is_block = meta.file_type().is_block_device(); - is_socket = meta.file_type().is_socket(); - is_char = meta.file_type().is_char_device(); - is_fifo = meta.file_type().is_fifo(); - is_symlink = meta.file_type().is_symlink(); - } + let file_kind = FileKind::new(direntry); + let dir_symbol = file_kind.extract_dir_symbol(); Ok(FileInfo { path, @@ -59,12 +87,7 @@ impl FileInfo { owner, system_time, is_selected, - is_dir, - is_block, - is_char, - is_fifo, - is_socket, - is_symlink, + file_kind, }) } @@ -227,7 +250,7 @@ fn convert_octal_mode(mode: u32) -> String { String::from(rwx[(mode & 7_u32) as usize]) } -fn extract_username(direntry: &DirEntry) -> String { +fn extract_owner(direntry: &DirEntry) -> String { match metadata(direntry.path()) { Ok(metadata) => String::from( get_user_by_uid(metadata.uid()) @@ -240,51 +263,6 @@ fn extract_username(direntry: &DirEntry) -> String { } } -fn extract_dir_symbol(direntry: &DirEntry) -> String { - match direntry.metadata() { - Ok(metadata) => { - let file_type = metadata.file_type(); - if file_type.is_dir() { - "d".into() - } else if file_type.is_file() { - ".".into() - } else if file_type.is_symlink() { - "l".into() - } else if file_type.is_block_device() { - "b".into() - } else if file_type.is_fifo() { - "p".into() - } else if file_type.is_socket() { - "s".into() - } else if file_type.is_char_device() { - "c".into() - } else { - ".".into() - } - } - Err(_) => ".".into(), - } - // match metadata(direntry.path()) { - // Ok(path) => { - // if path.is_dir() { - // "d".into() - // } else if path.is_symlink() { - // "l".into() - // } else if path.is_fifo() { - // "p".into() - // } else if path.is_block() { - // "b".into() - // } else if path.is_socket() { - // "s".into() - // } else { - // ".".into() - // } - // } - // // String::from(if path.is_dir() { "d" } else { "." }), - // Err(_) => String::from("."), - // } -} - fn extract_file_size(direntry: &DirEntry) -> u64 { match direntry.path().metadata() { Ok(size) => size.len(), diff --git a/src/main.rs b/src/main.rs index cd2999a0..99e3afe3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use tuikit::term::{Term, TermHeight}; use fm::args::Args; use fm::config::{load_file, str_to_tuikit, Colors, Keybindings}; -use fm::fileinfo::{FileInfo, PathContent}; +use fm::fileinfo::{FileInfo, FileKind, PathContent}; pub mod fileinfo; @@ -117,28 +117,27 @@ impl FilesWindow { } fn fileinfo_attr(fileinfo: &FileInfo, colors: &Colors) -> Attr { - let mut attr = Attr { - fg: str_to_tuikit(&colors.file), - bg: Color::default(), - effect: Effect::empty(), + let fg = match fileinfo.file_kind { + FileKind::Directory => str_to_tuikit(&colors.directory), + FileKind::BlockDevice => str_to_tuikit(&colors.block), + FileKind::CharDevice => str_to_tuikit(&colors.char), + FileKind::Fifo => str_to_tuikit(&colors.fifo), + FileKind::Socket => str_to_tuikit(&colors.socket), + FileKind::SymbolicLink => str_to_tuikit(&colors.symlink), + _ => str_to_tuikit(&colors.file), + }; + + let effect = if fileinfo.is_selected { + Effect::REVERSE + } else { + Effect::empty() }; - if fileinfo.is_dir { - attr.fg = str_to_tuikit(&colors.directory); - } else if fileinfo.is_block { - attr.fg = str_to_tuikit(&colors.block); - } else if fileinfo.is_char { - attr.fg = str_to_tuikit(&colors.char) - } else if fileinfo.is_fifo { - attr.fg = str_to_tuikit(&colors.fifo); - } else if fileinfo.is_socket { - attr.fg = str_to_tuikit(&colors.socket); - } else if fileinfo.is_symlink { - attr.fg = str_to_tuikit(&colors.symlink); - } - if fileinfo.is_selected { - attr.effect = Effect::REVERSE; - } - attr + + Attr { + fg, + bg: Color::default(), + effect, + } } #[derive(Clone)] @@ -297,7 +296,9 @@ impl Status { fn event_right(&mut self) { match self.mode { Mode::Normal => { - if self.path_content.files[self.path_content.selected].is_dir { + if let FileKind::Directory = + self.path_content.files[self.path_content.selected].file_kind + { self.path_content = PathContent::new( self.path_content.files[self.path_content.selected] .path @@ -632,7 +633,7 @@ impl Status { self.path_content.select_index(self.file_index); self.window.scroll_to(self.file_index) } - if self.path_content.files[self.file_index].is_dir { + if let FileKind::Directory = self.path_content.files[self.file_index].file_kind { self.event_right() } else { self.event_open_file() @@ -657,12 +658,9 @@ impl Status { fn exec_goto(&mut self) { let target = self.input_string.clone(); self.input_string.clear(); - match std::fs::canonicalize(path::Path::new(&target)) { - Ok(path) => { - self.path_content = PathContent::new(path, self.args.hidden); - self.window.reset(self.path_content.files.len()); - } - Err(_) => (), + if let Ok(path) = std::fs::canonicalize(path::Path::new(&target)) { + self.path_content = PathContent::new(path, self.args.hidden); + self.window.reset(self.path_content.files.len()); } } |