diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-27 21:11:19 -0400 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-05-27 21:14:27 -0400 |
commit | 4e00e82c62ded6e531f173914a68984307974323 (patch) | |
tree | 51c73862e8076736adf90f901240d11f7a2e62d5 | |
parent | 35579905606926893d4e7c0f3f58096c5b581444 (diff) |
move updating page state to cursor_move only
- refactor page state to take an end to prevent
blank views on init
- move functions out of JoshutoPanel into ui.rs
- create a struct for configuring how to display content
- new type JoshutoHistory
- rename display_options to display_menu
-rw-r--r-- | src/commands/cursor_move.rs | 9 | ||||
-rw-r--r-- | src/commands/open_file.rs | 2 | ||||
-rw-r--r-- | src/commands/rename_file.rs | 2 | ||||
-rw-r--r-- | src/commands/selection.rs | 4 | ||||
-rw-r--r-- | src/history.rs | 4 | ||||
-rw-r--r-- | src/preview.rs | 4 | ||||
-rw-r--r-- | src/run.rs | 2 | ||||
-rw-r--r-- | src/structs/dirlist.rs | 47 | ||||
-rw-r--r-- | src/tab.rs | 10 | ||||
-rw-r--r-- | src/ui.rs | 55 | ||||
-rw-r--r-- | src/window/page_state.rs | 4 | ||||
-rw-r--r-- | src/window/panel.rs | 69 |
12 files changed, 105 insertions, 107 deletions
diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs index dbea053..7b64c93 100644 --- a/src/commands/cursor_move.rs +++ b/src/commands/cursor_move.rs @@ -7,7 +7,7 @@ pub fn cursor_move(mut new_index: usize, context: &mut JoshutoContext, view: &Jo let curr_tab = &mut context.tabs[context.curr_tab_index]; match curr_tab.curr_list.index { - None => {} + None => return, Some(_) => { let dir_len = curr_tab.curr_list.contents.len(); /* @@ -19,10 +19,15 @@ pub fn cursor_move(mut new_index: usize, context: &mut JoshutoContext, view: &Jo new_index = dir_len - 1; } curr_tab.curr_list.index = Some(new_index); + curr_tab.curr_list.pagestate.update_page_state( + new_index, + view.mid_win.rows, + dir_len, + context.config_t.scroll_offset); } } - curr_tab.refresh_curr(&view.mid_win, context.config_t.scroll_offset); + curr_tab.refresh_curr(&view.mid_win); curr_tab.refresh_path_status( &view.top_win, &context.username, diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 863126e..0b60c02 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -197,7 +197,7 @@ impl OpenFileWith { display_vec.sort(); display_win.move_to_top(); - ui::display_options(&display_win, &display_vec); + ui::display_menu(&display_win, &display_vec); ncurses::doupdate(); let textfield = diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index a5ec420..487d9d6 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -45,7 +45,7 @@ impl RenameFile { curr_tab .curr_list .update_contents(&context.config_t.sort_option)?; - curr_tab.refresh_curr(&view.mid_win, context.config_t.scroll_offset); + curr_tab.refresh_curr(&view.mid_win); curr_tab.refresh_preview(&view.right_win, &context.config_t); Ok(()) } diff --git a/src/commands/selection.rs b/src/commands/selection.rs index c9a6982..a021666 100644 --- a/src/commands/selection.rs +++ b/src/commands/selection.rs @@ -52,7 +52,7 @@ impl JoshutoRunnable for SelectFiles { for curr in &mut curr_list.contents { curr.selected = !curr.selected; } - curr_tab.refresh_curr(&view.mid_win, context.config_t.scroll_offset); + curr_tab.refresh_curr(&view.mid_win); ncurses::doupdate(); } } else { @@ -67,7 +67,7 @@ impl JoshutoRunnable for SelectFiles { for curr in &mut curr_list.contents { curr.selected = true; } - curr_tab.refresh_curr(&view.mid_win, context.config_t.scroll_offset); + curr_tab.refresh_curr(&view.mid_win); ncurses::doupdate(); } } diff --git a/src/history.rs b/src/history.rs index 4667302..0f604c3 100644 --- a/src/history.rs +++ b/src/history.rs @@ -19,7 +19,9 @@ pub trait DirectoryHistory { fn depreciate_all_entries(&mut self); } -impl DirectoryHistory for HashMap<PathBuf, JoshutoDirList> { +pub type JoshutoHistory = HashMap<PathBuf, JoshutoDirList>; + +impl DirectoryHistory for JoshutoHistory { fn populate_to_root(&mut self, pathbuf: &PathBuf, sort_option: &sort::SortOption) { let mut ancestors = pathbuf.ancestors(); match ancestors.next() { diff --git a/src/preview.rs b/src/preview.rs index 44bcd41..b452616 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -45,11 +45,11 @@ fn preview_directory( ) { match history.entry(path.to_path_buf().clone()) { Entry::Occupied(mut entry) => { - win.display_contents(entry.get_mut(), config_t.scroll_offset); + ui::display_contents(win, entry.get_mut(), &ui::SECONDARY_DISPLAY_OPTION); } Entry::Vacant(entry) => { if let Ok(s) = JoshutoDirList::new(path.to_path_buf().clone(), &config_t.sort_option) { - win.display_contents(entry.insert(s), config_t.scroll_offset); + ui::display_contents(win, entry.insert(s), &ui::SECONDARY_DISPLAY_OPTION); } } } @@ -29,7 +29,7 @@ fn recurse_get_keycommand(keymap: &JoshutoKeymap) -> Option<&Box<JoshutoCommand> display_vec.sort(); win.move_to_top(); - ui::display_options(&win, &display_vec); + ui::display_menu(&win, &display_vec); ncurses::doupdate(); ncurses::wgetch(win.win) diff --git a/src/structs/dirlist.rs b/src/structs/dirlist.rs index aa8e9a3..2e3f34f 100644 --- a/src/structs/dirlist.rs +++ b/src/structs/dirlist.rs @@ -2,6 +2,7 @@ use std::{fs, path}; use crate::sort; use crate::structs::{JoshutoDirEntry, JoshutoMetadata}; +use crate::ui; use crate::window::JoshutoPageState; #[derive(Debug)] @@ -19,13 +20,23 @@ impl JoshutoDirList { path: path::PathBuf, sort_option: &sort::SortOption, ) -> Result<Self, std::io::Error> { - let mut contents = Self::read_dir_list(path.as_path(), sort_option)?; + let mut contents = read_dir_list(path.as_path(), sort_option)?; contents.sort_by(&sort_option.compare_func()); - let index = if !contents.is_empty() { Some(0) } else { None }; + let index = if contents.is_empty() { None } else { Some(0) }; + + let contents_len = contents.len(); + let (rows, _) = ui::getmaxyx(); + let end = if rows < 2 { + 0 + } else if contents_len > rows as usize - 2 { + rows as usize - 2 + } else { + contents_len + }; let metadata = JoshutoMetadata::from(&path)?; - let pagestate = JoshutoPageState::new(); + let pagestate = JoshutoPageState::new(end); Ok(JoshutoDirList { index, @@ -37,19 +48,6 @@ impl JoshutoDirList { }) } - fn read_dir_list( - path: &path::Path, - sort_option: &sort::SortOption, - ) -> Result<Vec<JoshutoDirEntry>, std::io::Error> { - let filter_func = sort_option.filter_func(); - let results: fs::ReadDir = fs::read_dir(path)?; - let result_vec: Vec<JoshutoDirEntry> = results - .filter(filter_func) - .filter_map(sort::map_entry_default) - .collect(); - Ok(result_vec) - } - pub fn depreciate(&mut self) { self.outdated = true; } @@ -65,7 +63,7 @@ impl JoshutoDirList { self.outdated = false; let sort_func = sort_option.compare_func(); - let mut contents = Self::read_dir_list(&self.path, sort_option)?; + let mut contents = read_dir_list(&self.path, sort_option)?; contents.sort_by(&sort_func); let contents_len = contents.len(); @@ -127,3 +125,18 @@ impl JoshutoDirList { } } } + + + +fn read_dir_list( + path: &path::Path, + sort_option: &sort::SortOption, +) -> Result<Vec<JoshutoDirEntry>, std::io::Error> { + let filter_func = sort_option.filter_func(); + let results: fs::ReadDir = fs::read_dir(path)?; + let result_vec: Vec<JoshutoDirEntry> = results + .filter(filter_func) + .filter_map(sort::map_entry_default) + .collect(); + Ok(result_vec) +} @@ -2,7 +2,7 @@ use std::collections::{hash_map::Entry, HashMap}; use std::path::PathBuf; use crate::config; -use crate::history::DirectoryHistory; +use crate::history::{DirectoryHistory, JoshutoHistory}; use crate::preview; use crate::sort; use crate::structs::JoshutoDirList; @@ -12,7 +12,7 @@ use crate::window::{JoshutoPanel, JoshutoView}; use crate::THEME_T; pub struct JoshutoTab { - pub history: HashMap<PathBuf, JoshutoDirList>, + pub history: JoshutoHistory, pub curr_path: PathBuf, pub curr_list: JoshutoDirList, } @@ -80,7 +80,7 @@ impl JoshutoTab { username: &str, hostname: &str, ) { - self.refresh_curr(&views.mid_win, config_t.scroll_offset); + self.refresh_curr(&views.mid_win); self.refresh_parent(&views.left_win, config_t); self.refresh_preview(&views.right_win, config_t); self.refresh_path_status( @@ -92,8 +92,8 @@ impl JoshutoTab { self.refresh_file_status(&views.bot_win); } - pub fn refresh_curr(&mut self, win: &JoshutoPanel, scroll_offset: usize) { - win.display_contents_detailed(&mut self.curr_list, scroll_offset); + pub fn refresh_curr(&self, win: &JoshutoPanel) { + ui::display_contents(win, &self.curr_list, &ui::PRIMARY_DISPLAY_OPTION); win.queue_for_refresh(); } @@ -12,6 +12,15 @@ use crate::THEME_T; pub const ERR_COLOR: i16 = 240; pub const EMPTY_COLOR: i16 = 241; +const MIN_WIN_WIDTH: usize = 4; + +pub struct DisplayOptions { + detailed: bool, +} + +pub const PRIMARY_DISPLAY_OPTION: DisplayOptions = DisplayOptions { detailed: true }; +pub const SECONDARY_DISPLAY_OPTION: DisplayOptions = DisplayOptions { detailed: false }; + pub fn init_ncurses() { ncurses::setlocale(ncurses::LcCategory::all, ""); @@ -54,7 +63,7 @@ pub fn getmaxyx() -> (i32, i32) { (term_rows, term_cols) } -pub fn display_options(win: &window::JoshutoPanel, vals: &[String]) { +pub fn display_menu(win: &window::JoshutoPanel, vals: &[String]) { ncurses::werase(win.win); ncurses::mvwhline(win.win, 0, 0, 0, win.cols); @@ -128,7 +137,7 @@ fn wprint_file_name( ncurses::waddstr(win, "…"); } -pub fn wprint_entry( +fn wprint_entry( win: &window::JoshutoPanel, file: &structs::JoshutoDirEntry, prefix: (usize, &str), @@ -148,7 +157,7 @@ pub fn wprint_entry( ); } -pub fn wprint_entry_detailed( +fn wprint_entry_detailed( win: &window::JoshutoPanel, file: &structs::JoshutoDirEntry, prefix: (usize, &str), @@ -170,10 +179,48 @@ pub fn wprint_entry_detailed( ncurses::mvwaddstr(win.win, coord.0, space_avail as i32, &file_size_string); } } - wprint_file_name(win.win, &file.file_name_as_string, coord, space_avail); } +pub fn display_contents(win: &window::JoshutoPanel, + dirlist: &structs::JoshutoDirList, + options: &DisplayOptions) { + if win.cols < MIN_WIN_WIDTH as i32 { + return; + } + let vec_len = dirlist.contents.len(); + if vec_len == 0 { + wprint_empty(win, "empty"); + return; + } + ncurses::werase(win.win); + ncurses::wmove(win.win, 0, 0); + + let dir_contents = &dirlist.contents; + let (start, end) = (dirlist.pagestate.start, dirlist.pagestate.end); + + let curr_index = dirlist.index.unwrap(); + + let draw_func = if options.detailed { wprint_entry_detailed } else { wprint_entry }; + + for (i, entry) in dir_contents.iter().enumerate().take(end).skip(start) { + let coord: (i32, i32) = (i as i32 - start as i32, 0); + + ncurses::wmove(win.win, coord.0, coord.1); + + let mut attr: ncurses::attr_t = 0; + if i == curr_index { + attr |= ncurses::A_STANDOUT(); + } + let attrs = get_theme_attr(attr, entry); + + draw_func(win, entry, attrs.0, coord); + + ncurses::mvwchgat(win.win, coord.0, coord.1, -1, attrs.1, attrs.2); + } + win.queue_for_refresh(); +} + pub fn wprint_file_mode(win: ncurses::WINDOW, file: &structs::JoshutoDirEntry) { use std::os::unix::fs::PermissionsExt; diff --git a/src/window/page_state.rs b/src/window/page_state.rs index 19967b2..cf4756e 100644 --- a/src/window/page_state.rs +++ b/src/window/page_state.rs @@ -5,8 +5,8 @@ pub struct JoshutoPageState { } impl JoshutoPageState { - pub fn new() -> Self { - JoshutoPageState { start: 0, end: 0 } + pub fn new(end: usize) -> Self { + JoshutoPageState { start: 0, end } } pub fn update_page_state( diff --git a/src/window/panel.rs b/src/window/panel.rs index 44eba9e..e9bd67a 100644 --- a/src/window/panel.rs +++ b/src/window/panel.rs @@ -3,8 +3,6 @@ use ncurses; use crate::structs; use crate::ui; -const MIN_WIN_WIDTH: usize = 4; - #[derive(Debug, Clone)] pub struct JoshutoPanel { pub win: ncurses::WINDOW, @@ -45,71 +43,4 @@ impl JoshutoPanel { pub fn queue_for_refresh(&self) { ncurses::wnoutrefresh(self.win); } - - pub fn display_contents(&self, dirlist: &mut structs::JoshutoDirList, scroll_offset: usize) { - if self.non_empty_dir_checks(dirlist, scroll_offset) { - Self::draw_dir_list(self, dirlist, ui::wprint_entry); - } - } - - pub fn display_contents_detailed( - &self, - dirlist: &mut structs::JoshutoDirList, - scroll_offset: usize, - ) { - if self.non_empty_dir_checks(dirlist, scroll_offset) { - Self::draw_dir_list(self, dirlist, ui::wprint_entry_detailed); - } - } - - pub fn draw_dir_list( - win: &JoshutoPanel, - dirlist: &structs::JoshutoDirList, - draw_func: fn(&JoshutoPanel, &structs::JoshutoDirEntry, (usize, &str), (i32, i32)), - ) { - let dir_contents = &dirlist.contents; - let (start, end) = (dirlist.pagestate.start, dirlist.pagestate.end); - - let curr_index = dirlist.index.unwrap(); - - for (i, entry) in dir_contents.iter().enumerate().take(end).skip(start) { - let coord: (i32, i32) = (i as i32 - start as i32, 0); - - ncurses::wmove(win.win, coord.0, coord.1); - - let mut attr: ncurses::attr_t = 0; - if i == curr_index { - attr |= ncurses::A_STANDOUT(); - } - let attrs = ui::get_theme_attr(attr, entry); - - draw_func(win, entry, attrs.0, coord); - - ncurses::mvwchgat(win.win, coord.0, coord.1, -1, attrs.1, attrs.2); - } - } - - fn non_empty_dir_checks( - &self, - dirlist: &mut structs::JoshutoDirList, - scroll_offset: usize, - ) -> bool { - if self.cols < MIN_WIN_WIDTH as i32 { - return false; - } - let vec_len = dirlist.contents.len(); - if vec_len == 0 { - ui::wprint_empty(self, "empty"); - return false; - } - ncurses::werase(self.win); - - if let Some(index) = dirlist.index { - dirlist - .pagestate - .update_page_state(index, self.rows, vec_len, scroll_offset); - } - ncurses::wmove(self.win, 0, 0); - true - } } |