Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(command-panes): optionally allow panes to be closed on exit #1869

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn main() {
cwd,
floating,
name,
close_on_exit,
})) = opts.command
{
let command_cli_action = CliAction::NewPane {
Expand All @@ -33,6 +34,7 @@ fn main() {
cwd,
floating,
name,
close_on_exit,
};
commands::send_action_to_session(command_cli_action, opts.session);
std::process::exit(0);
Expand Down
3 changes: 3 additions & 0 deletions zellij-server/src/unit/screen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,7 @@ pub fn send_cli_new_pane_action_with_default_parameters() {
cwd: None,
floating: false,
name: None,
close_on_exit: false,
};
send_cli_action_to_server(
&session_metadata,
Expand Down Expand Up @@ -1859,6 +1860,7 @@ pub fn send_cli_new_pane_action_with_split_direction() {
cwd: None,
floating: false,
name: None,
close_on_exit: false,
};
send_cli_action_to_server(
&session_metadata,
Expand Down Expand Up @@ -1897,6 +1899,7 @@ pub fn send_cli_new_pane_action_with_command_and_cwd() {
cwd: Some("/some/folder".into()),
floating: false,
name: None,
close_on_exit: false,
};
send_cli_action_to_server(
&session_metadata,
Expand Down
15 changes: 15 additions & 0 deletions zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ pub enum Sessions {
/// Name of the new pane
#[clap(short, long, value_parser)]
name: Option<String>,

/// Close the pane immediately when its command exits
#[clap(short, long, value_parser, default_value("false"), takes_value(false))]
close_on_exit: bool,
},
/// Edit file with default $EDITOR / $VISUAL
#[clap(visible_alias = "e")]
Expand Down Expand Up @@ -246,6 +250,17 @@ pub enum CliAction {
/// Name of the new pane
#[clap(short, long, value_parser)]
name: Option<String>,

/// Close the pane immediately when its command exits
#[clap(
short,
long,
value_parser,
default_value("false"),
takes_value(false),
requires("command")
)]
close_on_exit: bool,
},
/// Open the specified file in a new zellij pane with your default EDITOR
Edit {
Expand Down
4 changes: 3 additions & 1 deletion zellij-utils/src/input/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,19 @@ impl Action {
cwd,
floating,
name,
close_on_exit,
} => {
if !command.is_empty() {
let mut command = command.clone();
let (command, args) = (PathBuf::from(command.remove(0)), command);
let cwd = cwd.or_else(|| std::env::current_dir().ok());
let hold_on_close = !close_on_exit;
let run_command_action = RunCommandAction {
command,
args,
cwd,
direction,
hold_on_close: true,
hold_on_close,
};
if floating {
Ok(vec![Action::NewFloatingPane(
Expand Down
20 changes: 20 additions & 0 deletions zellij-utils/src/input/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ impl Run {
_ => {}, // plugins aren't yet supported
}
}
pub fn add_args(&mut self, args: Option<Vec<String>>) {
// overrides the args of a Run::Command if they are Some
// and not empty
if let Some(args) = args {
if let Run::Command(run_command) = self {
if !args.is_empty() {
run_command.args = args.clone();
}
}
}
}
pub fn add_close_on_exit(&mut self, close_on_exit: Option<bool>) {
// overrides the args of a Run::Command if they are Some
// and not empty
if let Some(close_on_exit) = close_on_exit {
if let Run::Command(run_command) = self {
run_command.hold_on_close = !close_on_exit;
}
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
Expand Down
75 changes: 75 additions & 0 deletions zellij-utils/src/input/unit/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ fn layout_with_command_panes_and_cwd_and_args() {
assert_eq!(layout, expected_layout);
}

#[test]
fn layout_with_command_panes_and_close_on_exit() {
let kdl_layout = r#"
layout {
pane command="htop" {
close_on_exit true
}
}
"#;
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap();
assert_snapshot!(format!("{:#?}", layout));
}

#[test]
fn layout_with_plugin_panes() {
let kdl_layout = r#"
Expand Down Expand Up @@ -1033,6 +1046,24 @@ fn args_override_args_in_template() {
assert_snapshot!(format!("{:#?}", layout));
}

#[test]
fn close_on_exit_overrides_close_on_exit_in_template() {
let kdl_layout = r#"
layout {
pane_template name="tail" {
command "tail"
close_on_exit false
}
tail
tail {
close_on_exit true
}
}
"#;
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap();
assert_snapshot!(format!("{:#?}", layout));
}

#[test]
fn args_added_to_args_in_template() {
let kdl_layout = r#"
Expand All @@ -1050,6 +1081,23 @@ fn args_added_to_args_in_template() {
assert_snapshot!(format!("{:#?}", layout));
}

#[test]
fn close_on_exit_added_to_close_on_exit_in_template() {
let kdl_layout = r#"
layout {
pane_template name="tail" {
command "tail"
}
tail
tail {
close_on_exit true
}
}
"#;
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap();
assert_snapshot!(format!("{:#?}", layout));
}

#[test]
fn cwd_override_cwd_in_template() {
let kdl_layout = r#"
Expand Down Expand Up @@ -1125,6 +1173,19 @@ fn error_on_bare_args_without_command() {
assert!(layout.is_err(), "error provided");
}

#[test]
fn error_on_bare_close_on_exit_without_command() {
let kdl_layout = r#"
layout {
pane {
close_on_exit true
}
}
"#;
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None);
assert!(layout.is_err(), "error provided");
}

#[test]
fn error_on_bare_args_in_template_without_command() {
let kdl_layout = r#"
Expand All @@ -1139,6 +1200,20 @@ fn error_on_bare_args_in_template_without_command() {
assert!(layout.is_err(), "error provided");
}

#[test]
fn error_on_bare_close_on_exit_in_template_without_command() {
let kdl_layout = r#"
layout {
pane_template name="my_template"
my_template {
close_on_exit true
}
}
"#;
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None);
assert!(layout.is_err(), "error provided");
}

#[test]
fn pane_template_command_with_cwd_overriden_by_its_consumers_command_cwd() {
let kdl_layout = r#"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
source: zellij-utils/src/input/./unit/layout_test.rs
assertion_line: 1098
expression: "format!(\"{:#?}\", layout)"
---
Layout {
tabs: [],
focused_tab_index: None,
template: Some(
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Command(
RunCommand {
command: "tail",
args: [],
cwd: None,
hold_on_close: true,
},
),
),
borderless: false,
focus: None,
external_children_index: None,
},
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Command(
RunCommand {
command: "tail",
args: [],
cwd: None,
hold_on_close: false,
},
),
),
borderless: false,
focus: None,
external_children_index: None,
},
],
split_size: None,
run: None,
borderless: false,
focus: None,
external_children_index: None,
},
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
source: zellij-utils/src/input/./unit/layout_test.rs
assertion_line: 1064
expression: "format!(\"{:#?}\", layout)"
---
Layout {
tabs: [],
focused_tab_index: None,
template: Some(
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Command(
RunCommand {
command: "tail",
args: [],
cwd: None,
hold_on_close: true,
},
),
),
borderless: false,
focus: None,
external_children_index: None,
},
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Command(
RunCommand {
command: "tail",
args: [],
cwd: None,
hold_on_close: false,
},
),
),
borderless: false,
focus: None,
external_children_index: None,
},
],
split_size: None,
run: None,
borderless: false,
focus: None,
external_children_index: None,
},
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
source: zellij-utils/src/input/./unit/layout_test.rs
assertion_line: 281
expression: "format!(\"{:#?}\", layout)"
---
Layout {
tabs: [],
focused_tab_index: None,
template: Some(
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [
PaneLayout {
children_split_direction: Horizontal,
name: None,
children: [],
split_size: None,
run: Some(
Command(
RunCommand {
command: "htop",
args: [],
cwd: None,
hold_on_close: false,
},
),
),
borderless: false,
focus: None,
external_children_index: None,
},
],
split_size: None,
run: None,
borderless: false,
focus: None,
external_children_index: None,
},
),
}
Loading