Skip to content

Commit

Permalink
refactor(plugins): improve api (#2552)
Browse files Browse the repository at this point in the history
* refactor(plugins): improve shim API

* style(fmt): rustfmt
  • Loading branch information
imsnif committed Jun 17, 2023
1 parent 10b7f3a commit 29a391f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 76 deletions.
6 changes: 1 addition & 5 deletions default-plugins/fixture-plugin-for-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
},
_ => {},
}
Expand Down
32 changes: 16 additions & 16 deletions default-plugins/strider/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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(),
);
},
_ => {
Expand Down
8 changes: 4 additions & 4 deletions default-plugins/strider/src/search/search_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
86 changes: 35 additions & 51 deletions zellij-tile/src/shim.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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() };
}

Expand Down Expand Up @@ -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));
unsafe { host_rename_terminal_pane() };
},
Err(e) => {
eprintln!("Failed to rename terminal: {:?}", e)
},
}
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() };
}

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));
unsafe { host_rename_plugin_pane() };
},
Err(e) => {
eprintln!("Failed to rename plugin: {:?}", e)
},
}
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() };
}

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));
unsafe { host_rename_tab() };
},
Err(e) => {
eprintln!("Failed to rename tab: {:?}", e)
},
}
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() };
}

// Internal Functions
Expand All @@ -344,17 +328,17 @@ 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),
}
unsafe { host_post_message_to() };
}

#[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),
}
Expand Down

0 comments on commit 29a391f

Please sign in to comment.