Skip to content

Commit

Permalink
fix(ux): LaunchPlugin and some cwd fixes (zellij-org#2916)
Browse files Browse the repository at this point in the history
* LaunchPlugin and some cwd fixes

* style(fmt): rustfmt

* fix e2e tests and some cleanups

* fmt
  • Loading branch information
imsnif authored Nov 8, 2023
1 parent d4657a2 commit ea5e6aa
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 17 deletions.
5 changes: 4 additions & 1 deletion zellij-server/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ pub(crate) fn plugin_thread_main(
client_id,
size,
cwd,
) => match wasm_bridge.load_plugin(&run, tab_index, size, cwd, Some(client_id)) {
) => match wasm_bridge.load_plugin(&run, tab_index, size, cwd.clone(), Some(client_id))
{
Ok(plugin_id) => {
drop(bus.senders.send_to_screen(ScreenInstruction::AddPlugin(
should_float,
Expand All @@ -194,6 +195,7 @@ pub(crate) fn plugin_thread_main(
tab_index,
plugin_id,
pane_id_to_replace,
cwd,
Some(client_id),
)));
},
Expand Down Expand Up @@ -231,6 +233,7 @@ pub(crate) fn plugin_thread_main(
plugin_id,
None,
None,
None,
)));
},
Err(e) => {
Expand Down
1 change: 1 addition & 0 deletions zellij-server/src/plugins/plugin_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ impl<'a> PluginLoader<'a> {
client_attributes: self.client_attributes.clone(),
default_shell: self.default_shell.clone(),
default_layout: self.default_layout.clone(),
plugin_cwd: self.zellij_cwd.clone(),
};

let subscriptions = Arc::new(Mutex::new(HashSet::new()));
Expand Down
1 change: 1 addition & 0 deletions zellij-server/src/plugins/plugin_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub struct PluginEnv {
pub client_attributes: ClientAttributes,
pub default_shell: Option<TerminalAction>,
pub default_layout: Box<Layout>,
pub plugin_cwd: PathBuf,
}

impl PluginEnv {
Expand Down
4 changes: 2 additions & 2 deletions zellij-server/src/plugins/unit/plugin_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5398,7 +5398,7 @@ pub fn run_command_plugin_command() {
)]));
background_jobs_thread.join().unwrap(); // this might take a while if the cache is cold
teardown();
let new_tab_event = received_background_jobs_instructions
let new_background_job = received_background_jobs_instructions
.lock()
.unwrap()
.iter()
Expand All @@ -5410,7 +5410,7 @@ pub fn run_command_plugin_command() {
}
})
.clone();
assert_snapshot!(format!("{:#?}", new_tab_event));
assert!(format!("{:#?}", new_background_job).contains("user_value_1"));
}

