Skip to content

Commit

Permalink
feat(plugins): make plugins configurable (#2646)
Browse files Browse the repository at this point in the history
* work

* make every plugin entry point configurable

* make integration tests pass

* make e2e tests pass

* add test for plugin configuration

* add test snapshot

* add plugin config parsing test

* cleanups

* style(fmt): rustfmt

* style(comment): remove commented code
  • Loading branch information
imsnif authored Jul 25, 2023
1 parent 6cf795a commit c95d0e7
Show file tree
Hide file tree
Showing 31 changed files with 605 additions and 98 deletions.
3 changes: 2 additions & 1 deletion default-plugins/compact-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod line;
mod tab;

use std::cmp::{max, min};
use std::collections::BTreeMap;
use std::convert::TryInto;

use tab::get_tab_to_focus;
Expand Down Expand Up @@ -30,7 +31,7 @@ static ARROW_SEPARATOR: &str = "";
register_plugin!(State);

impl ZellijPlugin for State {
fn load(&mut self) {
fn load(&mut self, configuration: BTreeMap<String, String>) {
set_selectable(false);
subscribe(&[
EventType::TabUpdate,
Expand Down
8 changes: 7 additions & 1 deletion default-plugins/fixture-plugin-for-tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use zellij_tile::prelude::*;

// This is a fixture plugin used only for tests in Zellij
Expand All @@ -9,6 +10,7 @@ use zellij_tile::prelude::*;
struct State {
received_events: Vec<Event>,
received_payload: Option<String>,
configuration: BTreeMap<String, String>,
}

#[derive(Default, Serialize, Deserialize)]
Expand All @@ -35,7 +37,8 @@ register_plugin!(State);
register_worker!(TestWorker, test_worker, TEST_WORKER);

impl ZellijPlugin for State {
fn load(&mut self) {
fn load(&mut self, configuration: BTreeMap<String, String>) {
self.configuration = configuration;
subscribe(&[
EventType::InputReceived,
EventType::Key,
Expand Down Expand Up @@ -210,6 +213,9 @@ impl ZellijPlugin for State {
Key::Ctrl('x') => {
rename_tab(1, "new tab name");
},
Key::Ctrl('z') => {
go_to_tab_name(&format!("{:?}", self.configuration));
},
_ => {},
},
Event::CustomMessage(message, payload) => {
Expand Down
8 changes: 6 additions & 2 deletions default-plugins/status-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ansi_term::{
Style,
};

use std::collections::BTreeMap;
use std::fmt::{Display, Error, Formatter};
use zellij_tile::prelude::actions::Action;
use zellij_tile::prelude::*;
Expand Down Expand Up @@ -196,7 +197,7 @@ fn color_elements(palette: Palette, different_color_alternates: bool) -> Colored
}

impl ZellijPlugin for State {
fn load(&mut self) {
fn load(&mut self, configuration: BTreeMap<String, String>) {
// TODO: Should be able to choose whether to use the cache through config.
self.tip_name = get_cached_tip_name();
set_selectable(false);
Expand All @@ -207,7 +208,10 @@ impl ZellijPlugin for State {
EventType::InputReceived,
EventType::SystemClipboardFailure,
]);
self.supermode = false; // TODO: from config
self.supermode = configuration
.get("supermode")
.and_then(|s| s.trim().parse().ok())
.unwrap_or(false);
self.standby_mode = InputMode::Pane;
if self.supermode {
switch_to_input_mode(&InputMode::Locked); // supermode should start locked (TODO: only
Expand Down
3 changes: 2 additions & 1 deletion default-plugins/strider/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use search::{FileContentsWorker, FileNameWorker, MessageToSearch, ResultsOfSearc
use serde::{Deserialize, Serialize};
use serde_json;
use state::{refresh_directory, FsEntry, State};
use std::collections::BTreeMap;
use std::{cmp::min, time::Instant};
use zellij_tile::prelude::*;

Expand All @@ -18,7 +19,7 @@ register_worker!(
);

impl ZellijPlugin for State {
fn load(&mut self) {
fn load(&mut self, configuration: BTreeMap<String, String>) {
refresh_directory(self);
self.search_state.loading = true;
subscribe(&[
Expand Down
3 changes: 2 additions & 1 deletion default-plugins/tab-bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod line;
mod tab;

use std::cmp::{max, min};
use std::collections::BTreeMap;
use std::convert::TryInto;

use tab::get_tab_to_focus;
Expand Down Expand Up @@ -30,7 +31,7 @@ static ARROW_SEPARATOR: &str = "";
register_plugin!(State);

impl ZellijPlugin for State {
fn load(&mut self) {
fn load(&mut self, configuration: BTreeMap<String, String>) {
set_selectable(false);
subscribe(&[
EventType::TabUpdate,
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn main() {
name,
close_on_exit,
start_suspended,
configuration: None,
};
commands::send_action_to_session(command_cli_action, opts.session, config);
std::process::exit(0);
Expand Down
9 changes: 8 additions & 1 deletion zellij-server/src/plugins/plugin_loader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::plugins::plugin_map::{PluginEnv, PluginMap, RunningPlugin, Subscriptions};
use crate::plugins::plugin_worker::{plugin_worker, RunningWorker};
use crate::plugins::zellij_exports::{wasi_read_string, zellij_exports};
use crate::plugins::zellij_exports::{wasi_read_string, wasi_write_object, zellij_exports};
use crate::plugins::PluginId;
use highway::{HighwayHash, PortableHash};
use log::info;
Expand Down Expand Up @@ -723,7 +723,14 @@ impl<'a> PluginLoader<'a> {
}
}
start_function.call(&[]).with_context(err_context)?;

wasi_write_object(
&plugin_env.wasi_env,
&self.plugin.userspace_configuration.inner(),
)
.with_context(err_context)?;
load_function.call(&[]).with_context(err_context)?;

display_loading_stage!(
indicate_starting_plugin_success,
self.loading_indication,
Expand Down
Loading

0 comments on commit c95d0e7

Please sign in to comment.