Skip to content

Commit

Permalink
feat(plugins): allow opening command panes in the background (hidden) (
Browse files Browse the repository at this point in the history
…#3530)

* start background pane

* open command and edit panes in the background

* some cleanups

* style(fmt): rustfmt

* more cleanups
  • Loading branch information
imsnif authored Jul 29, 2024
1 parent a6fe5ff commit 3554633
Show file tree
Hide file tree
Showing 53 changed files with 3,107 additions and 646 deletions.
42 changes: 33 additions & 9 deletions default-plugins/fixture-plugin-for-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,13 @@ impl ZellijPlugin for State {
start_or_reload_plugin(plugin_url)
},
BareKey::Char('g') if key.has_modifiers(&[KeyModifier::Ctrl]) => {
open_file(FileToOpen {
path: std::path::PathBuf::from("/path/to/my/file.rs"),
..Default::default()
});
open_file(
FileToOpen {
path: std::path::PathBuf::from("/path/to/my/file.rs"),
..Default::default()
},
BTreeMap::new(),
);
},
BareKey::Char('h') if key.has_modifiers(&[KeyModifier::Ctrl]) => {
open_file_floating(
Expand All @@ -178,14 +181,18 @@ impl ZellijPlugin for State {
..Default::default()
},
None,
BTreeMap::new(),
);
},
BareKey::Char('i') if key.has_modifiers(&[KeyModifier::Ctrl]) => {
open_file(FileToOpen {
path: std::path::PathBuf::from("/path/to/my/file.rs"),
line_number: Some(42),
..Default::default()
});
open_file(
FileToOpen {
path: std::path::PathBuf::from("/path/to/my/file.rs"),
line_number: Some(42),
..Default::default()
},
BTreeMap::new(),
);
},
BareKey::Char('j') if key.has_modifiers(&[KeyModifier::Ctrl]) => {
open_file_floating(
Expand All @@ -195,6 +202,7 @@ impl ZellijPlugin for State {
..Default::default()
},
None,
BTreeMap::new(),
);
},
BareKey::Char('k') if key.has_modifiers(&[KeyModifier::Ctrl]) => {
Expand Down Expand Up @@ -335,6 +343,22 @@ impl ZellijPlugin for State {
.to_owned(),
);
},
BareKey::Char('a') if key.has_modifiers(&[KeyModifier::Alt]) => {
hide_pane_with_id(PaneId::Terminal(1));
},
BareKey::Char('b') if key.has_modifiers(&[KeyModifier::Alt]) => {
show_pane_with_id(PaneId::Terminal(1), true);
},
BareKey::Char('c') if key.has_modifiers(&[KeyModifier::Alt]) => {
open_command_pane_background(
CommandToRun {
path: std::path::PathBuf::from("/path/to/my/file.rs"),
args: vec!["arg1".to_owned(), "arg2".to_owned()],
..Default::default()
},
BTreeMap::new(),
);
},
_ => {},
},
Event::CustomMessage(message, payload) => {
Expand Down
8 changes: 4 additions & 4 deletions default-plugins/status-bar/src/one_line_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ fn secondary_keybinds(help: &ModeInfo, tab_info: Option<&TabInfo>, max_len: usiz
let mut secondary_info = LinePart::default();
let binds = &help.get_mode_keybinds();
// New Pane
let new_pane_action_key = action_key(binds, &[Action::NewPane(None, None)]);
let new_pane_action_key = action_key(binds, &[Action::NewPane(None, None, false)]);
let mut new_pane_key_to_display = new_pane_action_key
.iter()
.find(|k| k.is_key_with_alt_modifier(BareKey::Char('n')))
Expand Down Expand Up @@ -1163,7 +1163,7 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
}

if mi.mode == IM::Pane { vec![
(s("New"), s("New"), single_action_key(&km, &[A::NewPane(None, None), TO_NORMAL])),
(s("New"), s("New"), single_action_key(&km, &[A::NewPane(None, None, false), TO_NORMAL])),
(s("Change Focus"), s("Move"),
action_key_group(&km, &[&[A::MoveFocus(Dir::Left)], &[A::MoveFocus(Dir::Down)],
&[A::MoveFocus(Dir::Up)], &[A::MoveFocus(Dir::Right)]])),
Expand Down Expand Up @@ -1271,8 +1271,8 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
(s("Move focus"), s("Move"), action_key_group(&km, &[
&[A::MoveFocus(Dir::Left)], &[A::MoveFocus(Dir::Down)],
&[A::MoveFocus(Dir::Up)], &[A::MoveFocus(Dir::Right)]])),
(s("Split down"), s("Down"), action_key(&km, &[A::NewPane(Some(Dir::Down), None), TO_NORMAL])),
(s("Split right"), s("Right"), action_key(&km, &[A::NewPane(Some(Dir::Right), None), TO_NORMAL])),
(s("Split down"), s("Down"), action_key(&km, &[A::NewPane(Some(Dir::Down), None, false), TO_NORMAL])),
(s("Split right"), s("Right"), action_key(&km, &[A::NewPane(Some(Dir::Right), None, false), TO_NORMAL])),
(s("Fullscreen"), s("Fullscreen"), action_key(&km, &[A::ToggleFocusFullscreen, TO_NORMAL])),
(s("New tab"), s("New"), action_key(&km, &[A::NewTab(None, vec![], None, None, None), TO_NORMAL])),
(s("Rename tab"), s("Rename"),
Expand Down
12 changes: 6 additions & 6 deletions default-plugins/status-bar/src/second_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
}

if mi.mode == IM::Pane { vec![
(s("New"), s("New"), action_key(&km, &[A::NewPane(None, None), TO_NORMAL])),
(s("New"), s("New"), action_key(&km, &[A::NewPane(None, None, false), TO_NORMAL])),
(s("Change Focus"), s("Move"),
action_key_group(&km, &[&[A::MoveFocus(Dir::Left)], &[A::MoveFocus(Dir::Down)],
&[A::MoveFocus(Dir::Up)], &[A::MoveFocus(Dir::Right)]])),
Expand Down Expand Up @@ -256,8 +256,8 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
(s("Move focus"), s("Move"), action_key_group(&km, &[
&[A::MoveFocus(Dir::Left)], &[A::MoveFocus(Dir::Down)],
&[A::MoveFocus(Dir::Up)], &[A::MoveFocus(Dir::Right)]])),
(s("Split down"), s("Down"), action_key(&km, &[A::NewPane(Some(Dir::Down), None), TO_NORMAL])),
(s("Split right"), s("Right"), action_key(&km, &[A::NewPane(Some(Dir::Right), None), TO_NORMAL])),
(s("Split down"), s("Down"), action_key(&km, &[A::NewPane(Some(Dir::Down), None, false), TO_NORMAL])),
(s("Split right"), s("Right"), action_key(&km, &[A::NewPane(Some(Dir::Right), None, false), TO_NORMAL])),
(s("Fullscreen"), s("Fullscreen"), action_key(&km, &[A::ToggleFocusFullscreen, TO_NORMAL])),
(s("New tab"), s("New"), action_key(&km, &[A::NewTab(None, vec![], None, None, None), TO_NORMAL])),
(s("Rename tab"), s("Rename"),
Expand Down Expand Up @@ -713,7 +713,7 @@ mod tests {
),
(
KeyWithModifier::new(BareKey::Char('n')),
vec![Action::NewPane(None, None), TO_NORMAL],
vec![Action::NewPane(None, None, false), TO_NORMAL],
),
(
KeyWithModifier::new(BareKey::Char('x')),
Expand Down Expand Up @@ -763,7 +763,7 @@ mod tests {
),
(
KeyWithModifier::new(BareKey::Char('n')),
vec![Action::NewPane(None, None), TO_NORMAL],
vec![Action::NewPane(None, None, false), TO_NORMAL],
),
(
KeyWithModifier::new(BareKey::Char('x')),
Expand Down Expand Up @@ -809,7 +809,7 @@ mod tests {
),
(
KeyWithModifier::new(BareKey::Backspace),
vec![Action::NewPane(None, None), TO_NORMAL],
vec![Action::NewPane(None, None, false), TO_NORMAL],
),
(
KeyWithModifier::new(BareKey::Esc),
Expand Down
2 changes: 1 addition & 1 deletion default-plugins/status-bar/src/tip/data/quicknav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct Keygroups<'a> {

fn add_keybinds(help: &ModeInfo) -> Keygroups {
let normal_keymap = help.get_mode_keybinds();
let new_pane_keys = action_key(&normal_keymap, &[Action::NewPane(None, None)]);
let new_pane_keys = action_key(&normal_keymap, &[Action::NewPane(None, None, false)]);
let new_pane = if new_pane_keys.is_empty() {
vec![Style::new().bold().paint("UNBOUND")]
} else {
Expand Down
3 changes: 2 additions & 1 deletion default-plugins/strider/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ impl State {
if let Some(parent_folder) = self.file_list_view.path.parent() {
open_file(
FileToOpen::new(&self.file_list_view.path).with_cwd(parent_folder.into()),
BTreeMap::new(),
);
} else {
open_file(FileToOpen::new(&self.file_list_view.path));
open_file(FileToOpen::new(&self.file_list_view.path), BTreeMap::new());
}
}
if self.close_on_selection {
Expand Down
15 changes: 8 additions & 7 deletions zellij-server/src/os_input_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ fn spawn_terminal(
// secondary fd
let mut failover_cmd_args = None;
let cmd = match terminal_action {
TerminalAction::OpenFile(mut file_to_open, line_number, cwd) => {
if file_to_open.is_relative() {
if let Some(cwd) = cwd.as_ref() {
file_to_open = cwd.join(file_to_open);
TerminalAction::OpenFile(mut payload) => {
if payload.path.is_relative() {
if let Some(cwd) = payload.cwd.as_ref() {
payload.path = cwd.join(payload.path);
}
}
let mut command = default_editor.unwrap_or_else(|| {
Expand All @@ -306,11 +306,12 @@ fn spawn_terminal(
if !command.is_dir() {
separate_command_arguments(&mut command, &mut args);
}
let file_to_open = file_to_open
let file_to_open = payload
.path
.into_os_string()
.into_string()
.expect("Not valid Utf8 Encoding");
if let Some(line_number) = line_number {
if let Some(line_number) = payload.line_number {
if command.ends_with("vim")
|| command.ends_with("nvim")
|| command.ends_with("emacs")
Expand All @@ -334,7 +335,7 @@ fn spawn_terminal(
RunCommand {
command,
args,
cwd,
cwd: payload.cwd,
hold_on_close: false,
hold_on_start: false,
..Default::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ impl<'a> FloatingPaneGrid<'a> {
}
}

fn half_size_middle_geom(space: &Viewport, offset: usize) -> PaneGeom {
pub fn half_size_middle_geom(space: &Viewport, offset: usize) -> PaneGeom {
let mut geom = PaneGeom {
x: space.x + (space.cols as f64 / 4.0).round() as usize + offset,
y: space.y + (space.rows as f64 / 4.0).round() as usize + offset,
Expand Down
2 changes: 1 addition & 1 deletion zellij-server/src/panes/floating_panes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod floating_pane_grid;
pub mod floating_pane_grid;
use zellij_utils::{
data::{Direction, PaneInfo, ResizeStrategy},
position::Position,
Expand Down
2 changes: 1 addition & 1 deletion zellij-server/src/panes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod sixel;
pub mod terminal_character;

mod active_panes;
mod floating_panes;
pub mod floating_panes;
mod plugin_pane;
mod search;
mod terminal_pane;
Expand Down
9 changes: 9 additions & 0 deletions zellij-server/src/panes/terminal_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ impl From<ZellijUtilsPaneId> for PaneId {
}
}

impl Into<ZellijUtilsPaneId> for PaneId {
fn into(self) -> ZellijUtilsPaneId {
match self {
PaneId::Terminal(id) => ZellijUtilsPaneId::Terminal(id),
PaneId::Plugin(id) => ZellijUtilsPaneId::Plugin(id),
}
}
}

type IsFirstRun = bool;

// FIXME: This should hold an os_api handle so that terminal panes can set their own size via FD in
Expand Down
14 changes: 14 additions & 0 deletions zellij-server/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub(crate) fn plugin_thread_main(
run_plugin_or_alias.populate_run_plugin_if_needed(&plugin_aliases);
let cwd = run_plugin_or_alias.get_initial_cwd().or(cwd);
let run_plugin = run_plugin_or_alias.get_run_plugin();
let start_suppressed = false;
match wasm_bridge.load_plugin(
&run_plugin,
Some(tab_index),
Expand All @@ -268,6 +269,7 @@ pub(crate) fn plugin_thread_main(
plugin_id,
pane_id_to_replace,
cwd,
start_suppressed,
Some(client_id),
)));
},
Expand Down Expand Up @@ -307,6 +309,7 @@ pub(crate) fn plugin_thread_main(
// we intentionally do not provide the client_id here because it belongs to
// the cli who spawned the command and is not an existing client_id
let skip_cache = true; // when reloading we always skip cache
let start_suppressed = false;
match wasm_bridge.load_plugin(
&Some(run_plugin),
Some(tab_index),
Expand All @@ -328,6 +331,7 @@ pub(crate) fn plugin_thread_main(
plugin_id,
None,
None,
start_suppressed,
None,
),
));
Expand Down Expand Up @@ -365,6 +369,16 @@ pub(crate) fn plugin_thread_main(
tab_index,
client_id,
) => {
// prefer connected clients so as to avoid opening plugins in the background for
// CLI clients unless no-one else is connected
let client_id = if wasm_bridge.client_is_connected(&client_id) {
client_id
} else if let Some(first_client_id) = wasm_bridge.get_first_client_id() {
first_client_id
} else {
client_id
};

let mut plugin_ids: HashMap<RunPluginOrAlias, Vec<PluginId>> = HashMap::new();
tab_layout = tab_layout.or_else(|| Some(layout.new_tab().0));
tab_layout
Expand Down
Loading

0 comments on commit 3554633

Please sign in to comment.