implement put event
This commit is contained in:
parent
0b4504e6d7
commit
d52311cafa
18
src/nvim.rs
18
src/nvim.rs
@ -2,6 +2,7 @@ use neovim_lib::{Neovim, NeovimApi, Session};
|
|||||||
use std::io::{Result, Error, ErrorKind};
|
use std::io::{Result, Error, ErrorKind};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::result;
|
use std::result;
|
||||||
|
use ui_model::UiModel;
|
||||||
use ui_mutex::UiMutex;
|
use ui_mutex::UiMutex;
|
||||||
use rmp::Value;
|
use rmp::Value;
|
||||||
use rmp::value::Integer;
|
use rmp::value::Integer;
|
||||||
@ -12,11 +13,13 @@ pub type SharedUi = Arc<UiMutex<Ui>>;
|
|||||||
|
|
||||||
pub trait RedrawEvents {
|
pub trait RedrawEvents {
|
||||||
fn on_cursor_goto(&mut self, row: u64, col: u64);
|
fn on_cursor_goto(&mut self, row: u64, col: u64);
|
||||||
|
|
||||||
|
fn on_put(&mut self, text: &str);
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! try_str {
|
macro_rules! try_str {
|
||||||
($exp:expr) => (match $exp {
|
($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())
|
_ => 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);
|
let nvim = Neovim::new(session);
|
||||||
ui.set_nvim(nvim);
|
ui.set_nvim(nvim);
|
||||||
|
ui.model = UiModel::new(80, 24);
|
||||||
|
|
||||||
let sh_ui = Arc::new(UiMutex::new(ui));
|
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 {
|
for ev in params {
|
||||||
if let Value::Array(ev_args) = ev {
|
if let Value::Array(ev_args) = ev {
|
||||||
if let Value::String(ref ev_name) = ev_args[0] {
|
if let Value::String(ref ev_name) = ev_args[0] {
|
||||||
let mut args = vec![];
|
let args = match ev_args[1] {
|
||||||
args.extend_from_slice(&ev_args[1..]);
|
Value::Array(ref ar) => ar.clone(),
|
||||||
|
_ => vec![],
|
||||||
|
};
|
||||||
call(ui, ev_name, args);
|
call(ui, ev_name, args);
|
||||||
} else {
|
} else {
|
||||||
println!("Unsupported event {:?}", ev_args);
|
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]));
|
ui.borrow_mut().on_cursor_goto(try_int!(args[0]), try_int!(args[1]));
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
"put" => {
|
||||||
|
safe_call(ui.clone(), move |ui| {
|
||||||
|
ui.borrow_mut().on_put(try_str!(args[0]));
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
_ => println!("Event {}", method),
|
_ => println!("Event {}", method),
|
||||||
};
|
};
|
||||||
|
@ -8,7 +8,7 @@ use ui_model::UiModel;
|
|||||||
use nvim::RedrawEvents;
|
use nvim::RedrawEvents;
|
||||||
|
|
||||||
pub struct Ui {
|
pub struct Ui {
|
||||||
model: UiModel,
|
pub model: UiModel,
|
||||||
nvim: Option<Neovim>,
|
nvim: Option<Neovim>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,5 +82,9 @@ impl RedrawEvents for Ui {
|
|||||||
fn on_cursor_goto(&mut self, row: u64, col: u64) {
|
fn on_cursor_goto(&mut self, row: u64, col: u64) {
|
||||||
self.model.set_cursor(row, col);
|
self.model.set_cursor(row, col);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_put(&mut self, text: &str) {
|
||||||
|
self.model.put(text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,11 +9,11 @@ impl Cell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct UiModel {
|
pub struct UiModel {
|
||||||
columns: u64,
|
columns: usize,
|
||||||
rows: u64,
|
rows: usize,
|
||||||
cur_row: u64,
|
cur_row: usize,
|
||||||
cur_col: u64,
|
cur_col: usize,
|
||||||
model: Vec<Cell>,
|
model: Vec<Vec<Cell>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UiModel {
|
impl UiModel {
|
||||||
@ -21,16 +21,18 @@ impl UiModel {
|
|||||||
UiModel::new(0, 0)
|
UiModel::new(0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(columns: u64, rows: u64) -> UiModel {
|
pub fn new(rows: u64, columns: u64) -> UiModel {
|
||||||
let cells = (columns * rows) as usize;
|
let mut model = Vec::with_capacity(rows as usize);
|
||||||
let mut model = Vec::with_capacity(cells);
|
for i in 0..rows as usize {
|
||||||
for i in 0..cells {
|
model.push(Vec::with_capacity(columns as usize));
|
||||||
model[i] = Cell::new(' ');
|
for _ in 0..columns as usize{
|
||||||
|
model[i].push(Cell::new(' '));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UiModel {
|
UiModel {
|
||||||
columns: columns,
|
columns: columns as usize,
|
||||||
rows: rows,
|
rows: rows as usize,
|
||||||
cur_row: 0,
|
cur_row: 0,
|
||||||
cur_col: 0,
|
cur_col: 0,
|
||||||
model: model,
|
model: model,
|
||||||
@ -38,7 +40,12 @@ impl UiModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_cursor(&mut self, row: u64, col: u64) {
|
pub fn set_cursor(&mut self, row: u64, col: u64) {
|
||||||
self.cur_col = col;
|
self.cur_col = col as usize;
|
||||||
self.cur_row = row;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user