forked from mars/breed
Add error on unsaved quit
This commit is contained in:
parent
b7715532f8
commit
f04b0ecaab
18
src/main.rs
18
src/main.rs
|
@ -31,9 +31,10 @@ use crossbeam_channel::Sender;
|
|||
use crossterm::{
|
||||
cursor,
|
||||
event::{Event, KeyCode, KeyEvent},
|
||||
terminal, Result, QueueableCommand,
|
||||
terminal, QueueableCommand, Result,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use ropey::Rope;
|
||||
use yacexits::{exit, EX_DATAERR, EX_UNAVAILABLE};
|
||||
|
||||
mod actions;
|
||||
|
@ -106,6 +107,7 @@ pub struct State {
|
|||
pub cursor: Cursor,
|
||||
pub file: Option<OsString>,
|
||||
pub mode: Mode,
|
||||
pub last_saved: Rope,
|
||||
pub scroll: Cursor,
|
||||
pub size: (usize, usize),
|
||||
pub quit: bool,
|
||||
|
@ -116,6 +118,7 @@ impl State {
|
|||
pub fn from_str(file_name: Option<OsString>, text: &str) -> Result<Self> {
|
||||
let styles = Arc::new(Mutex::new(StyleStore::default()));
|
||||
let buffer = Buffer::from_str(styles.clone(), text);
|
||||
let last_saved = buffer.as_ref().clone();
|
||||
let (cols, rows) = terminal::size()?;
|
||||
let theme_tx = config::ThemeWatcher::spawn(styles.clone());
|
||||
|
||||
|
@ -125,6 +128,7 @@ impl State {
|
|||
cursor: Cursor::default(),
|
||||
file: file_name,
|
||||
mode: Mode::default(),
|
||||
last_saved,
|
||||
scroll: Cursor::default(),
|
||||
size: (cols as usize, rows as usize),
|
||||
quit: false,
|
||||
|
@ -327,11 +331,10 @@ impl State {
|
|||
}
|
||||
|
||||
fn write_buffer(&mut self, file: OsString) -> Result<()> {
|
||||
self.last_saved = self.buffer.as_ref().clone();
|
||||
let out = self.buffer.as_ref().bytes().collect::<Vec<u8>>();
|
||||
let mut handle = OpenOptions::new().write(true).open(file)?;
|
||||
|
||||
handle.write_all(out.as_slice())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -360,7 +363,14 @@ impl State {
|
|||
let args = &command_parts[1..];
|
||||
|
||||
match *command {
|
||||
"q" => self.quit = true,
|
||||
"q!" => self.quit = true,
|
||||
"q" => {
|
||||
if self.last_saved == *self.buffer.as_ref() {
|
||||
self.quit = true;
|
||||
} else {
|
||||
return Err("Buffer is unsaved (add ! to override".into());
|
||||
}
|
||||
}
|
||||
"w" => return self.write_command(command, args),
|
||||
"wq" => {
|
||||
self.write_command(command, args)?;
|
||||
|
|
Reference in New Issue