Skip to content

Commit

Permalink
feat: add initial session name to layout template (#789)
Browse files Browse the repository at this point in the history
* feat: add session configuration to layout template

WIP: prototyping for issue #611

* test(layout): add session name

* feat(layout): add cond flow to check session name

* feat(layout): update session

* feat: add function to attach a session

* fix(layout): update feedback

* attach option only works when layout template exists.

* feat(layout): add conditional for session-layout

* update default attach value
  • Loading branch information
jaeheonji authored Nov 8, 2021
1 parent b232326 commit 4838f0b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 17 deletions.
83 changes: 66 additions & 17 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
}
17 changes: 17 additions & 0 deletions zellij-utils/src/input/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ pub struct Layout {
#[serde(crate = "self::serde")]
#[serde(default)]
pub struct LayoutFromYaml {
#[serde(default)]
pub session: SessionFromYaml,
#[serde(default)]
pub template: LayoutTemplate,
#[serde(default)]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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![],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
session:
name: "zellij-session"
22 changes: 22 additions & 0 deletions zellij-utils/src/input/unit/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4838f0b

Please sign in to comment.