diff options
author | Aram Drevekenin <aram@poor.dev> | 2022-10-20 15:23:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-20 15:23:20 +0200 |
commit | e59b09a1b6701b529c1f444596206969a4c3e90a (patch) | |
tree | 6affe19624286708ee2e2961f266aaab5c7fe612 /zellij-utils | |
parent | ebf61f6ff21238fe570bd38532ab7b1b8d7e22bb (diff) |
feat(layouts): allow defining a tab cwd (#1828)
Diffstat (limited to 'zellij-utils')
9 files changed, 709 insertions, 56 deletions
diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 8e317a8e0..b041412e2 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -111,6 +111,25 @@ impl Run { (None, None) => None, } } + pub fn add_cwd(&mut self, cwd: &PathBuf) { + match self { + Run::Command(run_command) => match run_command.cwd.as_mut() { + Some(run_cwd) => { + *run_cwd = cwd.join(&run_cwd); + }, + None => { + run_command.cwd = Some(cwd.clone()); + }, + }, + Run::EditFile(path_to_file, _line_number) => { + *path_to_file = cwd.join(&path_to_file); + }, + Run::Cwd(path) => { + *path = cwd.join(&path); + }, + _ => {}, // plugins aren't yet supported + } + } } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] @@ -233,6 +252,17 @@ impl PaneLayout { default_layout.children = vec![PaneLayout::default()]; default_layout } + pub fn add_cwd_to_layout(&mut self, cwd: &PathBuf) { + match self.run.as_mut() { + Some(run) => run.add_cwd(cwd), + None => { + self.run = Some(Run::Cwd(cwd.clone())); + }, + } + for child in self.children.iter_mut() { + child.add_cwd_to_layout(cwd); + } + } } #[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 6155024b2..1addb9382 100644 --- a/zellij-utils/src/input/unit/layout_test.rs +++ b/zellij-utils/src/input/unit/layout_test.rs @@ -1379,3 +1379,104 @@ fn global_cwd_passed_from_layout_constructor_overrides_global_cwd_in_layout_file .unwrap(); assert_snapshot!(format!("{:#?}", layout)); } + +#[test] +fn global_cwd_with_tab_cwd_given_to_panes_without_cwd() { + let kdl_layout = r#" + layout { + cwd "/tmp" + tab cwd="./foo" { + pane + pane command="tail" + } + // both should have the /tmp/foo cwd + } + "#; + let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap(); + assert_snapshot!(format!("{:#?}", layout)); +} + +#[test] +fn tab_cwd_given_to_panes_without_cwd() { + let kdl_layout = r#" + layout { + tab cwd="/tmp" { + pane + pane command="tail" + } + // both should have the /tmp cwd + } + "#; + let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap(); + assert_snapshot!(format!("{:#?}", layout)); +} + +#[test] +fn tab_cwd_prepended_to_panes_with_cwd() { + let kdl_layout = r#" + layout { + tab cwd="/tmp" { + pane cwd="./foo" + pane command="tail" cwd="./foo" + } + // both should have the /tmp/foo cwd + } + "#; + let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap(); + assert_snapshot!(format!("{:#?}", layout)); +} + +#[test] +fn global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd() { + let kdl_layout = r#" + layout { + cwd "/tmp" + tab cwd="./foo" { + pane // should have /tmp/foo + pane command="tail" cwd="./bar" // should have /tmp/foo/bar + } + } + "#; + let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap(); + assert_snapshot!(format!("{:#?}", layout)); +} + +#[test] +fn global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_pane_templates() { + let kdl_layout = r#" + layout { + cwd "/tmp" + pane_template name="my_pane_template" { + pane // should have /tmp/foo + pane command="tail" cwd="./bar" // should have /tmp/foo/bar + children + } + tab cwd="./foo" { + my_pane_template { + pane // should have /tmp/foo + } + } + } + "#; + let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap(); + assert_snapshot!(format!("{:#?}", layout)); +} + +#[test] +fn global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_tab_templates() { + let kdl_layout = r#" + layout { + cwd "/tmp" + tab_template name="my_tab_template" { + pane // should have /tmp/foo + pane command="tail" cwd="./bar" // should have /tmp/foo/bar + children + } + my_tab_template cwd="./foo" { + pane // should have /tmp/foo + } + } + "#; + let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None).unwrap(); + assert_snapshot!(format!("{:#?}", layout)); +} diff --git a/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd.snap new file mode 100644 index 000000000..a83674d2d --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd.snap @@ -0,0 +1,75 @@ +--- +source: zellij-utils/src/input/./unit/layout_test.rs +assertion_line: 1441 +expression: "format!(\"{:#?}\", layout)" +--- +Layout { + tabs: [ + ( + None, + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + 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: Some( + "/tmp/./foo/./bar", + ), + hold_on_close: true, + }, + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ), + ], + focused_tab_index: None, + template: Some( + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + 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__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_pane_templates.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_pane_templates.snap new file mode 100644 index 000000000..8381dcd1d --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_pane_templates.snap @@ -0,0 +1,119 @@ +--- +source: zellij-utils/src/input/./unit/layout_test.rs +assertion_line: 1462 +expression: "format!(\"{:#?}\", layout)" +--- +Layout { + tabs: [ + ( + None, + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + 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: Some( + "/tmp/./foo/./bar", + ), + hold_on_close: true, + }, + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ), + ], + focused_tab_index: None, + template: Some( + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + 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__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_tab_templates.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_tab_templates.snap new file mode 100644 index 000000000..65f287042 --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_and_tab_cwd_prepended_to_panes_with_and_without_cwd_in_tab_templates.snap @@ -0,0 +1,104 @@ +--- +source: zellij-utils/src/input/./unit/layout_test.rs +assertion_line: 1481 +expression: "format!(\"{:#?}\", layout)" +--- +Layout { + tabs: [ + ( + None, + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + 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: Some( + "/tmp/./foo/./bar", + ), + hold_on_close: true, + }, + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ), + ], + focused_tab_index: None, + template: Some( + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + 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__global_cwd_with_tab_cwd_given_to_panes_without_cwd.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_with_tab_cwd_given_to_panes_without_cwd.snap new file mode 100644 index 000000000..4bc2f412f --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__global_cwd_with_tab_cwd_given_to_panes_without_cwd.snap @@ -0,0 +1,75 @@ +--- +source: zellij-utils/src/input/./unit/layout_test.rs +assertion_line: 1396 +expression: "format!(\"{:#?}\", layout)" +--- +Layout { + tabs: [ + ( + None, + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + 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: Some( + "/tmp/./foo", + ), + hold_on_close: true, + }, + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp/./foo", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ), + ], + focused_tab_index: None, + template: Some( + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + 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__tab_cwd_given_to_panes_without_cwd.snap b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__tab_cwd_given_to_panes_without_cwd.snap new file mode 100644 index 000000000..8f05eda17 --- /dev/null +++ b/zellij-utils/src/input/unit/snapshots/zellij_utils__input__layout__layout_test__tab_cwd_given_to_panes_without_cwd.snap @@ -0,0 +1,75 @@ +--- +source: zellij-utils/src/input/./unit/layout_test.rs +assertion_line: 1411 +expression: "format!(\"{:#?}\", layout)" +--- +Layout { + tabs: [ + ( + None, + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [ + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: Some( + Cwd( + "/tmp", + ), + ), + 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: Some( + "/tmp", + ), + hold_on_close: true, + }, + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ], + split_size: None, + run: Some( + Cwd( + "/tmp", + ), + ), + borderless: false, + focus: None, + external_children_index: None, + }, + ), + ], + focused_tab_index: None, + template: Some( + PaneLayout { + children_split_direction: Horizontal, + name: None, + children: [], + split_size: None, + run: None, + borderless: false, + focus: None, |