neovim-gtk/src/nvim.rs

57 lines
1.6 KiB
Rust
Raw Normal View History

2016-03-19 11:32:59 +00:00
use neovim_lib::{Neovim, NeovimApi, Session};
2016-03-19 10:27:39 +00:00
use std::io::{Result, Error, ErrorKind};
2016-03-25 09:51:28 +00:00
use std::sync::Arc;
use ui_mutex::UiMutex;
2016-03-19 10:27:39 +00:00
use rmp::Value;
use ui::Ui;
2016-03-19 10:27:39 +00:00
pub type SharedUi = Arc<UiMutex<Ui>>;
2016-03-23 11:59:18 +00:00
pub trait RedrawEvents {
2016-03-19 10:27:39 +00:00
}
pub fn initialize(mut ui: Ui) -> Result<SharedUi> {
// let mut session = try!(Session::new_tcp("127.0.0.1:6666"));
let session = if cfg!(target_os = "windows") {
Session::new_child_path("E:\\Neovim\\bin\\nvim.exe").unwrap()
} else {
Session::new_child().unwrap()
};
let nvim = Neovim::new(session);
ui.set_nvim(nvim);
let sh_ui = Arc::new(UiMutex::new(ui));
{
let mut ui = (*sh_ui).borrow_mut();
let mut nvim = ui.nvim();
let moved_sh_ui = sh_ui.clone();
nvim.session.start_event_loop_cb(move |m, p| nvim_cb(&moved_sh_ui, m, p));
2016-03-19 11:32:59 +00:00
// fix neovim --embed bug to start embed mode
nvim.input("i").unwrap();
2016-03-19 10:27:39 +00:00
try!(nvim.ui_attach(80, 24, true).map_err(|e| Error::new(ErrorKind::Other, e)));
}
Ok(sh_ui)
}
fn nvim_cb(ui: &SharedUi, method: &str, params: Vec<Value>) {
if method == "redraw" {
for ev in params {
if let Value::Array(ev_args) = ev {
if let Value::String(ref ev_name) = ev_args[0] {
println!("Event {}", ev_name);
2016-03-23 11:59:18 +00:00
} else {
println!("Unsupported event {:?}", ev_args);
2016-03-19 10:27:39 +00:00
}
} else {
println!("Unsupported event type {:?}", ev);
2016-03-19 10:27:39 +00:00
}
}
} else {
println!("Notification {}", method);
2016-03-19 10:27:39 +00:00
}
}