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

Adds create option to attach #587

Closed
wants to merge 1 commit into from
Closed
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
99 changes: 55 additions & 44 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,52 +57,63 @@ pub fn main() {
process::exit(1);
}
};
if let Some(Command::Sessions(Sessions::Attach {
mut session_name,
force,
})) = opts.command.clone()
{
if let Some(session) = session_name.as_ref() {
assert_session(session);
} else {
session_name = Some(get_active_session());
}

start_client(
Box::new(os_input),
opts,
config,
ClientInfo::Attach(session_name.unwrap(), force, config_options),
None,
);
} else {
let session_name = opts
.session
.clone()
.unwrap_or_else(|| names::Generator::default().next().unwrap());
assert_session_ne(&session_name);

let client_info = match opts.command.clone() {
Some(Command::Sessions(Sessions::Attach {mut session_name, force, create})) => {
let is_new_session = match (session_name.as_ref(), create) {
(Some(_), true) => true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, it looks like it will always create a new session. We need to check if a session exists or not. In case it exists attach to it, otherwise create a new one.
You will probably have to change the assert_session() function (and maybe rename it) to return a bool corresponding to whether a session exists or not. And we can possibly remove the assert_session_ne() method in favour of this one.

(Some(session), false) => {
assert_session(session);
false
},
(None, _) => {
session_name = Some(get_active_session());
false
Comment on lines +69 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, we need to consider the --create flag as well-

  1. Flag is not present- If a single session exists attach to it, otherwise exit with a message.
  2. Flag is present- Is a single session exists, attach to it. If no session exists, create a new one with a random name (generated using names::Generator). If >1 sessions exist, exit with a message.

}
};

// 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);
if is_new_session {
ClientInfo::New(session_name.unwrap())
} else {
ClientInfo::Attach(session_name.unwrap(), force, config_options.clone())
}
}
_ => {
let session_name = opts
.session
.clone()
.unwrap_or_else(|| names::Generator::default().next().unwrap());
assert_session_ne(&session_name);

ClientInfo::New(session_name)
}
};

let layout = match client_info {
ClientInfo::New(_) => {
// 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 layout_dir = config_options.layout_dir.or_else(|| {
get_layout_dir(opts.config_dir.clone().or_else(find_default_config_dir))
});
let layout = Layout::from_path_or_default(
opts.layout.as_ref(),
opts.layout_path.as_ref(),
layout_dir,
);
let layout_dir = config_options.layout_dir.or_else(|| {
get_layout_dir(opts.config_dir.clone().or_else(find_default_config_dir))
});
Layout::from_path_or_default(
opts.layout.as_ref(),
opts.layout_path.as_ref(),
layout_dir,
)
}
ClientInfo::Attach(..) => None
};

start_client(
Box::new(os_input),
opts,
config,
ClientInfo::New(session_name),
layout,
);
}
start_client(
Box::new(os_input),
opts,
config,
client_info,
layout,
);
}
}
5 changes: 5 additions & 0 deletions zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ pub enum Sessions {
/// zellij client (if any) and attach to this.
#[structopt(long, short)]
force: bool,

/// Creates the session if it doesn't already exists.
/// Will be ignored if session-name is not set.
#[structopt(long, short)]
create: bool,
},
}