only quit if changes are saved
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
+4
-2
@@ -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),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user