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 => {
// this is just to trigger the worker message
post_message_to(
"test",
"ping".to_owned(),
"gimme_back_my_payload".to_owned(),
);
post_message_to("test", "ping", "gimme_back_my_payload");
},
_ => {},
}

View file

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

View file

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

View file

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