#[test]
Expand Down
40 changes: 31 additions & 9 deletions zellij-server/src/plugins/zellij_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,14 @@ fn open_file(env: &ForeignFunctionEnv, file_to_open: FileToOpen) {
let error_msg = || format!("failed to open file in plugin {}", env.plugin_env.name());
let floating = false;
let in_place = false;
let path = env.plugin_env.plugin_cwd.join(file_to_open.path);
let cwd = file_to_open
.cwd
.map(|cwd| env.plugin_env.plugin_cwd.join(cwd));
let action = Action::EditFile(
file_to_open.path,
path,
file_to_open.line_number,
file_to_open.cwd,
cwd,
None,
floating,
in_place,
Expand All @@ -389,10 +393,14 @@ fn open_file_floating(env: &ForeignFunctionEnv, file_to_open: FileToOpen) {
let error_msg = || format!("failed to open file in plugin {}", env.plugin_env.name());
let floating = true;
let in_place = false;
let path = env.plugin_env.plugin_cwd.join(file_to_open.path);
let cwd = file_to_open
.cwd
.map(|cwd| env.plugin_env.plugin_cwd.join(cwd));
let action = Action::EditFile(
file_to_open.path,
path,
file_to_open.line_number,
file_to_open.cwd,
cwd,
None,
floating,
in_place,
Expand All @@ -404,10 +412,14 @@ fn open_file_in_place(env: &ForeignFunctionEnv, file_to_open: FileToOpen) {
let error_msg = || format!("failed to open file in plugin {}", env.plugin_env.name());
let floating = false;
let in_place = true;
let path = env.plugin_env.plugin_cwd.join(file_to_open.path);
let cwd = file_to_open
.cwd
.map(|cwd| env.plugin_env.plugin_cwd.join(cwd));
let action = Action::EditFile(
file_to_open.path,
path,
file_to_open.line_number,
file_to_open.cwd,
cwd,
None,
floating,
in_place,
Expand All @@ -417,6 +429,7 @@ fn open_file_in_place(env: &ForeignFunctionEnv, file_to_open: FileToOpen) {

fn open_terminal(env: &ForeignFunctionEnv, cwd: PathBuf) {
let error_msg = || format!("failed to open file in plugin {}", env.plugin_env.name());
let cwd = env.plugin_env.plugin_cwd.join(cwd);
let mut default_shell = env
.plugin_env
.default_shell
Expand All @@ -433,6 +446,7 @@ fn open_terminal(env: &ForeignFunctionEnv, cwd: PathBuf) {

fn open_terminal_floating(env: &ForeignFunctionEnv, cwd: PathBuf) {
let error_msg = || format!("failed to open file in plugin {}", env.plugin_env.name());
let cwd = env.plugin_env.plugin_cwd.join(cwd);
let mut default_shell = env
.plugin_env
.default_shell
Expand All @@ -449,6 +463,7 @@ fn open_terminal_floating(env: &ForeignFunctionEnv, cwd: PathBuf) {

fn open_terminal_in_place(env: &ForeignFunctionEnv, cwd: PathBuf) {
let error_msg = || format!("failed to open file in plugin {}", env.plugin_env.name());
let cwd = env.plugin_env.plugin_cwd.join(cwd);
let mut default_shell = env
.plugin_env
.default_shell
Expand All @@ -466,7 +481,9 @@ fn open_terminal_in_place(env: &ForeignFunctionEnv, cwd: PathBuf) {
fn open_command_pane(env: &ForeignFunctionEnv, command_to_run: CommandToRun) {
let error_msg = || format!("failed to open command in plugin {}", env.plugin_env.name());
let command = command_to_run.path;
let cwd = command_to_run.cwd;
let cwd = command_to_run
.cwd
.map(|cwd| env.plugin_env.plugin_cwd.join(cwd));
let args = command_to_run.args;
let direction = None;
let hold_on_close = true;
Expand All @@ -487,7 +504,9 @@ fn open_command_pane(env: &ForeignFunctionEnv, command_to_run: CommandToRun) {
fn open_command_pane_floating(env: &ForeignFunctionEnv, command_to_run: CommandToRun) {
let error_msg = || format!("failed to open command in plugin {}", env.plugin_env.name());
let command = command_to_run.path;
let cwd = command_to_run.cwd;
let cwd = command_to_run
.cwd
.map(|cwd| env.plugin_env.plugin_cwd.join(cwd));
let args = command_to_run.args;
let direction = None;
let hold_on_close = true;
Expand All @@ -508,7 +527,9 @@ fn open_command_pane_floating(env: &ForeignFunctionEnv, command_to_run: CommandT
fn open_command_pane_in_place(env: &ForeignFunctionEnv, command_to_run: CommandToRun) {
let error_msg = || format!("failed to open command in plugin {}", env.plugin_env.name());
let command = command_to_run.path;
let cwd = command_to_run.cwd;
let cwd = command_to_run
.cwd
.map(|cwd| env.plugin_env.plugin_cwd.join(cwd));
let args = command_to_run.args;
let direction = None;
let hold_on_close = true;
Expand Down Expand Up @@ -621,6 +642,7 @@ fn run_command(
log::error!("Command cannot be empty");
} else {
let command = command_line.remove(0);
let cwd = env.plugin_env.plugin_cwd.join(cwd);
let _ = env
.plugin_env
.senders
Expand Down
11 changes: 11 additions & 0 deletions zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,17 @@ pub(crate) fn route_action(
))
.with_context(err_context)?;
},
Action::LaunchPlugin(run_plugin, should_float, should_open_in_place) => {
senders
.send_to_screen(ScreenInstruction::LaunchPlugin(
run_plugin,
should_float,
should_open_in_place,
pane_id,
client_id,
))
.with_context(err_context)?;
},
Action::CloseTerminalPane(terminal_pane_id) => {
senders
.send_to_screen(ScreenInstruction::ClosePane(
Expand Down
82 changes: 78 additions & 4 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,17 @@ pub enum ScreenInstruction {
usize, // tab index
u32, // plugin id
Option<PaneId>,
Option<PathBuf>, // cwd
Option<ClientId>,
),
UpdatePluginLoadingStage(u32, LoadingIndication), // u32 - plugin_id
StartPluginLoadingIndication(u32, LoadingIndication), // u32 - plugin_id
ProgressPluginLoadingOffset(u32), // u32 - plugin id
RequestStateUpdateForPlugins,
LaunchOrFocusPlugin(RunPlugin, bool, bool, bool, Option<PaneId>, ClientId), // bools are: should_float, move_to_focused_tab, should_open_in_place Option<PaneId> is the pane id to replace
SuppressPane(PaneId, ClientId), // bool is should_float
FocusPaneWithId(PaneId, bool, ClientId), // bool is should_float
LaunchPlugin(RunPlugin, bool, bool, Option<PaneId>, ClientId), // bools are: should_float, should_open_in_place Option<PaneId> is the pane id to replace
SuppressPane(PaneId, ClientId), // bool is should_float
FocusPaneWithId(PaneId, bool, ClientId), // bool is should_float
RenamePane(PaneId, Vec<u8>),
RenameTab(usize, Vec<u8>),
RequestPluginPermissions(
Expand Down Expand Up @@ -481,6 +483,7 @@ impl From<&ScreenInstruction> for ScreenContext {
ScreenContext::RequestStateUpdateForPlugins
},
ScreenInstruction::LaunchOrFocusPlugin(..) => ScreenContext::LaunchOrFocusPlugin,
ScreenInstruction::LaunchPlugin(..) => ScreenContext::LaunchPlugin,
ScreenInstruction::SuppressPane(..) => ScreenContext::SuppressPane,
ScreenInstruction::FocusPaneWithId(..) => ScreenContext::FocusPaneWithId,
ScreenInstruction::RenamePane(..) => ScreenContext::RenamePane,
Expand Down Expand Up @@ -3242,10 +3245,17 @@ pub(crate) fn screen_thread_main(
tab_index,
plugin_id,
pane_id_to_replace,
cwd,
client_id,
) => {
let pane_title =
pane_title.unwrap_or_else(|| run_plugin_location.location.to_string());
let pane_title = pane_title.unwrap_or_else(|| {
format!(
"({}) - {}",
cwd.map(|cwd| cwd.display().to_string())
.unwrap_or(".".to_owned()),
run_plugin_location.location
)
});
let run_plugin = Run::Plugin(run_plugin_location);

if should_be_in_place {
Expand Down Expand Up @@ -3399,6 +3409,70 @@ pub(crate) fn screen_thread_main(
}
},
},
ScreenInstruction::LaunchPlugin(
run_plugin,
should_float,
should_open_in_place,
pane_id_to_replace,
client_id,
) => match pane_id_to_replace {
Some(pane_id_to_replace) => match screen.active_tab_indices.values().next() {
Some(tab_index) => {
let size = Size::default();
screen
.bus
.senders
.send_to_pty(PtyInstruction::FillPluginCwd(
Some(should_float),
should_open_in_place,
None,
run_plugin,
*tab_index,
Some(pane_id_to_replace),
client_id,
size,
))?;
},
None => {
log::error!(
"Could not find an active tab - is there at least 1 connected user?"
);
},
},
None => {
let client_id = if screen.active_tab_indices.contains_key(&client_id) {
Some(client_id)
} else {
screen.get_first_client_id()
};
let client_id_and_focused_tab = client_id.and_then(|client_id| {
screen
.active_tab_indices
.get(&client_id)
.map(|tab_index| (*tab_index, client_id))
});
match client_id_and_focused_tab {
Some((tab_index, client_id)) => {
screen
.bus
.senders
.send_to_pty(PtyInstruction::FillPluginCwd(
Some(should_float),
should_open_in_place,
None,
run_plugin,
tab_index,
None,
client_id,
Size::default(),
))?;
},
None => {
log::error!("No connected clients found - cannot load or focus plugin")
},
}
},
},
ScreenInstruction::SuppressPane(pane_id, client_id) => {
let all_tabs = screen.get_tabs_mut();
for tab in all_tabs.values_mut() {
Expand Down
7 changes: 6 additions & 1 deletion zellij-utils/assets/prost/api.action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub struct Action {
pub name: i32,
#[prost(
oneof = "action::OptionalPayload",
tags = "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45"
tags = "2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46"
)]
pub optional_payload: ::core::option::Option<action::OptionalPayload>,
}
Expand Down Expand Up @@ -102,6 +102,8 @@ pub mod action {
RenameTabPayload(super::IdAndName),
#[prost(string, tag = "45")]
RenameSessionPayload(::prost::alloc::string::String),
#[prost(message, tag = "46")]
LaunchPluginPayload(super::LaunchOrFocusPluginPayload),
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down Expand Up @@ -403,6 +405,7 @@ pub enum ActionName {
BreakPaneRight = 78,
BreakPaneLeft = 79,
RenameSession = 80,
LaunchPlugin = 81,
}
impl ActionName {
/// String value of the enum field names used in the ProtoBuf definition.
Expand Down Expand Up @@ -492,6 +495,7 @@ impl ActionName {
ActionName::BreakPaneRight => "BreakPaneRight",
ActionName::BreakPaneLeft => "BreakPaneLeft",
ActionName::RenameSession => "RenameSession",
ActionName::LaunchPlugin => "LaunchPlugin",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand Down Expand Up @@ -578,6 +582,7 @@ impl ActionName {
"BreakPaneRight" => Some(Self::BreakPaneRight),
"BreakPaneLeft" => Some(Self::BreakPaneLeft),
"RenameSession" => Some(Self::RenameSession),
"LaunchPlugin" => Some(Self::LaunchPlugin),
_ => None,
}
}
Expand Down
9 changes: 9 additions & 0 deletions zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ pub enum CliAction {
#[clap(short, long, value_parser)]
configuration: Option<PluginUserConfiguration>,
},
LaunchPlugin {
#[clap(short, long, value_parser)]
floating: bool,
#[clap(short, long, value_parser)]
in_place: bool,
url: Url,
#[clap(short, long, value_parser)]
configuration: Option<PluginUserConfiguration>,
},
RenameSession {
name: String,
},
Expand Down
1 change: 1 addition & 0 deletions zellij-utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ pub enum ScreenContext {
StartPluginLoadingIndication,
RequestStateUpdateForPlugins,
LaunchOrFocusPlugin,
LaunchPlugin,
SuppressPane,
FocusPaneWithId,
RenamePane,
Expand Down
Loading

0 comments on commit ea5e6aa

Please sign in to comment.