From 74c4075f06480e089739f1a2380764fce9a356e4 Mon Sep 17 00:00:00 2001 From: KillTheMule Date: Thu, 16 Jan 2020 16:20:06 +0100 Subject: [PATCH] Make main() own the channel, and pass it off --- Cargo.toml | 1 + src/main.rs | 40 +++++++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d95a486..3d1f1f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ msgbox = "0.4.0" rust-embed = { version = "5.2.0", features = ["debug-embed"] } image = "0.22.3" nvim-rs = "0.1.0" +#nvim-rs = { git = "https://github.com/KillTheMule/nvim-rs", branch = "master" } tokio = { version = "0.2.9", features = [ "blocking" ] } async-trait = "0.1.18" diff --git a/src/main.rs b/src/main.rs index e1c3d67..947ccdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,21 +8,20 @@ mod renderer; mod error_handling; mod ui_commands; -#[macro_use] extern crate async_trait; #[macro_use] extern crate derive_new; #[macro_use] extern crate rust_embed; use std::sync::{Arc, Mutex}; -use std::thread; +//use std::thread; use std::process::Stdio; -use std::sync::mpsc::{channel, Receiver, Sender}; +use std::sync::mpsc::{channel, Receiver}; use async_trait::async_trait; use rmpv::Value; use nvim_rs::runtime::ChildStdin; use nvim_rs::{create, Neovim, UiAttachOptions, Handler}; use tokio::process::Command; -use tokio::task::spawn_blocking; +use tokio::runtime::Runtime; use window::ui_loop; use editor::Editor; @@ -58,7 +57,7 @@ struct NeovimHandler(Arc>); impl Handler for NeovimHandler { type Writer = ChildStdin; - async fn handle_notify(&self, event_name: String, arguments: Vec, neovim: Neovim) { + async fn handle_notify(&self, event_name: String, arguments: Vec, _neovim: Neovim) { dbg!(&event_name); let parsed_events = parse_neovim_event(event_name, arguments) .unwrap_or_explained_panic("Could not parse event", "Could not parse event from neovim"); @@ -69,8 +68,7 @@ impl Handler for NeovimHandler { } } -#[tokio::main] -async fn start_nvim(editor: Arc>) -> Sender { +async fn start_nvim(editor: Arc>, receiver: Receiver) { let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler(editor.clone())).await .unwrap_or_explained_panic("Could not create nvim process", "Could not locate or start the neovim process"); @@ -86,23 +84,31 @@ async fn start_nvim(editor: Arc>) -> Sender { let mut options = UiAttachOptions::new(); options.set_linegrid_external(true); options.set_rgb(true); - nvim.set_var("neovide", Value::Boolean(true)).await; + nvim.set_var("neovide", Value::Boolean(true)).await + .unwrap_or_explained_panic("Could not communicate.", "Could not communicate with neovim process"); nvim.ui_attach(INITIAL_WIDTH as i64, INITIAL_HEIGHT as i64, &options).await .unwrap_or_explained_panic("Could not attach.", "Could not attach ui to neovim process"); - let (mut sender, receiver) = channel::(); - tokio::spawn(async move { - while let Ok(ui_command) = spawn_blocking(|| receiver.recv()).await.unwrap() { - dbg!(&ui_command); - ui_command.execute(&nvim).await; - } - }); + loop { + let r = receiver.recv(); - sender + if let Ok(ui_command) = r { + dbg!(&ui_command); + ui_command.execute(&nvim).await; + } else { + return + } + } } fn main() { + let rt = Runtime::new().unwrap(); + let editor = Arc::new(Mutex::new(Editor::new(INITIAL_WIDTH, INITIAL_HEIGHT))); - let sender = start_nvim(editor.clone()); + let (sender, receiver) = channel::(); + let editor_clone = editor.clone(); + rt.spawn(async move { + start_nvim(editor_clone, receiver).await; + }); ui_loop(editor, sender, (INITIAL_WIDTH, INITIAL_HEIGHT)); }