diff options
-rw-r--r-- | src/commands.rs | 83 | ||||
-rw-r--r-- | zellij-utils/src/input/layout.rs | 17 | ||||
-rw-r--r-- | zellij-utils/src/input/unit/fixtures/layouts/session-name-to-layout.yaml | 3 | ||||
-rw-r--r-- | zellij-utils/src/input/unit/layout_test.rs | 22 |
4 files changed, 108 insertions, 17 deletions
diff --git a/src/commands.rs b/src/commands.rs index 23105ec01..0879e5361 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -211,24 +211,73 @@ pub(crate) fn start_client(opts: CliArgs) { attach_layout, ); } else { - let session_name = opts - .session - .clone() - .unwrap_or_else(|| names::Generator::default().next().unwrap()); - assert_session_ne(&session_name); + let start_client_plan = |session_name: std::string::String| { + assert_session_ne(&session_name); - // Determine and initialize the data directory - let data_dir = opts.data_dir.clone().unwrap_or_else(get_default_data_dir); - #[cfg(not(disable_automatic_asset_installation))] - populate_data_dir(&data_dir); + let data_dir = opts.data_dir.clone().unwrap_or_else(get_default_data_dir); + #[cfg(not(disable_automatic_asset_installation))] + populate_data_dir(&data_dir); + }; - start_client_impl( - Box::new(os_input), - opts, - config, - config_options, - ClientInfo::New(session_name), - layout, - ); + if let Some(session_name) = opts.session.clone() { + start_client_plan(session_name.clone()); + start_client_impl( + Box::new(os_input), + opts, + config, + config_options, + ClientInfo::New(session_name), + layout, + ); + } else { + if let Some(layout_some) = layout.clone() { + if let Some(session_name) = layout_some.session.name { + if layout_some.session.attach.unwrap() { + let client = attach_with_session_name( + Some(session_name), + config_options.clone(), + true, + ); + + let attach_layout = match client { + ClientInfo::Attach(_, _) => None, + ClientInfo::New(_) => layout, + }; + + start_client_impl( + Box::new(os_input), + opts, + config, + config_options, + client, + attach_layout, + ); + } else { + start_client_plan(session_name.clone()); + start_client_impl( + Box::new(os_input), + opts, + config, + config_options, + ClientInfo::New(session_name), + layout, + ); + } + + process::exit(0); + } + } + + let session_name = names::Generator::default().next().unwrap(); + start_client_plan(session_name.clone()); + start_client_impl( + Box::new(os_input), + opts, + config, + config_options, + ClientInfo::New(session_name), + layout, + ); + } } } diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 2bbc8cb48..df976b8e1 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -144,6 +144,8 @@ pub struct Layout { #[serde(default)] pub struct LayoutFromYaml { #[serde(default)] + pub session: SessionFromYaml, + #[serde(default)] pub template: LayoutTemplate, #[serde(default)] pub borderless: bool, @@ -245,6 +247,20 @@ impl LayoutFromYaml { } } +// The struct that is used to deserialize the session from +// a yaml configuration file +#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)] +#[serde(crate = "self::serde")] +pub struct SessionFromYaml { + pub name: Option<String>, + #[serde(default = "default_as_some_true")] + pub attach: Option<bool>, +} + +fn default_as_some_true() -> Option<bool> { + Some(true) +} + // The struct that carries the information template that is used to // construct the layout #[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] @@ -591,6 +607,7 @@ impl Default for LayoutTemplate { impl Default for LayoutFromYaml { fn default() -> Self { Self { + session: SessionFromYaml::default(), template: LayoutTemplate::default(), borderless: false, tabs: vec![], diff --git a/zellij-utils/src/input/unit/fixtures/layouts/session-name-to-layout.yaml b/zellij-utils/src/input/unit/fixtures/layouts/session-name-to-layout.yaml new file mode 100644 index 000000000..144d555b9 --- /dev/null +++ b/zellij-utils/src/input/unit/fixtures/layouts/session-name-to-layout.yaml @@ -0,0 +1,3 @@ +--- +session: + name: "zellij-session" diff --git a/zellij-utils/src/input/unit/layout_test.rs b/zellij-utils/src/input/unit/layout_test.rs index dc8969251..2ec66a2a0 100644 --- a/zellij-utils/src/input/unit/layout_test.rs +++ b/zellij-utils/src/input/unit/layout_test.rs @@ -719,3 +719,25 @@ fn no_layout_template_merged_correctly() { assert_eq!(merged_layout, tab_layout.try_into().unwrap()); } + +#[test] +fn session_name_to_layout_is_ok() { + let path = layout_test_dir("session-name-to-layout.yaml".into()); + let layout_from_yaml = LayoutFromYaml::new(&path); + assert!(layout_from_yaml.is_ok()); +} + +#[test] +fn session_name_to_layout_has_name() { + let path = layout_test_dir("session-name-to-layout.yaml".into()); + let layout_from_yaml = LayoutFromYaml::new(&path); + let layout_template = layout_from_yaml.unwrap(); + let session_layout = layout_template.session; + + let expected_session = SessionFromYaml { + name: Some(String::from("zellij-session")), + attach: Some(true), + }; + + assert_eq!(expected_session, session_layout); +} |