diff options
author | Aram Drevekenin <aram@poor.dev> | 2023-06-07 12:43:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-07 12:43:35 +0200 |
commit | c11d75f9157873fc99fe0d40933de8ec5fbb4f6b (patch) | |
tree | fc55dc7f9132395585613dd9cce94c98c8c76600 /zellij-tile | |
parent | b8f095330a57c905f23563ca7c2bfae3171abf57 (diff) |
feat(wasm-plugin-system): major overhaul and some goodies (#2510)
* strider resiliency
* worker channel prototype
* finalized ui
* show hide plugin
* fs events to plugins
* tests for events and new screen instructions
* various refactoringz
* report plugin errors instead of crashing zellij
* fix plugin loading with workers
* refactor: move watch filesystem
* some fixes and refactoring
* refactor(panes): combine pane insertion logic
* refactor(screen): launch or focus
* refactor(pty): consolidate default shell fetching
* refactor: various cleanups
* initial refactoring
* more initial refactoring
* refactor(strider): search
* style(fmt): rustfmt
* style(pty): cleanup
* style(clippy): ok clippy
* style(fmt): rustfmt
Diffstat (limited to 'zellij-tile')
-rw-r--r-- | zellij-tile/src/lib.rs | 65 | ||||
-rw-r--r-- | zellij-tile/src/shim.rs | 29 |
2 files changed, 47 insertions, 47 deletions
diff --git a/zellij-tile/src/lib.rs b/zellij-tile/src/lib.rs index 685de4afe..7968ced3d 100644 --- a/zellij-tile/src/lib.rs +++ b/zellij-tile/src/lib.rs @@ -14,7 +14,6 @@ pub trait ZellijPlugin { } #[allow(unused_variables)] -// TODO: can we get rid of the lifetime? maybe with generics? pub trait ZellijWorker<'de>: Default + Serialize + Deserialize<'de> { fn on_message(&mut self, message: String, payload: String) {} } @@ -54,12 +53,13 @@ macro_rules! register_plugin { #[no_mangle] pub fn update() -> bool { - let object = $crate::shim::object_from_stdin() - .context($crate::PLUGIN_MISMATCH) - .to_stdout() - .unwrap(); - - STATE.with(|state| state.borrow_mut().update(object)) + STATE.with(|state| { + let object = $crate::shim::object_from_stdin() + .context($crate::PLUGIN_MISMATCH) + .to_stdout() + .unwrap(); + state.borrow_mut().update(object) + }) } #[no_mangle] @@ -78,10 +78,14 @@ macro_rules! register_plugin { #[macro_export] macro_rules! register_worker { - ($worker:ty, $worker_name:ident) => { + ($worker:ty, $worker_name:ident, $worker_static_name:ident) => { + // persist worker state in memory in a static variable + thread_local! { + static $worker_static_name: std::cell::RefCell<$worker> = std::cell::RefCell::new(Default::default()); + } #[no_mangle] pub fn $worker_name() { - use serde_json::*; + let worker_display_name = std::stringify!($worker_name); // read message from STDIN @@ -93,43 +97,10 @@ macro_rules! register_worker { ); Default::default() }); - - // read previous worker state from HD if it exists - let mut worker_instance = match std::fs::read(&format!("/data/{}", worker_display_name)) - .map_err(|e| format!("Failed to read file: {:?}", e)) - .and_then(|s| { - serde_json::from_str::<$worker>(&String::from_utf8_lossy(&s)) - .map_err(|e| format!("Failed to deserialize: {:?}", e)) - }) { - Ok(s) => s, - Err(e) => { - eprintln!( - "Failed to read existing state ({:?}), creating new state for worker", - e - ); - <$worker>::default() - }, - }; - - // invoke worker - worker_instance.on_message(message, payload); - - // persist worker state to HD for next run - match serde_json::to_string(&worker_instance) - .map_err(|e| format!("Failed to serialize worker state")) - .and_then(|serialized_state| { - std::fs::write( - &format!("/data/{}", worker_display_name), - serialized_state.as_bytes(), - ) - .map_err(|e| format!("Failed to persist state to HD: {:?}", e)) - }) { - Ok(()) => {}, - Err(e) => eprintln!( - "Failed to serialize and persist worker state to hd: {:?}", - e - ), - } - } + $worker_static_name.with(|worker_instance| { + let mut worker_instance = worker_instance.borrow_mut(); + worker_instance.on_message(message, payload); + }); + } }; } diff --git a/zellij-tile/src/shim.rs b/zellij-tile/src/shim.rs index f1c1fb54d..c43fc8db7 100644 --- a/zellij-tile/src/shim.rs +++ b/zellij-tile/src/shim.rs @@ -39,11 +39,31 @@ pub fn open_file(path: &Path) { unsafe { host_open_file() }; } +pub fn open_file_floating(path: &Path) { + object_to_stdout(&path); + unsafe { host_open_file_floating() }; +} + pub fn open_file_with_line(path: &Path, line: usize) { object_to_stdout(&(path, line)); unsafe { host_open_file_with_line() }; } +pub fn open_file_with_line_floating(path: &Path, line: usize) { + object_to_stdout(&(path, line)); + unsafe { host_open_file_with_line_floating() }; +} + +pub fn open_terminal(path: &Path) { + object_to_stdout(&path); + unsafe { host_open_terminal() }; +} + +pub fn open_terminal_floating(path: &Path) { + object_to_stdout(&path); + unsafe { host_open_terminal_floating() }; +} + pub fn switch_tab_to(tab_idx: u32) { unsafe { host_switch_tab_to(tab_idx) }; } @@ -56,6 +76,10 @@ pub fn exec_cmd(cmd: &[&str]) { unsafe { host_exec_cmd() }; } +pub fn hide_self() { + unsafe { host_hide_self() }; +} + pub fn report_panic(info: &std::panic::PanicInfo) { println!(""); println!("A panic occured in a plugin"); @@ -105,11 +129,16 @@ extern "C" { fn host_get_plugin_ids(); fn host_get_zellij_version(); fn host_open_file(); + fn host_open_file_floating(); fn host_open_file_with_line(); + fn host_open_file_with_line_floating(); + fn host_open_terminal(); + fn host_open_terminal_floating(); fn host_switch_tab_to(tab_idx: u32); fn host_set_timeout(secs: f64); fn host_exec_cmd(); fn host_report_panic(); fn host_post_message_to(); fn host_post_message_to_plugin(); + fn host_hide_self(); } |