diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-06 20:43:57 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-01-06 20:43:57 -0500 |
commit | b3781dae43c1eb673251f4522c6d9104cd046fd3 (patch) | |
tree | 1c02a62c5cd7a64644345ef33b23a9c52ad3de1d | |
parent | 3058066d5a150c0b48c66c9aec10d260b7e1533f (diff) |
now keeps track of metadata
- creation of JoshutoDirEntry is more encapsulated now, rather than manually created by sort.rs
-rw-r--r-- | src/joshuto/sort.rs | 38 | ||||
-rw-r--r-- | src/joshuto/structs.rs | 72 | ||||
-rw-r--r-- | src/joshuto/ui.rs | 41 |
3 files changed, 91 insertions, 60 deletions
diff --git a/src/joshuto/sort.rs b/src/joshuto/sort.rs index 8c2ece8..7646def 100644 --- a/src/joshuto/sort.rs +++ b/src/joshuto/sort.rs @@ -91,18 +91,13 @@ fn filter_default(result : Result<fs::DirEntry, std::io::Error>) -> Option<struc { match result { Ok(direntry) => { - let file_name = direntry.file_name(); - let file_name_as_string: String = file_name.clone().into_string().unwrap(); - let path = direntry.path(); - let dir_entry = structs::JoshutoDirEntry { - file_name, - file_name_as_string, - path, - file_type: direntry.file_type(), - selected: false, - marked: false, - }; - Some(dir_entry) + match structs::JoshutoDirEntry::from(&direntry) { + Ok(s) => Some(s), + Err(e) => { + eprintln!("{}", e); + None + }, + } }, Err(e) => { eprintln!("{}", e); @@ -120,18 +115,13 @@ fn filter_hidden_files(result : Result<fs::DirEntry, std::io::Error>) -> Option< if file_name.starts_with(".") { None } else { - let file_name = direntry.file_name(); - let file_name_as_string: String = file_name.clone().into_string().unwrap(); - let path = direntry.path(); - let dir_entry = structs::JoshutoDirEntry { - file_name, - file_name_as_string, - path, - file_type: direntry.file_type(), - selected: false, - marked: false, - }; - Some(dir_entry) + match structs::JoshutoDirEntry::from(&direntry) { + Ok(s) => Some(s), + Err(e) => { + eprintln!("{}", e); + None + }, + } } }, Err(_e) => { diff --git a/src/joshuto/structs.rs b/src/joshuto/structs.rs index 0b51285..a29713d 100644 --- a/src/joshuto/structs.rs +++ b/src/joshuto/structs.rs @@ -9,46 +9,95 @@ use joshuto::ui; use joshuto::window; #[derive(Debug)] +pub struct JoshutoMetadata { + pub len: u64, + pub modified: time::SystemTime, + pub permissions: fs::Permissions, + pub file_type: fs::FileType, +} + +impl JoshutoMetadata { + pub fn from(metadata: &fs::Metadata) -> Result<Self, std::io::Error> + { + let len = metadata.len(); + let modified = metadata.modified()?; + let permissions = metadata.permissions(); + let file_type = metadata.file_type(); + + Ok(JoshutoMetadata { + len, + modified, + permissions, + file_type + }) + } +} + +#[derive(Debug)] pub struct JoshutoDirEntry { pub file_name: ffi::OsString, pub file_name_as_string: String, pub path: path::PathBuf, - pub file_type: Result<fs::FileType, std::io::Error>, + pub metadata: JoshutoMetadata, pub selected: bool, pub marked: bool, } +impl JoshutoDirEntry { + + pub fn from(direntry: &fs::DirEntry) -> Result<Self, std::io::Error> + { + let file_name = direntry.file_name(); + let file_name_as_string: String = file_name.clone().into_string().unwrap(); + let path = direntry.path(); + + let metadata = direntry.metadata()?; + let metadata = JoshutoMetadata::from(&metadata)?; + + let dir_entry = JoshutoDirEntry { + file_name, + file_name_as_string, + path, + metadata, + selected: false, + marked: false, + }; + Ok(dir_entry) + } + +} + #[derive(Debug)] pub struct JoshutoDirList { pub index: i32, pub path: path::PathBuf, pub update_needed: bool, - pub modified: time::SystemTime, + pub metadata: JoshutoMetadata, pub contents: Vec<JoshutoDirEntry>, pub selected: usize } impl JoshutoDirList { - - pub fn new(path: path::PathBuf, sort_type: &sort::SortType) -> Result<JoshutoDirList, std::io::Error> + pub fn new(path: path::PathBuf, sort_type: &sort::SortType) -> Result<Self, std::io::Error> { let mut contents = Self::read_dir_list(path.as_path(), sort_type)?; contents.sort_by(&sort_type.compare_func()); - let modified = std::fs::metadata(&path)?.modified()?; - let index = if contents.len() > 0 { 0 } else { -1 }; + let metadata = fs::metadata(&path)?; + let metadata = JoshutoMetadata::from(&metadata)?; + Ok(JoshutoDirList { index, path, update_needed: false, - modified, + metadata, contents, selected: 0, }) @@ -61,7 +110,7 @@ impl JoshutoDirList { } if let Ok(metadata) = std::fs::metadata(&self.path) { if let Ok(modified) = metadata.modified() { - return self.modified < modified; + return self.metadata.modified < modified; } } return true; @@ -97,10 +146,9 @@ impl JoshutoDirList { } if let Ok(metadata) = std::fs::metadata(&self.path) { - match metadata.modified() { - Ok(s) => { self.modified = s; }, - Err(e) => { eprintln!("{}", e); }, - }; + if let Ok(metadata) = JoshutoMetadata::from(&metadata) { + self.metadata = metadata; + } } } diff --git a/src/joshuto/ui.rs b/src/joshuto/ui.rs index ee8bb60..e4072ea 100644 --- a/src/joshuto/ui.rs +++ b/src/joshuto/ui.rs @@ -153,31 +153,24 @@ pub fn wprint_file_info(win: ncurses::WINDOW, file: &structs::JoshutoDirEntry) ncurses::werase(win); ncurses::wmove(win, 0, 0); - match fs::symlink_metadata(&file.path) { - Ok(metadata) => { - let permissions: fs::Permissions = metadata.permissions(); - let mode = permissions.mode(); - - ncurses::waddstr(win, unix::stringify_mode(mode).as_str()); - ncurses::waddstr(win, " "); - - if metadata.is_dir() { - } else if file.path.is_dir() { - if mode >> 9 & unix::S_IFLNK >> 9 == mode >> 9 { - if let Ok(path) = fs::read_link(&file.path) { - ncurses::waddstr(win, " -> "); - ncurses::waddstr(win, path.to_str().unwrap()); - } - } - } else { - let file_size = metadata.len() as f64; - wprint_file_size(win, file_size); + + let mode = file.metadata.permissions.mode(); + + ncurses::waddstr(win, unix::stringify_mode(mode).as_str()); + ncurses::waddstr(win, " "); + + if file.metadata.file_type.is_dir() { + } else if file.path.is_dir() { + if mode >> 9 & unix::S_IFLNK >> 9 == mode >> 9 { + if let Ok(path) = fs::read_link(&file.path) { + ncurses::waddstr(win, " -> "); + ncurses::waddstr(win, path.to_str().unwrap()); } - }, - Err(e) => { - ncurses::waddstr(win, e.to_string().as_str()); - }, - }; + } + } else { + let file_size = file.metadata.len as f64; + wprint_file_size(win, file_size); + } ncurses::wnoutrefresh(win); } |