diff options
| author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-29 00:32:06 +0530 |
|---|---|---|
| committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-29 00:32:06 +0530 |
| commit | 0bd05cbcb46d607079608bb83bdefba97bf7a764 (patch) | |
| tree | d4abb52e544ec5afb8bc23c3ee770492d942ba72 /src/sessions.rs | |
| parent | docs(changelog): Fix lag #536 (diff) | |
| download | sl0ppij-0bd05cbcb46d607079608bb83bdefba97bf7a764.tar.gz sl0ppij-0bd05cbcb46d607079608bb83bdefba97bf7a764.tar.xz sl0ppij-0bd05cbcb46d607079608bb83bdefba97bf7a764.zip | |
Make session-name option in attach command
If only one session is running, attach to it.
Otherwise list the active sessions (if any)
Diffstat (limited to 'src/sessions.rs')
| -rw-r--r-- | src/sessions.rs | 97 |
1 files changed, 55 insertions, 42 deletions
diff --git a/src/sessions.rs b/src/sessions.rs index fd834e40..fb1a9635 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -29,22 +29,61 @@ fn get_sessions() -> Result<Vec<String>, io::ErrorKind> { } } +fn assert_socket(name: &str) -> bool { + let path = &*ZELLIJ_SOCK_DIR.join(name); + match LocalSocketStream::connect(path) { + Ok(stream) => { + IpcSenderWithContext::new(stream).send(ClientToServerMsg::ClientExited); + true + } + Err(e) => { + if e.kind() == io::ErrorKind::ConnectionRefused { + drop(fs::remove_file(path)); + false + } else { + true + } + } + } +} + +fn print_sessions(sessions: Vec<String>) { + let curr_session = std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into()); + sessions.iter().for_each(|session| { + let suffix = if curr_session == *session { + " (current)" + } else { + "" + }; + println!("{}{}", session, suffix); + }) +} + +pub(crate) fn get_active_session() -> String { + match get_sessions() { + Ok(mut sessions) => { + if sessions.len() == 1 { + return sessions.pop().unwrap(); + } + if sessions.is_empty() { + println!("No active zellij sessions found."); + } else { + println!("Please specify the session name to attach to. The following sessions are active:"); + print_sessions(sessions); + } + } + Err(e) => eprintln!("Error occured: {:?}", e), + } + process::exit(1); +} + pub(crate) fn list_sessions() { let exit_code = match get_sessions() { Ok(sessions) => { if sessions.is_empty() { println!("No active zellij sessions found."); } else { - let curr_session = - std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into()); - sessions.iter().for_each(|session| { - let suffix = if curr_session == *session { - " (current)" - } else { - "" - }; - println!("{}{}", session, suffix); - }) + print_sessions(sessions); } 0 } @@ -57,53 +96,27 @@ pub(crate) fn list_sessions() { } pub(crate) fn assert_session(name: &str) { - let exit_code = match get_sessions() { + match get_sessions() { Ok(sessions) => { if sessions.iter().any(|s| s == name) { return; } println!("No session named {:?} found.", name); - 0 - } - Err(e) => { - eprintln!("Error occured: {:?}", e); - 1 } + Err(e) => eprintln!("Error occured: {:?}", e), }; - process::exit(exit_code); + process::exit(1); } pub(crate) fn assert_session_ne(name: &str) { - let exit_code = match get_sessions() { + 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 } + Err(e) => eprintln!("Error occured: {:?}", e), }; - process::exit(exit_code); -} - -fn assert_socket(name: &str) -> bool { - let path = &*ZELLIJ_SOCK_DIR.join(name); - match LocalSocketStream::connect(path) { - Ok(stream) => { - IpcSenderWithContext::new(stream).send(ClientToServerMsg::ClientExited); - true - } - Err(e) => { - if e.kind() == io::ErrorKind::ConnectionRefused { - drop(fs::remove_file(path)); - false - } else { - true - } - } - } + process::exit(1); } |
