diff options
author | Aram Drevekenin <aram@poor.dev> | 2022-10-28 13:03:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-28 13:03:37 +0200 |
commit | c97b972383d50ae6db750d4d7f2441232e41ba4c (patch) | |
tree | 448c6f3d626e7c405ec1e54c2be2ccf5a6bd2be7 /zellij-utils/src/input | |
parent | eed9541a74879e1ec683beda13ccfda7e63bfa88 (diff) |
feat(command-panes): optionally allow panes to be closed on exit (#1869)
* feat(cli): allow option to close command pane on exit
* feat(layouts): allow option to close command panes on exit
* style(fmt): rustfmt
Diffstat (limited to 'zellij-utils/src/input')
6 files changed, 259 insertions, 1 deletions
diff --git a/zellij-utils/src/input/actions.rs b/zellij-utils/src/input/actions.rs index a9ab37053..51ead67b3 100644 --- a/zellij-utils/src/input/actions.rs +++ b/zellij-utils/src/input/actions.rs @@ -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( diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 8d0dc104d..fa50237a9 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -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)] diff --git a/zellij-utils/src/input/unit/layout_test.rs b/zellij-utils/src/input/unit/layout_test.rs index 1addb9382..9614eef46 100644 --- a/zellij-utils/src/input/unit/layout_test.rs +++ b/zellij-utils/src/input/unit/layout_test.rs @@ -269,6 +269,19 @@ fn layout_with_command_panes_and_cwd_and_args() { } #[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#" layout { @@ -1034,6 +1047,24 @@ fn args_override_args_in_template() { } #[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#" layout { @@ -1051,6 +1082,23 @@ fn args_added_to_args_in_template() { } #[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#" layout { @@ -1126,6 +1174,19 @@ fn error_on_bare_args_without_command() { } #[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#" layout { @@ -1140,6 +1201,20 @@ fn error_on_bare_args_in_template_without_command() { } #[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#" layout { diff --git a/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__close_on_exit_added_to_close_on_exit_in_template.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__close_on_exit_added_to_close_on_exit_in_template.snap new file mode 100644 index 000000000..da83cc62c --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__close_on_exit_added_to_close_on_exit_in_template.snap @@ -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, + }, + ), +} diff --git a/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__close_on_exit_overrides_close_on_exit_in_template.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__close_on_exit_overrides_close_on_exit_in_template.snap new file mode 100644 index 000000000..3cb80cf74 --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__close_on_exit_overrides_close_on_exit_in_template.snap @@ -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, + }, + ), +} diff --git a/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__layout_with_command_panes_and_close_on_exit.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__layout_with_command_panes_and_close_on_exit.snap new file mode 100644 index 000000000..69e3d7f1f --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__layout_with_command_panes_and_close_on_exit.snap @@ -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, + }, + ), +} |