add confirmation dialog on exit when there are unsaved changes
This commit is contained in:
parent
eb9ab8f85b
commit
aecedfb4be
3 changed files with 24 additions and 1 deletions
|
@ -82,6 +82,10 @@ impl Editor {
|
||||||
"*untitled".into()
|
"*untitled".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_unsaved(&self) -> bool {
|
||||||
|
self.unsaved_changes
|
||||||
|
}
|
||||||
|
|
||||||
pub fn path(&self) -> Option<&PathBuf> {
|
pub fn path(&self) -> Option<&PathBuf> {
|
||||||
self.path.as_ref()
|
self.path.as_ref()
|
||||||
}
|
}
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -17,9 +17,9 @@ use std::{
|
||||||
mod clipboard;
|
mod clipboard;
|
||||||
mod editor;
|
mod editor;
|
||||||
mod util;
|
mod util;
|
||||||
use crate::util::{color_highlight, color_reset};
|
|
||||||
use clipboard::Clipboard;
|
use clipboard::Clipboard;
|
||||||
use editor::Editor;
|
use editor::Editor;
|
||||||
|
use util::{color_highlight, color_reset, read_yes_no};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
Navigator::new().run();
|
Navigator::new().run();
|
||||||
|
@ -257,7 +257,16 @@ impl Navigator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn any_unsaved(&self) -> bool {
|
||||||
|
self.editors.iter().any(Editor::is_unsaved)
|
||||||
|
}
|
||||||
|
|
||||||
fn quit(&self) {
|
fn quit(&self) {
|
||||||
|
if self.any_unsaved() {
|
||||||
|
if !read_yes_no("Unsaved changes, quit anyway?", false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
disable_raw_mode().unwrap();
|
disable_raw_mode().unwrap();
|
||||||
execute!(stdout(), LeaveAlternateScreen, cursor::Show).unwrap();
|
execute!(stdout(), LeaveAlternateScreen, cursor::Show).unwrap();
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
10
src/util.rs
10
src/util.rs
|
@ -7,6 +7,16 @@ use crossterm::{
|
||||||
};
|
};
|
||||||
use std::io::{stdout, Write};
|
use std::io::{stdout, Write};
|
||||||
|
|
||||||
|
pub fn read_yes_no(prompt: &str, default: bool) -> bool {
|
||||||
|
let options = if default { "Y/n" } else { "y/N" };
|
||||||
|
let prompt = format!("{prompt} [{options}]: ");
|
||||||
|
match read_line(&prompt).and_then(|s| s.chars().next()) {
|
||||||
|
Some('Y' | 'y') => true,
|
||||||
|
Some('N' | 'n') => false,
|
||||||
|
_ => default,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn read_line(prompt: &str) -> Option<String> {
|
pub fn read_line(prompt: &str) -> Option<String> {
|
||||||
let mut response = String::new();
|
let mut response = String::new();
|
||||||
let size = terminal::size().unwrap();
|
let size = terminal::size().unwrap();
|
||||||
|
|
Loading…
Reference in a new issue