diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-12-10 22:44:17 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-12-10 22:44:17 +0100 |
commit | 273627ad94ddb0e3df5ebaa3a8a711b6d021ee69 (patch) | |
tree | a4d3559f9cbf43fa81819e5bab906c1d223d103c /src | |
parent | 7a07091f9819b54a1ed66db017e065bf7b8f5b58 (diff) |
documentation & session refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/app/mod.rs | 4 | ||||
-rw-r--r-- | src/app/session.rs (renamed from src/app/display_settings.rs) | 69 | ||||
-rw-r--r-- | src/app/status.rs | 8 |
3 files changed, 45 insertions, 36 deletions
diff --git a/src/app/mod.rs b/src/app/mod.rs index 4b3ce92..7625f36 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,16 +1,16 @@ mod application; -mod display_settings; mod header_footer; mod internal_settings; mod refresher; +mod session; mod status; mod tab; pub use application::FM; -pub use display_settings::DisplaySettings; pub use header_footer::{ClickableLine, Footer, Header}; pub use internal_settings::InternalSettings; pub use refresher::Refresher; +pub use session::Session; pub use status::Status; pub use status::Window; pub use tab::Tab; diff --git a/src/app/display_settings.rs b/src/app/session.rs index fa3ffd5..659b89b 100644 --- a/src/app/display_settings.rs +++ b/src/app/session.rs @@ -1,6 +1,5 @@ use std::fs::File; -use anyhow::Result; use serde::Serialize; use serde_yaml::{Error as YamlError, Value as YamlValue}; @@ -8,6 +7,11 @@ use crate::common::SESSION_PATH; use crate::io::MIN_WIDTH_FOR_DUAL_PANE; use crate::log_info; +/// Everything about the current session. +/// We keep track of display settings (metadata, dual pane, second pane as preview). +/// Display hidden files is read from args or set to false by default. +/// Since it's specific to a tab, it's not stored here. +/// /// Reads its display values from a session file and updates them when modified. /// The file is stored at [`crate::common::SESSION_PATH`] which points to `~/.config/fm/session.yaml`. /// Unreachable or unreadable files are ignored. @@ -17,7 +21,7 @@ use crate::log_info; /// - do we display files metadata ? Default to true. /// - do we use to second pane to preview files ? Default to false. #[derive(Debug, Serialize)] -pub struct DisplaySettings { +pub struct Session { /// do we display one or two tabs ? pub dual: bool, /// do we display all info or only the filenames ? @@ -29,7 +33,7 @@ pub struct DisplaySettings { filepath: String, } -impl Default for DisplaySettings { +impl Default for Session { fn default() -> Self { Self { dual: true, @@ -40,7 +44,10 @@ impl Default for DisplaySettings { } } -impl DisplaySettings { +impl Session { + /// Creates a new instance of `DisplaySettings`. + /// Tries to read them from the session file. + /// Use default value if the file can't be read. pub fn new(width: usize) -> Self { Self::default().update_from_config(width) } @@ -69,7 +76,6 @@ impl DisplaySettings { YamlValue::Bool(value) => self.preview = value, _ => self.preview = false, } - log_info!("{self:?}"); self } @@ -81,54 +87,57 @@ impl DisplaySettings { } /// True iff the terminal is wide enough to display two panes - /// - /// # Errors - /// - /// Fail if the terminal has crashed pub fn display_wide_enough(width: usize) -> bool { width >= MIN_WIDTH_FOR_DUAL_PANE } + /// True if we display 2 tabs. + /// It requires two conditions: + /// 1. The display should be wide enough, bigger than [`crate::io::MIN_WIDTH_FOR_DUAL_PANE`]. + /// 2. The `dual_tab` setting must be true. pub fn use_dual_tab(&self, width: usize) -> bool { self.dual && Self::display_wide_enough(width) } pub fn set_dual(&mut self, dual: bool) { self.dual = dual; - match self.update_yaml_file() { - Ok(()) => (), - Err(error) => log_info!("Error while updating session file {error:?}"), - }; + self.update_yaml_file(); } pub fn toggle_dual(&mut self) { self.dual = !self.dual; - match self.update_yaml_file() { - Ok(()) => (), - Err(error) => log_info!("Error while updating session file {error:?}"), - }; + self.update_yaml_file(); } pub fn toggle_metadata(&mut self) { self.metadata = !self.metadata; - match self.update_yaml_file() { - Ok(()) => (), - Err(error) => log_info!("Error while updating session file {error:?}"), - }; + self.update_yaml_file(); } pub fn toggle_preview(&mut self) { self.preview = !self.preview; - match self.update_yaml_file() { - Ok(()) => (), - Err(error) => log_info!("Error while updating session file {error:?}"), - }; + self.update_yaml_file(); } - fn update_yaml_file(&self) -> Result<()> { - let mut file = File::create(&self.filepath)?; - serde_yaml::to_writer(&mut file, &self)?; - - Ok(()) + /// Writes itself to the session file. + /// Does nothing if an error is encountered while creating or writing to the session file. + fn update_yaml_file(&self) { + let mut file = match File::create(&self.filepath) { + Ok(file) => file, + Err(error) => { + log_info!( + "Couldn't create session {file}. Error: {error:?}", + file = self.filepath + ); + return; + } + }; + match serde_yaml::to_writer(&mut file, &self) { + Ok(()) => (), + Err(e) => log_info!( + "Couldn't write config to session {file}. Error: {e:?}", + file = self.filepath + ), + } } } diff --git a/src/app/status.rs b/src/app/status.rs index 35afe7c..2653757 100644 --- a/src/app/status.rs +++ b/src/app/status.rs @@ -10,10 +10,10 @@ use tuikit::prelude::{from_keyname, Event}; use tuikit::term::Term; use crate::app::ClickableLine; -use crate::app::DisplaySettings; use crate::app::Footer; use crate::app::Header; use crate::app::InternalSettings; +use crate::app::Session; use crate::app::Tab; use crate::common::{args_is_empty, is_sudo_command, path_to_string}; use crate::common::{current_username, disk_space, filename_from_path, is_program_in_path}; @@ -77,7 +77,7 @@ pub struct Status { /// Navigable menu pub menu: Menu, /// Display settings - pub display_settings: DisplaySettings, + pub display_settings: Session, /// Interna settings pub internal_settings: InternalSettings, } @@ -98,7 +98,7 @@ impl Status { path.parent().context("")? }; let sys = System::new_with_specifics(RefreshKind::new().with_disks()); - let display_settings = DisplaySettings::new(term.term_size()?.0); + let display_settings = Session::new(term.term_size()?.0); let mut internal_settings = InternalSettings::new(opener, term, sys); let mount_points = internal_settings.mount_points(); let menu = Menu::new(start_dir, &mount_points)?; @@ -340,7 +340,7 @@ impl Status { /// Force preview the selected file of the first pane in the second pane. /// Doesn't check if it has do. pub fn set_second_pane_for_preview(&mut self) -> Result<()> { - if !DisplaySettings::display_wide_enough(self.term_size()?.0) { + if !Session::display_wide_enough(self.term_size()?.0) { self.tabs[1].preview = Preview::empty(); return Ok(()); } |