implement put event

This commit is contained in:
daa84 2016-03-28 18:05:10 +03:00
parent 0b4504e6d7
commit d52311cafa
3 changed files with 41 additions and 18 deletions

View File

@ -2,6 +2,7 @@ use neovim_lib::{Neovim, NeovimApi, Session};
use std::io::{Result, Error, ErrorKind};
use std::sync::Arc;
use std::result;
use ui_model::UiModel;
use ui_mutex::UiMutex;
use rmp::Value;
use rmp::value::Integer;
@ -12,11 +13,13 @@ pub type SharedUi = Arc<UiMutex<Ui>>;
pub trait RedrawEvents {
fn on_cursor_goto(&mut self, row: u64, col: u64);
fn on_put(&mut self, text: &str);
}
macro_rules! try_str {
($exp:expr) => (match $exp {
Value::String(ref val) => val.to_owned(),
Value::String(ref val) => val,
_ => return Err("Can't convert argument to string".to_owned())
})
}
@ -37,6 +40,7 @@ pub fn initialize(mut ui: Ui) -> Result<SharedUi> {
};
let nvim = Neovim::new(session);
ui.set_nvim(nvim);
ui.model = UiModel::new(80, 24);
let sh_ui = Arc::new(UiMutex::new(ui));
@ -60,8 +64,10 @@ fn nvim_cb(ui: &SharedUi, method: &str, params: Vec<Value>) {
for ev in params {
if let Value::Array(ev_args) = ev {
if let Value::String(ref ev_name) = ev_args[0] {
let mut args = vec![];
args.extend_from_slice(&ev_args[1..]);
let args = match ev_args[1] {
Value::Array(ref ar) => ar.clone(),
_ => vec![],
};
call(ui, ev_name, args);
} else {
println!("Unsupported event {:?}", ev_args);
@ -82,6 +88,12 @@ fn call(ui: &SharedUi, method: &str, args: Vec<Value>) {
ui.borrow_mut().on_cursor_goto(try_int!(args[0]), try_int!(args[1]));
Ok(())
})
},
"put" => {
safe_call(ui.clone(), move |ui| {
ui.borrow_mut().on_put(try_str!(args[0]));
Ok(())
})
}
_ => println!("Event {}", method),
};

View File

@ -8,7 +8,7 @@ use ui_model::UiModel;
use nvim::RedrawEvents;
pub struct Ui {
model: UiModel,
pub model: UiModel,
nvim: Option<Neovim>,
}
@ -82,5 +82,9 @@ impl RedrawEvents for Ui {
fn on_cursor_goto(&mut self, row: u64, col: u64) {
self.model.set_cursor(row, col);
}
fn on_put(&mut self, text: &str) {
self.model.put(text);
}
}

View File

@ -9,11 +9,11 @@ impl Cell {
}
pub struct UiModel {
columns: u64,
rows: u64,
cur_row: u64,
cur_col: u64,
model: Vec<Cell>,
columns: usize,
rows: usize,
cur_row: usize,
cur_col: usize,
model: Vec<Vec<Cell>>,
}
impl UiModel {
@ -21,16 +21,18 @@ impl UiModel {
UiModel::new(0, 0)
}
pub fn new(columns: u64, rows: u64) -> UiModel {
let cells = (columns * rows) as usize;
let mut model = Vec::with_capacity(cells);
for i in 0..cells {
model[i] = Cell::new(' ');
pub fn new(rows: u64, columns: u64) -> UiModel {
let mut model = Vec::with_capacity(rows as usize);
for i in 0..rows as usize {
model.push(Vec::with_capacity(columns as usize));
for _ in 0..columns as usize{
model[i].push(Cell::new(' '));
}
}
UiModel {
columns: columns,
rows: rows,
columns: columns as usize,
rows: rows as usize,
cur_row: 0,
cur_col: 0,
model: model,
@ -38,7 +40,12 @@ impl UiModel {
}
pub fn set_cursor(&mut self, row: u64, col: u64) {
self.cur_col = col;
self.cur_row = row;
self.cur_col = col as usize;
self.cur_row = row as usize;
}
pub fn put(&mut self, text: &str) {
self.model[self.cur_row][self.cur_col].ch = text.chars().last().unwrap();
self.cur_col += 1;
}
}