From 29a391f60eb3aeecba4682dfdb38cdcd2a61b2b2 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Sat, 17 Jun 2023 17:47:28 +0200 Subject: [PATCH] refactor(plugins): improve api (#2552) * refactor(plugins): improve shim API * style(fmt): rustfmt --- .../fixture-plugin-for-tests/src/main.rs | 6 +- default-plugins/strider/src/main.rs | 32 +++---- .../strider/src/search/search_state.rs | 8 +- zellij-tile/src/shim.rs | 86 ++++++++----------- 4 files changed, 56 insertions(+), 76 deletions(-) diff --git a/default-plugins/fixture-plugin-for-tests/src/main.rs b/default-plugins/fixture-plugin-for-tests/src/main.rs index 9ce45f8af7..20457f13fd 100644 --- a/default-plugins/fixture-plugin-for-tests/src/main.rs +++ b/default-plugins/fixture-plugin-for-tests/src/main.rs @@ -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"); }, _ => {}, } diff --git a/default-plugins/strider/src/main.rs b/default-plugins/strider/src/main.rs index 6cfaea81b5..24d443d10c 100644 --- a/default-plugins/strider/src/main.rs +++ b/default-plugins/strider/src/main.rs @@ -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(), ); }, _ => { diff --git a/default-plugins/strider/src/search/search_state.rs b/default-plugins/strider/src/search/search_state.rs index c8707cc9d8..865a98e553 100644 --- a/default-plugins/strider/src/search/search_state.rs +++ b/default-plugins/strider/src/search/search_state.rs @@ -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(); diff --git a/zellij-tile/src/shim.rs b/zellij-tile/src/shim.rs index cfa02eaf2a..671532158d 100644 --- a/zellij-tile/src/shim.rs +++ b/zellij-tile/src/shim.rs @@ -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>(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>(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>(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>(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>(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>(path: P) { + object_to_stdout(&path.as_ref()); unsafe { host_open_terminal_floating() }; } -pub fn open_command_pane(path: &Path, args: Vec) { - object_to_stdout(&(path, args)); +pub fn open_command_pane, A: AsRef>(path: P, args: Vec) { + object_to_stdout(&( + path.as_ref(), + args.iter().map(|a| a.as_ref()).collect::>(), + )); unsafe { host_open_command_pane() }; } -pub fn open_command_pane_floating(path: &Path, args: Vec) { - object_to_stdout(&(path, args)); +pub fn open_command_pane_floating, A: AsRef>(path: P, args: Vec) { + object_to_stdout(&( + path.as_ref(), + args.iter().map(|a| a.as_ref()).collect::>(), + )); 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)); - unsafe { host_rename_terminal_pane() }; - }, - Err(e) => { - eprintln!("Failed to rename terminal: {:?}", e) - }, - } +pub fn rename_terminal_pane>(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>(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>(tab_position: i32, new_name: S) { + object_to_stdout(&(tab_position, new_name.as_ref())); + unsafe { host_rename_tab() }; } // 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>(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>(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), }