First try to find changed values

This commit is contained in:
daa 2017-04-01 13:00:14 +03:00
parent 5cdc104b40
commit e717c92f33
3 changed files with 73 additions and 8 deletions

View File

@ -18,6 +18,7 @@ mod shell;
mod input;
mod settings;
mod cursor;
mod shell_dlg;
use std::thread;
use std::env;

62
src/shell_dlg.rs Normal file
View File

@ -0,0 +1,62 @@
use ui::{SH, Ui};
use neovim_lib::{NeovimApi, CallError, Value};
use gtk;
use gtk_sys;
use gtk::{Dialog, DialogExt};
pub fn can_close_window(ui: &Ui) -> bool {
match get_changed_buffers() {
Ok(vec) => {
if !vec.is_empty() {
show_not_saved_dlg(ui, &vec)
} else {
true
}
},
Err(ref err) => {
println!("Error getting info from nvim: {}", err);
true
}
}
}
fn show_not_saved_dlg(ui: &Ui, changed_bufs: &Vec<String>) -> bool {
let flags = gtk::DIALOG_MODAL | gtk::DIALOG_DESTROY_WITH_PARENT;
let dlg = Dialog::new_with_buttons(Some("Question"), ui.window.as_ref(), flags, &[
("_OK", gtk_sys::GTK_RESPONSE_ACCEPT as i32),
("_Cancel",
gtk_sys::GTK_RESPONSE_REJECT as i32)
]);
dlg.run();
true
}
fn get_changed_buffers() -> Result<Vec<String>, CallError> {
SHELL!(shell = {
let mut nvim = shell.nvim();
let buffers = nvim.get_buffers()?;
Ok(buffers.iter()
.map(|buf| {
(match buf.get_option(&mut nvim, "modified") {
Ok(Value::Boolean(val)) => val,
_ => {
println!("Value must be boolean");
false
},
},
match buf.get_name(&mut nvim) {
Ok(name) => name,
Err(ref err) => {
println!("Something going wrong while getting buffer name: {}", err);
"<Error>".to_owned()
},
})
})
.filter(|e| e.0)
.map(|e| e.1)
.collect())
})
}

View File

@ -13,6 +13,7 @@ use neovim_lib::NeovimApi;
use settings;
use shell::{Shell, NvimMode};
use shell_dlg;
use nvim::ErrorReport;
macro_rules! ui_thread_var {
@ -169,16 +170,17 @@ fn edit_save_all() {
});
}
fn quit() {
UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
ui.destroy();
});
}
fn gtk_delete(_: &ApplicationWindow, _: &Event) -> Inhibit {
quit();
Inhibit(false)
Inhibit(UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut();
if shell_dlg::can_close_window(&ui) {
ui.destroy();
false
} else {
true
}
}))
}