diff options
| author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-22 22:12:43 +0530 |
|---|---|---|
| committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-22 22:21:26 +0530 |
| commit | 0621ba8f349a28d0f746f4c8349658025e5f64f5 (patch) | |
| tree | bfe825e2987fdc3df35a12a380e1e0730757297e /src | |
| parent | check if session exists before attaching (diff) | |
| download | sl0ppij-0621ba8f349a28d0f746f4c8349658025e5f64f5.tar.gz sl0ppij-0621ba8f349a28d0f746f4c8349658025e5f64f5.tar.xz sl0ppij-0621ba8f349a28d0f746f4c8349658025e5f64f5.zip | |
Allow user to specify session name
Diffstat (limited to 'src')
| -rw-r--r-- | src/list_sessions.rs | 17 | ||||
| -rw-r--r-- | src/main.rs | 18 | ||||
| -rw-r--r-- | src/tests/mod.rs | 4 |
3 files changed, 33 insertions, 6 deletions
diff --git a/src/list_sessions.rs b/src/list_sessions.rs index 41437e1a..ee72a34d 100644 --- a/src/list_sessions.rs +++ b/src/list_sessions.rs @@ -67,3 +67,20 @@ pub(crate) fn assert_session(name: &str) { }; process::exit(exit_code); } + +pub(crate) fn assert_session_ne(name: &str) { + let exit_code = match get_sessions() { + Ok(sessions) => { + if sessions.iter().all(|s| s != name) { + return; + } + println!("Session with name {:?} aleady exists. Use attach command to connect to it or specify a different name.", name); + 0 + } + Err(e) => { + eprintln!("Error occured: {:?}", e); + 1 + } + }; + process::exit(exit_code); +} diff --git a/src/main.rs b/src/main.rs index 5a6d5ed1..a91ce508 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,10 @@ mod list_sessions; #[cfg(test)] mod tests; -use list_sessions::{assert_session, list_sessions}; +use list_sessions::{assert_session, assert_session_ne, list_sessions}; use std::convert::TryFrom; use std::process; -use zellij_client::{os_input_output::get_client_os_input, start_client}; +use zellij_client::{os_input_output::get_client_os_input, start_client, ClientInfo}; use zellij_server::{os_input_output::get_server_os_input, start_server}; use zellij_utils::{ cli::{CliArgs, Command, Sessions}, @@ -61,10 +61,20 @@ pub fn main() { Box::new(os_input), opts, config, - Some((session_name, force)), + ClientInfo::Attach(session_name, force), ); } else { - start_client(Box::new(os_input), opts, config, None); + let session_name = opts + .session + .clone() + .unwrap_or_else(|| names::Generator::default().next().unwrap()); + assert_session_ne(&session_name); + start_client( + Box::new(os_input), + opts, + config, + ClientInfo::New(session_name), + ); } } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index f8a48a39..e075ed49 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -5,7 +5,7 @@ pub mod tty_inputs; pub mod utils; use std::path::PathBuf; -use zellij_client::{os_input_output::ClientOsApi, start_client}; +use zellij_client::{os_input_output::ClientOsApi, start_client, ClientInfo}; use zellij_server::{os_input_output::ServerOsApi, start_server}; use zellij_utils::{cli::CliArgs, input::config::Config}; @@ -21,6 +21,6 @@ pub fn start( start_server(server_os_input, PathBuf::from("")); }) .unwrap(); - start_client(client_os_input, opts, config, None); + start_client(client_os_input, opts, config, ClientInfo::New("".into())); let _ = server_thread.join(); } |
