summaryrefslogtreecommitdiffstats
path: root/src/history.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-07-19 21:33:08 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-07-19 22:18:23 -0400
commit0b8747eb37d6d943d90e15ed82858d99d1800425 (patch)
tree6cb910dc37343000296c2951e0f67ff269ee3efd /src/history.rs
parent98e9665e59d7af0b2c002f0e6007578b3e90aa69 (diff)
changed how commands are handled
- arguments no longer go through wordexp (still working on a good alternative) other changes: - changed update_contents to reload_contents - opening files with mimetype entries are now moved from unix.rs to mimetypes.rs
Diffstat (limited to 'src/history.rs')
-rw-r--r--src/history.rs52
1 files changed, 32 insertions, 20 deletions
diff --git a/src/history.rs b/src/history.rs
index 9d2cc7c..7333af7 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -1,13 +1,13 @@
use std::collections::{hash_map::Entry, HashMap};
use std::path::{Path, PathBuf};
-use crate::fs::JoshutoDirList;
+use crate::fs::{JoshutoDirEntry, JoshutoDirList};
use crate::sort;
pub trait DirectoryHistory {
fn populate_to_root(
&mut self,
- pathbuf: &PathBuf,
+ path: &Path,
sort_option: &sort::SortOption,
) -> std::io::Result<()>;
fn pop_or_create(
@@ -28,28 +28,29 @@ pub type JoshutoHistory = HashMap<PathBuf, JoshutoDirList>;
impl DirectoryHistory for JoshutoHistory {
fn populate_to_root(
&mut self,
- pathbuf: &PathBuf,
+ path: &Path,
sort_option: &sort::SortOption,
) -> std::io::Result<()> {
- let mut ancestors = pathbuf.ancestors();
- match ancestors.next() {
- None => {}
- Some(mut ancestor) => {
- for curr in ancestors {
- let mut dirlist = JoshutoDirList::new(curr.to_path_buf().clone(), sort_option)?;
- let index = dirlist.contents.iter().enumerate().find_map(|(i, dir)| {
- if dir.file_path() == ancestor {
- Some(i)
- } else {
- None
+ let mut ancestors = path.ancestors();
+ if let Some(mut ancestor) = ancestors.next() {
+ for curr in ancestors {
+ match self.entry(curr.to_path_buf()) {
+ Entry::Occupied(mut entry) => {
+ let dirlist = entry.get_mut();
+ dirlist.reload_contents(sort_option)?;
+ if let Some(i) = get_index_of_value(&dirlist.contents, &ancestor) {
+ dirlist.index = Some(i);
+ }
+ }
+ Entry::Vacant(entry) => {
+ let mut dirlist = JoshutoDirList::new(curr.to_path_buf().clone(), sort_option)?;
+ if let Some(i) = get_index_of_value(&dirlist.contents, &ancestor) {
+ dirlist.index = Some(i);
}
- });
- if let Some(i) = index {
- dirlist.index = Some(i);
+ entry.insert(dirlist);
}
- self.insert(curr.to_path_buf(), dirlist);
- ancestor = curr;
}
+ ancestor = curr;
}
}
Ok(())
@@ -76,7 +77,8 @@ impl DirectoryHistory for JoshutoHistory {
}
None => {
let path_clone = path.to_path_buf();
- JoshutoDirList::new(path_clone, &sort_option)
+ let dirlist = JoshutoDirList::new(path_clone, &sort_option)?;
+ Ok(dirlist)
}
}
}
@@ -105,3 +107,13 @@ impl DirectoryHistory for JoshutoHistory {
self.iter_mut().for_each(|(_, v)| v.depreciate());
}
}
+
+fn get_index_of_value(arr: &[JoshutoDirEntry], val: &Path) -> Option<usize> {
+ arr.iter().enumerate().find_map(|(i, dir)| {
+ if dir.file_path() == val {
+ Some(i)
+ } else {
+ None
+ }
+ })
+}