diff --git a/src/action.rs b/src/action.rs index 8984d05..79a3caa 100644 --- a/src/action.rs +++ b/src/action.rs @@ -4,6 +4,7 @@ use crate::{BYTES_PER_LINE, app::{App, Mode, PartialAction}, edit_action::EditAc #[derive(Clone, Copy)] pub enum Action { + QuitIfSaved, Quit, NormalMode, @@ -62,6 +63,7 @@ pub enum Action { impl App { pub fn execute(&mut self, action: Action) { match action { + Action::QuitIfSaved => self.quit_if_saved(), Action::Quit => self.quit(), Action::NormalMode => self.normal_mode(), @@ -118,6 +120,14 @@ impl App { } } + const fn quit_if_saved(&mut self) { + if self.all_changes_saved() { + self.quit(); + } else { + // TODO: show alert of some kind + } + } + const fn quit(&mut self) { self.should_quit = true; } diff --git a/src/app/mod.rs b/src/app/mod.rs index 43b94b4..e55777c 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -168,11 +168,11 @@ impl App { } } - const fn has_unsaved_changes(&self) -> bool { + pub const fn has_unsaved_changes(&self) -> bool { !self.all_changes_saved() } - const fn all_changes_saved(&self) -> bool { + pub const fn all_changes_saved(&self) -> bool { if let Some(last_saved_at) = self.last_saved_at { if let Some(time_traveling) = self.time_traveling { last_saved_at == time_traveling diff --git a/src/config.rs b/src/config.rs index a7098fa..2cf4659 100644 --- a/src/config.rs +++ b/src/config.rs @@ -99,7 +99,8 @@ impl Default for Config { [ (Mode::Normal, [ (None, [ - ("q".try_into().unwrap(), Action::Quit), + ("q".try_into().unwrap(), Action::QuitIfSaved), + ("Q".try_into().unwrap(), Action::Quit), ("v".try_into().unwrap(), Action::SelectMode), @@ -150,7 +151,8 @@ impl Default for Config { ].into()), (Mode::Select, [ (None, [ - ("q".try_into().unwrap(), Action::Quit), + ("q".try_into().unwrap(), Action::QuitIfSaved), + ("Q".try_into().unwrap(), Action::Quit), ("v".try_into().unwrap(), Action::NormalMode),