diff options
-rw-r--r-- | src/app/first_line.rs | 2 | ||||
-rw-r--r-- | src/app/mod.rs | 1 | ||||
-rw-r--r-- | src/app/status.rs | 29 | ||||
-rw-r--r-- | src/app/tab.rs | 87 | ||||
-rw-r--r-- | src/event/action_map.rs | 2 | ||||
-rw-r--r-- | src/event/event_exec.rs | 26 | ||||
-rw-r--r-- | src/io/display.rs | 2 | ||||
-rw-r--r-- | src/modes/display/lsl.rs | 18 |
8 files changed, 84 insertions, 83 deletions
diff --git a/src/app/first_line.rs b/src/app/first_line.rs index e1ddbf8..e2611fd 100644 --- a/src/app/first_line.rs +++ b/src/app/first_line.rs @@ -145,7 +145,7 @@ impl FirstLine { } fn string_sort_kind(tab: &Tab) -> String { - format!(" {} ", &tab.sort_kind) + format!(" {} ", &tab.settings.sort_kind) } fn string_first_row_flags(status: &Status) -> String { diff --git a/src/app/mod.rs b/src/app/mod.rs index b647816..61174bc 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -11,3 +11,4 @@ pub use first_line::FirstLine; pub use refresher::Refresher; pub use status::Status; pub use tab::Tab; +pub use tab::TabSettings; diff --git a/src/app/status.rs b/src/app/status.rs index 44248f1..644dba5 100644 --- a/src/app/status.rs +++ b/src/app/status.rs @@ -355,21 +355,12 @@ impl Status { Ok(()) } - /// Select a tab according to its index. - /// It's deprecated and is left mostly because I'm not sure I want - /// tabs & panes... and I haven't fully decided yet. - /// Since I'm lazy and don't want to write it twice, it's left here. - pub fn select_tab(&mut self, index: usize) -> Result<()> { - if index >= self.tabs.len() { - Err(anyhow!( - "Only {} tabs. Can't select tab {}", - self.tabs.len(), - index - )) - } else { - self.index = index; - Ok(()) - } + pub fn select_left(&mut self) { + self.index = 0; + } + + pub fn select_right(&mut self) { + self.index = 1; } /// Refresh every disk information. @@ -508,7 +499,7 @@ impl Status { /// Set dual pane if the term is big enough pub fn set_dual_pane_if_wide_enough(&mut self, width: usize) -> Result<()> { if width < MIN_WIDTH_FOR_DUAL_PANE { - self.select_tab(0)?; + self.select_left(); self.settings.dual = false; } else { self.settings.dual = true; @@ -889,12 +880,12 @@ impl Status { let (width, _) = self.term_size()?; if self.settings.dual { if (col as usize) < width / 2 { - self.select_tab(0)?; + self.select_left(); } else { - self.select_tab(1)?; + self.select_right(); }; } else { - self.select_tab(0)?; + self.select_left(); } Ok(()) } diff --git a/src/app/tab.rs b/src/app/tab.rs index f57c6b7..6d6e913 100644 --- a/src/app/tab.rs +++ b/src/app/tab.rs @@ -20,6 +20,28 @@ use crate::modes::Users; use crate::modes::{calculate_top_bottom, Go, To, Tree}; use crate::modes::{Display, Edit}; +pub struct TabSettings { + /// read from command line + pub show_hidden: bool, + /// The filter use before displaying files + pub filter: FilterKind, + /// The kind of sort used to display the files. + pub sort_kind: SortKind, +} + +impl TabSettings { + fn new(args: &Args, settings: &Settings) -> Self { + let filter = FilterKind::All; + let show_hidden = args.all || settings.all; + let sort_kind = SortKind::default(); + Self { + show_hidden, + filter, + sort_kind, + } + } +} + /// Holds every thing about the current tab of the application. /// Most of the mutation is done externally. pub struct Tab { @@ -42,12 +64,7 @@ pub struct Tab { /// Height of the terminal window pub height: usize, - /// read from command line - pub show_hidden: bool, - /// The filter use before displaying files - pub filter: FilterKind, - /// The kind of sort used to display the files. - pub sort_kind: SortKind, + pub settings: TabSettings, /// Last searched string pub searched: Option<String>, @@ -70,9 +87,9 @@ impl Tab { } else { path.parent().context("")? }; - let filter = FilterKind::All; - let show_hidden = args.all || settings.all; - let mut path_content = PathContent::new(start_dir, &users, &filter, show_hidden)?; + let settings = TabSettings::new(args, settings); + let mut path_content = + PathContent::new(start_dir, &users, &settings.filter, settings.show_hidden)?; let display_mode = Display::default(); let edit_mode = Edit::Nothing; let mut window = ContentWindow::new(path_content.content.len(), height); @@ -82,7 +99,6 @@ impl Tab { let searched = None; let index = path_content.select_file(&path); let tree = Tree::default(); - let sort_kind = SortKind::default(); window.scroll_to(index); Ok(Self { display_mode, @@ -93,18 +109,16 @@ impl Tab { must_quit, preview, searched, - filter, - show_hidden, history, users, tree, - sort_kind, + settings, }) } /// Refresh everything but the view pub fn refresh_params(&mut self) -> Result<()> { - self.filter = FilterKind::All; + self.settings.filter = FilterKind::All; self.preview = Preview::empty(); self.set_edit_mode(Edit::Nothing); if matches!(self.display_mode, Display::Tree) { @@ -119,8 +133,7 @@ impl Tab { /// displayed files is reset. /// The first file is selected. pub fn refresh_view(&mut self) -> Result<()> { - self.path_content - .reset_files(&self.filter, self.show_hidden, &self.users)?; + self.path_content.reset_files(&self.settings, &self.users)?; self.window.reset(self.path_content.content.len()); self.refresh_params()?; Ok(()) @@ -128,7 +141,7 @@ impl Tab { /// Update the kind of sort from a char typed by the user. pub fn update_sort_from_char(&mut self, c: char) { - self.sort_kind.update_from_char(c) + self.settings.sort_kind.update_from_char(c) } /// Refresh the view if files were modified in current directory. @@ -211,21 +224,26 @@ impl Tab { &self.path_content.path, &self.path_content.selected().context("")?.path, ); - self.path_content.change_directory( - path, - &self.filter, - self.show_hidden, - &self.users, - &self.sort_kind, - )?; + self.path_content + .change_directory(path, &self.settings, &self.users)?; if matches!(self.display_mode, Display::Tree) { - self.make_tree(Some(self.sort_kind))?; + self.make_tree(Some(self.settings.sort_kind))?; } self.window.reset(self.path_content.content.len()); std::env::set_current_dir(path)?; Ok(()) } + pub fn toggle_hidden(&mut self) -> Result<()> { + self.settings.show_hidden = !self.settings.show_hidden; + self.path_content.reset_files(&self.settings, &self.users)?; + self.window.reset(self.path_content.content.len()); + if let Display::Tree = self.display_mode { + self.make_tree(None)? + } + Ok(()) + } + /// Set the window. Doesn't require the lenght to be known. pub fn set_window(&mut self) { let len = self.path_content.content.len(); @@ -233,7 +251,7 @@ impl Tab { } /// Apply the filter. pub fn set_filter(&mut self, filter: FilterKind) { - self.filter = filter + self.settings.filter = filter } /// Set the line index to `index` and scroll there. @@ -324,7 +342,7 @@ impl Tab { return Ok(()); }; self.cd(parent.to_owned().as_ref())?; - self.make_tree(Some(self.sort_kind)) + self.make_tree(Some(self.settings.sort_kind)) } else { self.tree.go(To::Parent); Ok(()) @@ -392,10 +410,17 @@ impl Tab { Some(sort_kind) => sort_kind, None => SortKind::tree_default(), }; - self.sort_kind = sort_kind.to_owned(); + self.settings.sort_kind = sort_kind.to_owned(); let path = self.path_content.path.clone(); let users = &self.users; - self.tree = Tree::new(path, 5, sort_kind, users, self.show_hidden, &self.filter); + self.tree = Tree::new( + path, + 5, + sort_kind, + users, + self.settings.show_hidden, + &self.settings.filter, + ); Ok(()) } @@ -595,14 +620,14 @@ impl Tab { Display::Normal => { self.path_content.unselect_current(); self.update_sort_from_char(c); - self.path_content.sort(&self.sort_kind); + self.path_content.sort(&self.settings.sort_kind); self.normal_go_top(); self.path_content.select_index(0); } Display::Tree => { self.update_sort_from_char(c); let selected_path = self.tree.selected_path().to_owned(); - self.make_tree(Some(self.sort_kind))?; + self.make_tree(Some(self.settings.sort_kind))?; self.tree.go(To::Path(&selected_path)); } _ => (), diff --git a/src/event/action_map.rs b/src/event/action_map.rs index bbf63d4..e112462 100644 --- a/src/event/action_map.rs +++ b/src/event/action_map.rs @@ -192,7 +192,7 @@ impl ActionMap { ActionMap::ToggleDisplayFull => EventAction::toggle_display_full(status), ActionMap::ToggleDualPane => EventAction::toggle_dualpane(status), ActionMap::ToggleFlag => EventAction::toggle_flag(status), - ActionMap::ToggleHidden => EventAction::toggle_hidden(status), + ActionMap::ToggleHidden => EventAction::toggle_hidden(current_tab), ActionMap::TogglePreviewSecond => EventAction::toggle_preview_second(status), ActionMap::TrashEmpty => EventAction::trash_empty(status), ActionMap::TrashMoveFile => EventAction::trash_move_file(status), diff --git a/src/event/event_exec.rs b/src/event/event_exec.rs index d19fdca..40e6525 100644 --- a/src/event/event_exec.rs +++ b/src/event/event_exec.rs @@ -274,16 +274,8 @@ impl EventAction { Ok(()) } /// Toggle the display of hidden files. - pub fn toggle_hidden(status: &mut Status) -> Result<()> { - let tab = status.current_tab(); - tab.show_hidden = !tab.show_hidden; - tab.path_content - .reset_files(&tab.filter, tab.show_hidden, &tab.users)?; - tab.window.reset(tab.path_content.content.len()); - if let Display::Tree = tab.display_mode { - tab.make_tree(None)? - } - Ok(()) + pub fn toggle_hidden(tab: &mut Tab) -> Result<()> { + tab.toggle_hidden() } /// Open files with custom opener. @@ -879,7 +871,7 @@ impl EventAction { /// is too low to display both panes. pub fn toggle_dualpane(status: &mut Status) -> Result<()> { status.settings.dual = !status.settings.dual; - status.select_tab(0)?; + status.select_left(); Ok(()) } @@ -1490,13 +1482,11 @@ impl LeaveMode { let filter = FilterKind::from_input(&status.menu.input.string()); status.current_tab().set_filter(filter); status.menu.input.reset(); - let filter = status.current_tab_non_mut().filter.clone(); - let show_hidden = status.current_tab_non_mut().show_hidden; - let users = status.current_tab_non_mut().users.clone(); - status - .current_tab() - .path_content - .reset_files(&filter, show_hidden, &users)?; + // ugly hack to please borrow checker :( + status.tabs[status.index].path_content.reset_files( + &status.tabs[status.index].settings, + &status.tabs[status.index].users, + )?; if let Display::Tree = status.current_tab_non_mut().display_mode { status.current_tab().make_tree(None)?; } diff --git a/src/io/display.rs b/src/io/display.rs index fea668c..1df550a 100644 --- a/src/io/display.rs +++ b/src/io/display.rs @@ -569,7 +569,7 @@ impl WinMainSecondLine { fn second_line_simple(status: &Status) -> (Option<String>, Option<Attr>) { ( - Some(status.current_tab_non_mut().filter.to_string()), + Some(status.current_tab_non_mut().settings.filter.to_string()), Some(ATTR_YELLOW_BOLD), ) } diff --git a/src/modes/display/lsl.rs b/src/modes/display/lsl.rs index 0d7d929..60c17a1 100644 --- a/src/modes/display/lsl.rs +++ b/src/modes/display/lsl.rs @@ -4,6 +4,7 @@ use std::path; use anyhow::{Context, Result}; +use crate::app::TabSettings; use crate::common::{filename_from_path, path_to_string}; use crate::io::git; use crate::modes::FilterKind; @@ -56,13 +57,11 @@ impl PathContent { pub fn change_directory( &mut self, path: &path::Path, - filter: &FilterKind, - show_hidden: bool, + settings: &TabSettings, users: &Users, - sort_kind: &SortKind, ) -> Result<()> { - self.content = Self::files(path, show_hidden, filter, users)?; - sort_kind.sort(&mut self.content); + self.content = Self::files(path, settings.show_hidden, &settings.filter, users)?; + settings.sort_kind.sort(&mut self.content); self.index = 0; if !self.content.is_empty() { self.content[0].select() @@ -137,13 +136,8 @@ impl PathContent { /// Reset the current file content. /// Reads and sort the content with current key. /// Select the first file if any. - pub fn reset_files( - &mut self, - filter: &FilterKind, - show_hidden: bool, - users: &Users, - ) -> Result<()> { - self.content = Self::files(&self.path, show_hidden, filter, users)?; + pub fn reset_files(&mut self, settings: &TabSettings, users: &Users) -> Result<()> { + self.content = Self::files(&self.path, settings.show_hidden, &settings.filter, users)?; let sort_kind = SortKind::default(); self.sort(&sort_kind); self.index = 0; |