From bd9dde064e11dd7c320c226a03895130f818e696 Mon Sep 17 00:00:00 2001 From: daa Date: Sat, 19 Mar 2016 11:47:23 +0300 Subject: [PATCH 1/3] Add screen model --- build.rs | 4 +++- src/main.rs | 1 + src/ui.rs | 2 ++ src/ui_model.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/ui_model.rs diff --git a/build.rs b/build.rs index 8e12b51..afc333b 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,6 @@ fn main() { - println!("cargo:rustc-link-search=native=C:\\msys64\\mingw64\\lib"); + if cfg!(target = "windows") { + println!("cargo:rustc-link-search=native=C:\\msys64\\mingw64\\lib"); + } } diff --git a/src/main.rs b/src/main.rs index 18932b4..21a6425 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate gtk; extern crate cairo; +mod ui_model; mod ui; use ui::Ui; diff --git a/src/ui.rs b/src/ui.rs index ecb1924..bea65c9 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -3,6 +3,8 @@ use gtk; use gtk::prelude::*; use gtk::{Window, WindowType, DrawingArea, Grid, Button, ButtonBox, Orientation}; +use ui_model::UiModel; + pub struct Ui; impl Ui { diff --git a/src/ui_model.rs b/src/ui_model.rs new file mode 100644 index 0000000..18705a6 --- /dev/null +++ b/src/ui_model.rs @@ -0,0 +1,40 @@ +pub struct Cell { + ch: char, +} + +impl Cell { + pub fn new(ch: char) -> Cell { + Cell { ch: ch } + } +} + +pub struct UiModel { + columns: u64, + rows: u64, + cur_row: u64, + cur_col: u64, + model: Vec, +} + +impl UiModel { + 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(' '); + } + + UiModel { + columns: columns, + rows: rows, + cur_row: 0, + cur_col: 0, + model: model, + } + } + + pub fn set_cursor(&mut self, col: u64, row: u64) { + self.cur_col = col; + self.cur_row = row; + } +} From 1043d9ac8e491a50bd81ec874e53e5701cd79891 Mon Sep 17 00:00:00 2001 From: daa Date: Sat, 19 Mar 2016 13:27:39 +0300 Subject: [PATCH 2/3] Nvim connection with events handler --- Cargo.lock | 39 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ Makefile | 2 ++ src/main.rs | 7 ++++++- src/nvim.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/ui.rs | 10 +++++++--- src/ui_model.rs | 4 ++++ 7 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 Makefile create mode 100644 src/nvim.rs diff --git a/Cargo.lock b/Cargo.lock index 1f41c12..385cee3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,6 +5,8 @@ dependencies = [ "cairo-rs 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "glib 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "gtk 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "neovim-lib 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -24,6 +26,11 @@ name = "bitflags" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "byteorder" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "c_vec" version = "1.0.12" @@ -185,6 +192,16 @@ name = "libc" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "neovim-lib" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rmp-serialize 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "pango" version = "0.0.7" @@ -212,3 +229,25 @@ name = "pkg-config" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rmp" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 0.3.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rmp-serialize" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/Cargo.toml b/Cargo.toml index f4bc866..790a99d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ build = "build.rs" [dependencies] cairo-rs = "0.0.8" glib = "0.0.8" +neovim-lib = "0.1" +rmp = "0.7" [dependencies.gtk] version = "0.0.7" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..38cad81 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +test: + cargo test diff --git a/src/main.rs b/src/main.rs index 21a6425..1b17022 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,17 @@ extern crate gtk; extern crate cairo; +extern crate neovim_lib; +extern crate rmp; +mod nvim; mod ui_model; mod ui; use ui::Ui; +use nvim::Nvim; fn main() { - Ui::new().start(); + let nvim = Nvim::start().expect("Can't start nvim instance"); + Ui::new().show(); } diff --git a/src/nvim.rs b/src/nvim.rs new file mode 100644 index 0000000..ea3382d --- /dev/null +++ b/src/nvim.rs @@ -0,0 +1,43 @@ +use neovim_lib::{Neovim, Session}; +use std::io::{Result, Error, ErrorKind}; +use rmp::Value; + +pub struct Nvim { + nvim: Neovim +} + +impl Nvim { + pub fn start() -> Result { + let mut session = try!(Session::new_tcp("127.0.0.1:6666")); + //let mut session = try!(Session::new_child()); + + session.start_event_loop_cb(Nvim::cb); + + let mut nvim = Neovim::new(session); + try!(nvim.ui_attach(80, 24, true).map_err(|e| Error::new(ErrorKind::Other, e))); + + Ok(Nvim { + nvim: nvim, + }) + } + + fn cb(method: &str, params: Vec) { + 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); + } + else { + println!("Unsupported event {:?}", ev_args); + } + } + else { + println!("Unsupported event type {:?}", ev); + } + } + } else { + println!("Notification {}", method); + } + } +} diff --git a/src/ui.rs b/src/ui.rs index bea65c9..ceb0e49 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -5,14 +5,18 @@ use gtk::{Window, WindowType, DrawingArea, Grid, Button, ButtonBox, Orientation} use ui_model::UiModel; -pub struct Ui; +pub struct Ui { + model: UiModel, +} impl Ui { pub fn new() -> Ui { - Ui + Ui { + model: UiModel::empty(), + } } - pub fn start(&self) { + pub fn show(&self) { gtk::init().expect("Failed to initialize GTK"); let window = Window::new(WindowType::Toplevel); diff --git a/src/ui_model.rs b/src/ui_model.rs index 18705a6..fb50497 100644 --- a/src/ui_model.rs +++ b/src/ui_model.rs @@ -17,6 +17,10 @@ pub struct UiModel { } impl UiModel { + pub fn empty() -> 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); From dc1bf55a46927329e0b30db363c9dc7d0b9d058f Mon Sep 17 00:00:00 2001 From: daa Date: Sat, 19 Mar 2016 14:32:59 +0300 Subject: [PATCH 3/3] Fix embed mode --- Cargo.lock | 3 +-- src/nvim.rs | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 385cee3..d5c44c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,7 +5,7 @@ dependencies = [ "cairo-rs 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "glib 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)", "gtk 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "neovim-lib 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "neovim-lib 0.1.0", "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -195,7 +195,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "neovim-lib" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rmp 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "rmp-serialize 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/src/nvim.rs b/src/nvim.rs index ea3382d..016b4ed 100644 --- a/src/nvim.rs +++ b/src/nvim.rs @@ -1,4 +1,4 @@ -use neovim_lib::{Neovim, Session}; +use neovim_lib::{Neovim, NeovimApi, Session}; use std::io::{Result, Error, ErrorKind}; use rmp::Value; @@ -8,12 +8,14 @@ pub struct Nvim { impl Nvim { pub fn start() -> Result { - let mut session = try!(Session::new_tcp("127.0.0.1:6666")); - //let mut session = try!(Session::new_child()); + //let mut session = try!(Session::new_tcp("127.0.0.1:6666")); + let mut session = try!(Session::new_child()); session.start_event_loop_cb(Nvim::cb); let mut nvim = Neovim::new(session); + // fix neovim --embed bug to start embed mode + nvim.input("i").unwrap(); try!(nvim.ui_attach(80, 24, true).map_err(|e| Error::new(ErrorKind::Other, e))); Ok(Nvim {