refactor(plugins): improve api (#2552)

* refactor(plugins): improve shim API

* style(fmt): rustfmt
This commit is contained in:
Aram Drevekenin 2023-06-17 17:47:28 +02:00 committed by GitHub
parent 10b7f3a981
commit 29a391f60e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 76 deletions

View file

@ -219,11 +219,7 @@ impl ZellijPlugin for State {
}, },
Event::SystemClipboardFailure => { Event::SystemClipboardFailure => {
// this is just to trigger the worker message // this is just to trigger the worker message
post_message_to( post_message_to("test", "ping", "gimme_back_my_payload");
"test",
"ping".to_owned(),
"gimme_back_my_payload".to_owned(),
);
}, },
_ => {}, _ => {},
} }

View file

@ -32,13 +32,13 @@ impl ZellijPlugin for State {
]); ]);
post_message_to( post_message_to(
"file_name_search", "file_name_search",
serde_json::to_string(&MessageToSearch::ScanFolder).unwrap(), &serde_json::to_string(&MessageToSearch::ScanFolder).unwrap(),
"".to_owned(), "",
); );
post_message_to( post_message_to(
"file_contents_search", "file_contents_search",
serde_json::to_string(&MessageToSearch::ScanFolder).unwrap(), &serde_json::to_string(&MessageToSearch::ScanFolder).unwrap(),
"".to_owned(), "",
); );
self.search_state.loading = true; self.search_state.loading = true;
set_timeout(0.5); // for displaying loading animation set_timeout(0.5); // for displaying loading animation
@ -192,13 +192,13 @@ impl ZellijPlugin for State {
.collect(); .collect();
post_message_to( post_message_to(
"file_name_search", "file_name_search",
serde_json::to_string(&MessageToSearch::FileSystemCreate).unwrap(), &serde_json::to_string(&MessageToSearch::FileSystemCreate).unwrap(),
serde_json::to_string(&paths).unwrap(), &serde_json::to_string(&paths).unwrap(),
); );
post_message_to( post_message_to(
"file_contents_search", "file_contents_search",
serde_json::to_string(&MessageToSearch::FileSystemCreate).unwrap(), &serde_json::to_string(&MessageToSearch::FileSystemCreate).unwrap(),
serde_json::to_string(&paths).unwrap(), &serde_json::to_string(&paths).unwrap(),
); );
}, },
Event::FileSystemUpdate(paths) => { Event::FileSystemUpdate(paths) => {
@ -208,13 +208,13 @@ impl ZellijPlugin for State {
.collect(); .collect();
post_message_to( post_message_to(
"file_name_search", "file_name_search",
serde_json::to_string(&MessageToSearch::FileSystemUpdate).unwrap(), &serde_json::to_string(&MessageToSearch::FileSystemUpdate).unwrap(),
serde_json::to_string(&paths).unwrap(), &serde_json::to_string(&paths).unwrap(),
); );
post_message_to( post_message_to(
"file_contents_search", "file_contents_search",
serde_json::to_string(&MessageToSearch::FileSystemUpdate).unwrap(), &serde_json::to_string(&MessageToSearch::FileSystemUpdate).unwrap(),
serde_json::to_string(&paths).unwrap(), &serde_json::to_string(&paths).unwrap(),
); );
}, },
Event::FileSystemDelete(paths) => { Event::FileSystemDelete(paths) => {
@ -224,13 +224,13 @@ impl ZellijPlugin for State {
.collect(); .collect();
post_message_to( post_message_to(
"file_name_search", "file_name_search",
serde_json::to_string(&MessageToSearch::FileSystemDelete).unwrap(), &serde_json::to_string(&MessageToSearch::FileSystemDelete).unwrap(),
serde_json::to_string(&paths).unwrap(), &serde_json::to_string(&paths).unwrap(),
); );
post_message_to( post_message_to(
"file_contents_search", "file_contents_search",
serde_json::to_string(&MessageToSearch::FileSystemDelete).unwrap(), &serde_json::to_string(&MessageToSearch::FileSystemDelete).unwrap(),
serde_json::to_string(&paths).unwrap(), &serde_json::to_string(&paths).unwrap(),
); );
}, },
_ => { _ => {

View file

@ -155,13 +155,13 @@ impl SearchState {
if !self.search_term.is_empty() { if !self.search_term.is_empty() {
post_message_to( post_message_to(
"file_name_search", "file_name_search",
serde_json::to_string(&MessageToSearch::Search).unwrap(), &serde_json::to_string(&MessageToSearch::Search).unwrap(),
"".to_owned(), "",
); );
post_message_to( post_message_to(
"file_contents_search", "file_contents_search",
serde_json::to_string(&MessageToSearch::Search).unwrap(), &serde_json::to_string(&MessageToSearch::Search).unwrap(),
"".to_owned(), "",
); );
self.file_name_search_results.clear(); self.file_name_search_results.clear();
self.file_contents_search_results.clear(); self.file_contents_search_results.clear();

View file

@ -1,5 +1,4 @@
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use std::str::FromStr;
use std::{io, path::Path}; use std::{io, path::Path};
use zellij_utils::data::*; use zellij_utils::data::*;
use zellij_utils::errors::prelude::*; use zellij_utils::errors::prelude::*;
@ -35,43 +34,49 @@ pub fn get_zellij_version() -> String {
// Host Functions // Host Functions
pub fn open_file(path: &Path) { pub fn open_file<P: AsRef<Path>>(path: P) {
object_to_stdout(&path); object_to_stdout(&path.as_ref());
unsafe { host_open_file() }; unsafe { host_open_file() };
} }
pub fn open_file_floating(path: &Path) { pub fn open_file_floating<P: AsRef<Path>>(path: P) {
object_to_stdout(&path); object_to_stdout(&path.as_ref());
unsafe { host_open_file_floating() }; unsafe { host_open_file_floating() };
} }
pub fn open_file_with_line(path: &Path, line: usize) { pub fn open_file_with_line<P: AsRef<Path>>(path: P, line: usize) {
object_to_stdout(&(path, line)); object_to_stdout(&(path.as_ref(), line));
unsafe { host_open_file_with_line() }; unsafe { host_open_file_with_line() };
} }
pub fn open_file_with_line_floating(path: &Path, line: usize) { pub fn open_file_with_line_floating<P: AsRef<Path>>(path: P, line: usize) {
object_to_stdout(&(path, line)); object_to_stdout(&(path.as_ref(), line));
unsafe { host_open_file_with_line_floating() }; unsafe { host_open_file_with_line_floating() };
} }
pub fn open_terminal(path: &Path) { pub fn open_terminal<P: AsRef<Path>>(path: P) {
object_to_stdout(&path); object_to_stdout(&path.as_ref());
unsafe { host_open_terminal() }; unsafe { host_open_terminal() };
} }
pub fn open_terminal_floating(path: &Path) { pub fn open_terminal_floating<P: AsRef<Path>>(path: P) {
object_to_stdout(&path); object_to_stdout(&path.as_ref());
unsafe { host_open_terminal_floating() }; unsafe { host_open_terminal_floating() };
} }
pub fn open_command_pane(path: &Path, args: Vec<String>) { pub fn open_command_pane<P: AsRef<Path>, A: AsRef<str>>(path: P, args: Vec<A>) {
object_to_stdout(&(path, args)); object_to_stdout(&(
path.as_ref(),
args.iter().map(|a| a.as_ref()).collect::<Vec<&str>>(),
));
unsafe { host_open_command_pane() }; unsafe { host_open_command_pane() };
} }
pub fn open_command_pane_floating(path: &Path, args: Vec<String>) { pub fn open_command_pane_floating<P: AsRef<Path>, A: AsRef<str>>(path: P, args: Vec<A>) {
object_to_stdout(&(path, args)); object_to_stdout(&(
path.as_ref(),
args.iter().map(|a| a.as_ref()).collect::<Vec<&str>>(),
));
unsafe { host_open_command_pane_floating() }; unsafe { host_open_command_pane_floating() };
} }
@ -290,40 +295,19 @@ pub fn focus_plugin_pane(plugin_pane_id: i32, should_float_if_hidden: bool) {
unsafe { host_focus_plugin_pane(plugin_pane_id, should_float_if_hidden as i32) }; unsafe { host_focus_plugin_pane(plugin_pane_id, should_float_if_hidden as i32) };
} }
pub fn rename_terminal_pane(terminal_pane_id: i32, new_name: &str) { pub fn rename_terminal_pane<S: AsRef<str>>(terminal_pane_id: i32, new_name: S) {
match String::from_str(new_name) { object_to_stdout(&(terminal_pane_id, new_name.as_ref()));
Ok(new_name) => { unsafe { host_rename_terminal_pane() };
object_to_stdout(&(terminal_pane_id, new_name));
unsafe { host_rename_terminal_pane() };
},
Err(e) => {
eprintln!("Failed to rename terminal: {:?}", e)
},
}
} }
pub fn rename_plugin_pane(plugin_pane_id: i32, new_name: &str) { pub fn rename_plugin_pane<S: AsRef<str>>(plugin_pane_id: i32, new_name: S) {
match String::from_str(new_name) { object_to_stdout(&(plugin_pane_id, new_name.as_ref()));
Ok(new_name) => { unsafe { host_rename_plugin_pane() };
object_to_stdout(&(plugin_pane_id, new_name));
unsafe { host_rename_plugin_pane() };
},
Err(e) => {
eprintln!("Failed to rename plugin: {:?}", e)
},
}
} }
pub fn rename_tab(tab_position: i32, new_name: &str) { pub fn rename_tab<S: AsRef<str>>(tab_position: i32, new_name: S) {
match String::from_str(new_name) { object_to_stdout(&(tab_position, new_name.as_ref()));
Ok(new_name) => { unsafe { host_rename_tab() };
object_to_stdout(&(tab_position, new_name));
unsafe { host_rename_tab() };
},
Err(e) => {
eprintln!("Failed to rename tab: {:?}", e)
},
}
} }
// Internal Functions // Internal Functions
@ -344,8 +328,8 @@ pub fn object_to_stdout(object: &impl Serialize) {
} }
#[doc(hidden)] #[doc(hidden)]
pub fn post_message_to(worker_name: &str, message: String, payload: String) { pub fn post_message_to<S: AsRef<str>>(worker_name: S, message: S, payload: S) {
match serde_json::to_string(&(worker_name, message, payload)) { match serde_json::to_string(&(worker_name.as_ref(), message.as_ref(), payload.as_ref())) {
Ok(serialized) => println!("{}", serialized), Ok(serialized) => println!("{}", serialized),
Err(e) => eprintln!("Failed to serialize message: {:?}", e), Err(e) => eprintln!("Failed to serialize message: {:?}", e),
} }
@ -353,8 +337,8 @@ pub fn post_message_to(worker_name: &str, message: String, payload: String) {
} }
#[doc(hidden)] #[doc(hidden)]
pub fn post_message_to_plugin(message: String, payload: String) { pub fn post_message_to_plugin<S: AsRef<str>>(message: S, payload: S) {
match serde_json::to_string(&(message, payload)) { match serde_json::to_string(&(message.as_ref(), payload.as_ref())) {
Ok(serialized) => println!("{}", serialized), Ok(serialized) => println!("{}", serialized),
Err(e) => eprintln!("Failed to serialize message: {:?}", e), Err(e) => eprintln!("Failed to serialize message: {:?}", e),
} }