diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-11-28 21:43:12 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-11-28 21:43:12 +0100 |
commit | 63728ece2d9e7eb45880f6b3f2c7b0a904165c0b (patch) | |
tree | 5efe83e94fbe298dfb88ad0d4208ded99cf375a1 /src/app | |
parent | 0cb4542238459ba5c369231044121d3ded4a1971 (diff) |
use settings for tabs. Use better name for tab selecting
Diffstat (limited to 'src/app')
-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 |
4 files changed, 68 insertions, 51 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)); } _ => (), |