From 01eaedcd3746e9de30fe867340667c65d8eb2733 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Thu, 7 Jan 2021 09:39:16 +0000 Subject: [PATCH] Parse bytes into keys with Termion --- Cargo.lock | 28 +++++++++++++++++++ Cargo.toml | 1 + src/errors.rs | 2 ++ src/main.rs | 9 +++++- src/tab.rs | 26 +++++++++++------ .../fixtures/layouts/panes-with-plugins.yaml | 2 +- src/wasm_vm.rs | 4 ++- 7 files changed, 60 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8713a3f0..eb4f1770 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -892,6 +892,7 @@ dependencies = [ "signal-hook", "strip-ansi-escapes", "structopt", + "termion", "termios", "unicode-truncate", "unicode-width", @@ -932,6 +933,12 @@ dependencies = [ "libc", ] +[[package]] +name = "numtoa" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" + [[package]] name = "object" version = "0.22.0" @@ -1135,6 +1142,15 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +[[package]] +name = "redox_termios" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +dependencies = [ + "redox_syscall", +] + [[package]] name = "regalloc" version = "0.0.31" @@ -1436,6 +1452,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "termion" +version = "1.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c22cec9d8978d906be5ac94bceb5a010d885c626c4c8855721a4dbd20e3ac905" +dependencies = [ + "libc", + "numtoa", + "redox_syscall", + "redox_termios", +] + [[package]] name = "termios" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index add92d48..9103a685 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ serde_yaml = "0.8" signal-hook = "0.1.10" strip-ansi-escapes = "0.1.0" structopt = "0.3" +termion = "1.5.5" termios = "0.3" unicode-truncate = "0.1.1" unicode-width = "0.1.8" diff --git a/src/errors.rs b/src/errors.rs index 04c25ee0..e53bbc30 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -243,6 +243,7 @@ use crate::wasm_vm::PluginInstruction; pub enum PluginContext { Load, Draw, + Input, Unload, Quit, } @@ -252,6 +253,7 @@ impl From<&PluginInstruction> for PluginContext { match *plugin_instruction { PluginInstruction::Load(..) => PluginContext::Load, PluginInstruction::Draw(..) => PluginContext::Draw, + PluginInstruction::Input(..) => PluginContext::Input, PluginInstruction::Unload(_) => PluginContext::Unload, PluginInstruction::Quit => PluginContext::Quit, } diff --git a/src/main.rs b/src/main.rs index 01d0f232..1cbfac45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ mod utils; mod wasm_vm; +use std::cell::RefCell; use std::io::Write; use std::os::unix::net::UnixStream; use std::path::PathBuf; @@ -24,6 +25,7 @@ use std::thread; use panes::PaneId; use serde::{Deserialize, Serialize}; use structopt::StructOpt; +use termion::input::TermRead; use wasm_vm::PluginInstruction; use crate::command_is_executing::CommandIsExecuting; @@ -37,7 +39,6 @@ use crate::utils::{ consts::{MOSAIC_IPC_PIPE, MOSAIC_TMP_DIR, MOSAIC_TMP_LOG_DIR}, logging::*, }; -use std::cell::RefCell; thread_local!(static OPENCALLS: RefCell = RefCell::default()); @@ -462,6 +463,12 @@ pub fn start(mut os_input: Box, opts: Opt) { buf_tx.send(wasi_stdout(&wasi_env)).unwrap(); } + PluginInstruction::Input(pid, input_bytes) => { + let (instance, wasi_env) = plugin_map.get(&pid).unwrap(); + + let handle_key = instance.exports.get_function("handle_key").unwrap(); + dbg!(input_bytes.keys().collect::>()); + } PluginInstruction::Quit => break, i => panic!("Yo, dawg, nice job calling the wasm thread!\n {:?} is defo not implemented yet...", i), } diff --git a/src/tab.rs b/src/tab.rs index ea709c7c..5eafb9f8 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -472,15 +472,23 @@ impl Tab { } } pub fn write_to_active_terminal(&mut self, input_bytes: Vec) { - if let Some(active_terminal_id) = &self.get_active_terminal_id() { - let active_terminal = self.get_active_pane().unwrap(); - let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes); - self.os_api - .write_to_tty_stdin(*active_terminal_id, &mut adjusted_input) - .expect("failed to write to terminal"); - self.os_api - .tcdrain(*active_terminal_id) - .expect("failed to drain terminal"); + match self.get_active_pane_id() { + Some(PaneId::Terminal(active_terminal_id)) => { + let active_terminal = self.get_active_pane().unwrap(); + let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes); + self.os_api + .write_to_tty_stdin(active_terminal_id, &mut adjusted_input) + .expect("failed to write to terminal"); + self.os_api + .tcdrain(active_terminal_id) + .expect("failed to drain terminal"); + } + Some(PaneId::Plugin(pid)) => { + self.send_plugin_instructions + .send(PluginInstruction::Input(pid, input_bytes)) + .unwrap(); + } + _ => {} } } pub fn get_active_terminal_cursor_position(&self) -> Option<(usize, usize)> { diff --git a/src/tests/fixtures/layouts/panes-with-plugins.yaml b/src/tests/fixtures/layouts/panes-with-plugins.yaml index d64114b5..fe8e7ca4 100644 --- a/src/tests/fixtures/layouts/panes-with-plugins.yaml +++ b/src/tests/fixtures/layouts/panes-with-plugins.yaml @@ -15,4 +15,4 @@ parts: - direction: Vertical split_size: Percent: 20 - #plugin: strider.wasm + plugin: strider.wasm diff --git a/src/wasm_vm.rs b/src/wasm_vm.rs index a81c4fba..6d2d4a19 100644 --- a/src/wasm_vm.rs +++ b/src/wasm_vm.rs @@ -10,6 +10,7 @@ use wasmer_wasi::WasiEnv; pub enum PluginInstruction { Load(Sender, PathBuf), Draw(Sender, u32, usize, usize), // String buffer, plugin id, rows, cols + Input(u32, Vec), // plugin id, input bytes Unload(u32), Quit, } @@ -24,6 +25,7 @@ pub fn mosaic_imports(store: &Store, wasi_env: &WasiEnv) -> ImportObject { } } +// FIXME: Bundle up all of the channels! Pair that with WasiEnv? fn host_open_file(wasi_env: &WasiEnv) { Command::new("xdg-open") .arg(format!( @@ -46,7 +48,7 @@ pub fn wasi_stdout(wasi_env: &WasiEnv) -> String { buf } -pub fn _wasi_write_string(wasi_env: &WasiEnv, buf: &str) { +pub fn wasi_write_string(wasi_env: &WasiEnv, buf: &str) { let mut state = wasi_env.state(); let wasi_file = state.fs.stdin_mut().unwrap().as_mut().unwrap(); writeln!(wasi_file, "{}\r", buf).unwrap();