Parse bytes into keys with Termion

This commit is contained in:
Brooks J Rady 2021-01-07 09:39:16 +00:00
parent b5f9066172
commit 01eaedcd37
7 changed files with 60 additions and 12 deletions

28
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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,
}

View file

@ -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<ErrorContext> = RefCell::default());
@ -462,6 +463,12 @@ pub fn start(mut os_input: Box<dyn OsApi>, 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::<Vec<_>>());
}
PluginInstruction::Quit => break,
i => panic!("Yo, dawg, nice job calling the wasm thread!\n {:?} is defo not implemented yet...", i),
}

View file

@ -472,16 +472,24 @@ impl Tab {
}
}
pub fn write_to_active_terminal(&mut self, input_bytes: Vec<u8>) {
if let Some(active_terminal_id) = &self.get_active_terminal_id() {
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)
.write_to_tty_stdin(active_terminal_id, &mut adjusted_input)
.expect("failed to write to terminal");
self.os_api
.tcdrain(*active_terminal_id)
.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)> {
// (x, y)

View file

@ -15,4 +15,4 @@ parts:
- direction: Vertical
split_size:
Percent: 20
#plugin: strider.wasm
plugin: strider.wasm

View file

@ -10,6 +10,7 @@ use wasmer_wasi::WasiEnv;
pub enum PluginInstruction {
Load(Sender<u32>, PathBuf),
Draw(Sender<String>, u32, usize, usize), // String buffer, plugin id, rows, cols
Input(u32, Vec<u8>), // 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();