summaryrefslogtreecommitdiffstats
path: root/src/tab.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-04-24 22:36:09 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-04-24 23:15:13 -0400
commit2be79f4cd0ee0e89c5cadf5121a8b979391d0d51 (patch)
treeadbd77abb51ea2a4613c78d210896f1ed55b4498 /src/tab.rs
parentc3919c91a866a7fd520d7dcb8b99c7d4aafbbb93 (diff)
major refactoring
- removed parent_list from JoshutoTab struct - parent_list will behave much like previewing now where the contents will be retrieved from the dictionary - completely remove DirHistory struct in favour of trait implemenation on top of HashMap
Diffstat (limited to 'src/tab.rs')
-rw-r--r--src/tab.rs114
1 files changed, 41 insertions, 73 deletions
diff --git a/src/tab.rs b/src/tab.rs
index 9d8abad..2407810 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -1,7 +1,8 @@
+use std::collections::HashMap;
use std::path::PathBuf;
use crate::config;
-use crate::history;
+use crate::history::DirectoryHistory;
use crate::sort;
use crate::structs::JoshutoDirList;
use crate::ui;
@@ -10,30 +11,22 @@ use crate::window::{JoshutoPanel, JoshutoView};
use crate::THEME_T;
pub struct JoshutoTab {
- pub history: history::DirHistory,
+ pub history: HashMap<PathBuf, JoshutoDirList>,
pub curr_path: PathBuf,
- pub parent_list: Option<JoshutoDirList>,
- pub curr_list: Option<JoshutoDirList>,
+ pub curr_list: JoshutoDirList,
}
impl JoshutoTab {
pub fn new(curr_path: PathBuf, sort_option: &sort::SortOption) -> Result<Self, std::io::Error> {
- let mut history = history::DirHistory::new();
+ let mut history = HashMap::new();
history.populate_to_root(&curr_path, sort_option);
- let curr_list: Option<JoshutoDirList> =
- Some(history.pop_or_create(&curr_path, sort_option)?);
-
- let parent_list: Option<JoshutoDirList> = match curr_path.parent() {
- Some(parent) => Some(history.pop_or_create(&parent, sort_option)?),
- None => None,
- };
+ let curr_list = history.pop_or_create(&curr_path, sort_option)?;
let tab = JoshutoTab {
curr_path,
history,
curr_list,
- parent_list,
};
Ok(tab)
}
@@ -42,21 +35,9 @@ impl JoshutoTab {
&mut self,
sort_option: &sort::SortOption,
) -> Result<(), std::io::Error> {
- let mut list = self.curr_list.take();
- if let Some(ref mut s) = list {
- if s.path.exists() {
- s.update_contents(sort_option)?;
- }
- };
- self.curr_list = list;
-
- let mut list = self.parent_list.take();
- if let Some(ref mut s) = list {
- if s.path.exists() {
- s.update_contents(sort_option)?;
- }
- };
- self.parent_list = list;
+ if self.curr_list.path.exists() {
+ self.curr_list.update_contents(sort_option)?;
+ }
Ok(())
}
@@ -67,62 +48,51 @@ impl JoshutoTab {
username: &str,
hostname: &str,
) {
- self.refresh_(
- views,
- config_t.tilde_in_titlebar,
- config_t.scroll_offset,
+ self.refresh_curr(&views.mid_win, config_t.scroll_offset);
+ self.refresh_parent(&views.left_win, config_t);
+ self.refresh_path_status(
+ &views.top_win,
username,
hostname,
+ config_t.tilde_in_titlebar,
);
- }
-
- pub fn refresh_(
- &mut self,
- views: &JoshutoView,
- tilde_in_titlebar: bool,
- scroll_offset: usize,
- username: &str,
- hostname: &str,
- ) {
- self.refresh_curr(&views.mid_win, scroll_offset);
- self.refresh_parent(&views.left_win, scroll_offset);
- self.refresh_path_status(&views.top_win, username, hostname, tilde_in_titlebar);
self.refresh_file_status(&views.bot_win);
}
pub fn refresh_curr(&mut self, win: &JoshutoPanel, scroll_offset: usize) {
- if let Some(ref mut s) = self.curr_list {
- win.display_contents_detailed(s, scroll_offset);
- win.queue_for_refresh();
- }
+ win.display_contents_detailed(&mut self.curr_list, scroll_offset);
+ win.queue_for_refresh();
}
- pub fn refresh_parent(&mut self, win: &JoshutoPanel, scroll_offset: usize) {
- if let Some(ref mut s) = self.parent_list {
- win.display_contents(s, scroll_offset);
- win.queue_for_refresh();
+ pub fn refresh_parent(&mut self, win: &JoshutoPanel, config_t: &config::JoshutoConfig) {
+ if let Some(parent) = self.curr_list.path.parent() {
+ if let Ok(parent_list) = self
+ .history
+ .get_mut_or_create(&parent, &config_t.sort_option)
+ {
+ win.display_contents_detailed(parent_list, config_t.scroll_offset);
+ win.queue_for_refresh();
+ }
}
}
pub fn refresh_file_status(&self, win: &JoshutoPanel) {
- if let Some(ref dirlist) = self.curr_list {
- ncurses::werase(win.win);
- ncurses::wmove(win.win, 0, 0);
-
- if let Some(entry) = dirlist.get_curr_ref() {
- ui::wprint_file_mode(win.win, entry);
- ncurses::waddstr(win.win, " ");
- if let Some(index) = dirlist.index {
- ncurses::waddstr(
- win.win,
- format!("{}/{} ", index + 1, dirlist.contents.len()).as_str(),
- );
- }
- ncurses::waddstr(win.win, " ");
- ui::wprint_file_info(win.win, entry);
+ ncurses::werase(win.win);
+ ncurses::wmove(win.win, 0, 0);
+
+ if let Some(entry) = self.curr_list.get_curr_ref() {
+ ui::wprint_file_mode(win.win, entry);
+ ncurses::waddstr(win.win, " ");
+ if let Some(index) = self.curr_list.index {
+ ncurses::waddstr(
+ win.win,
+ format!("{}/{} ", index + 1, self.curr_list.contents.len()).as_str(),
+ );
}
- win.queue_for_refresh();
+ ncurses::waddstr(win.win, " ");
+ ui::wprint_file_info(win.win, entry);
}
+ win.queue_for_refresh();
}
pub fn refresh_path_status(
@@ -151,10 +121,8 @@ impl JoshutoTab {
}
ncurses::waddstr(win.win, "/");
ncurses::wattroff(win.win, ncurses::COLOR_PAIR(THEME_T.directory.colorpair));
- if let Some(ref dirlist) = self.curr_list {
- if let Some(entry) = dirlist.get_curr_ref() {
- ncurses::waddstr(win.win, &entry.file_name_as_string);
- }
+ if let Some(entry) = self.curr_list.get_curr_ref() {
+ ncurses::waddstr(win.win, &entry.file_name_as_string);
}
ncurses::wattroff(win.win, ncurses::A_BOLD());
win.queue_for_refresh();