diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commands.rs | 176 | ||||
-rw-r--r-- | src/sessions.rs | 77 | ||||
-rw-r--r-- | src/tests/e2e/snapshots/zellij__tests__e2e__cases__scrolling_inside_a_pane.snap | 2 |
3 files changed, 133 insertions, 122 deletions
diff --git a/src/commands.rs b/src/commands.rs index 23105ec01..aec3970e0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -23,27 +23,26 @@ pub(crate) use crate::sessions::list_sessions; pub(crate) fn kill_all_sessions(yes: bool) { match get_sessions() { + Ok(sessions) if sessions.is_empty() => { + println!("No active zellij sessions found."); + process::exit(1); + } Ok(sessions) => { - if sessions.is_empty() { - println!("No active zellij sessions found."); - process::exit(1); - } else { - if !yes { - println!("WARNING: this action will kill all sessions."); - if !Confirm::new() - .with_prompt("Do you want to continue?") - .interact() - .unwrap() - { - println!("Abort."); - process::exit(1); - } - } - for session in sessions.iter() { - kill_session_impl(session); + if !yes { + println!("WARNING: this action will kill all sessions."); + if !Confirm::new() + .with_prompt("Do you want to continue?") + .interact() + .unwrap() + { + println!("Abort."); + process::exit(1); } - process::exit(0); } + for session in sessions.iter() { + kill_session_impl(session); + } + process::exit(0); } Err(e) => { eprintln!("Error occurred: {:?}", e); @@ -95,17 +94,14 @@ fn find_indexed_session( ) -> ClientInfo { match sessions.get(index) { Some(session) => ClientInfo::Attach(session.clone(), config_options), + None if create => create_new_client(), None => { - if create { - create_new_client() - } else { - println!( - "No session indexed by {} found. The following sessions are active:", - index - ); - print_sessions_with_index(sessions); - process::exit(1); - } + println!( + "No session indexed by {} found. The following sessions are active:", + index + ); + print_sessions_with_index(sessions); + process::exit(1); } } } @@ -113,18 +109,15 @@ fn find_indexed_session( fn attach_with_session_index(config_options: Options, index: usize, create: bool) -> ClientInfo { // Ignore the session_name when `--index` is provided match get_sessions_sorted_by_creation_date() { - Ok(sessions) => { - if sessions.is_empty() { - if create { - create_new_client() - } else { - println!("No active zellij sessions found."); - process::exit(1); - } + Ok(sessions) if sessions.is_empty() => { + if create { + create_new_client() } else { - find_indexed_session(sessions, config_options, index, create) + println!("No active zellij sessions found."); + process::exit(1); } } + Ok(sessions) => find_indexed_session(sessions, config_options, index, create), Err(e) => { eprintln!("Error occurred: {:?}", e); process::exit(1); @@ -138,26 +131,22 @@ fn attach_with_session_name( create: bool, ) -> ClientInfo { match session_name.as_ref() { - Some(session) => { - if create { - if !session_exists(session).unwrap() { - ClientInfo::New(session_name.unwrap()) - } else { - ClientInfo::Attach(session_name.unwrap(), config_options) - } + Some(session) if create => { + if !session_exists(session).unwrap() { + ClientInfo::New(session_name.unwrap()) } else { - assert_session(session); ClientInfo::Attach(session_name.unwrap(), config_options) } } + Some(session) => { + assert_session(session); + ClientInfo::Attach(session_name.unwrap(), config_options) + } None => match get_active_session() { + ActiveSession::None if create => create_new_client(), ActiveSession::None => { - if create { - create_new_client() - } else { - println!("No active zellij sessions found."); - process::exit(1); - } + println!("No active zellij sessions found."); + process::exit(1); } ActiveSession::One(session_name) => ClientInfo::Attach(session_name, config_options), ActiveSession::Many => { @@ -187,7 +176,7 @@ pub(crate) fn start_client(opts: CliArgs) { })) = opts.command.clone() { let config_options = match options { - Some(SessionCommand::Options(o)) => config_options.merge(o), + Some(SessionCommand::Options(o)) => config_options.merge_from_cli(o.into()), None => config_options, }; @@ -211,24 +200,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/src/sessions.rs b/src/sessions.rs index 4d2fe5fb9..0cc870a7e 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -4,6 +4,7 @@ use std::{fs, io, process}; use suggestion::Suggest; use zellij_utils::{ consts::ZELLIJ_SOCK_DIR, + envs, interprocess::local_socket::LocalSocketStream, ipc::{ClientToServerMsg, IpcSenderWithContext}, }; @@ -21,13 +22,8 @@ pub(crate) fn get_sessions() -> Result<Vec<String>, io::ErrorKind> { }); Ok(sessions) } - Err(err) => { - if let io::ErrorKind::NotFound = err.kind() { - Ok(Vec::with_capacity(0)) - } else { - Err(err.kind()) - } - } + Err(err) if io::ErrorKind::NotFound != err.kind() => Err(err.kind()), + Err(_) => Ok(Vec::with_capacity(0)), } } @@ -51,13 +47,8 @@ pub(crate) fn get_sessions_sorted_by_creation_date() -> anyhow::Result<Vec<Strin .collect(); Ok(sessions) } - Err(err) => { - if let io::ErrorKind::NotFound = err.kind() { - Ok(Vec::with_capacity(0)) - } else { - Err(err.into()) - } - } + Err(err) if io::ErrorKind::NotFound != err.kind() => Err(err.into()), + Err(_) => Ok(Vec::with_capacity(0)), } } @@ -68,19 +59,16 @@ fn assert_socket(name: &str) -> bool { IpcSenderWithContext::new(stream).send(ClientToServerMsg::ClientExited); true } - Err(e) => { - if e.kind() == io::ErrorKind::ConnectionRefused { - drop(fs::remove_file(path)); - false - } else { - true - } + Err(e) if e.kind() == io::ErrorKind::ConnectionRefused => { + drop(fs::remove_file(path)); + false } + Err(_) => true, } } pub(crate) fn print_sessions(sessions: Vec<String>) { - let curr_session = std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into()); + let curr_session = envs::get_session_name().unwrap_or_else(|_| "".into()); sessions.iter().for_each(|session| { let suffix = if curr_session == *session { " (current)" @@ -92,7 +80,7 @@ pub(crate) fn print_sessions(sessions: Vec<String>) { } pub(crate) fn print_sessions_with_index(sessions: Vec<String>) { - let curr_session = std::env::var("ZELLIJ_SESSION_NAME").unwrap_or_else(|_| "".into()); + let curr_session = envs::get_session_name().unwrap_or_else(|_| "".into()); for (i, session) in sessions.iter().enumerate() { let suffix = if curr_session == *session { " (current)" @@ -111,16 +99,9 @@ pub(crate) enum ActiveSession { pub(crate) fn get_active_session() -> ActiveSession { match get_sessions() { - Ok(mut sessions) => { - if sessions.len() == 1 { - return ActiveSession::One(sessions.pop().unwrap()); - } - if sessions.is_empty() { - ActiveSession::None - } else { - ActiveSession::Many - } - } + Ok(sessions) if sessions.is_empty() => ActiveSession::None, + Ok(mut sessions) if sessions.len() == 1 => ActiveSession::One(sessions.pop().unwrap()), + Ok(_) => ActiveSession::Many, Err(e) => { eprintln!("Error occurred: {:?}", e); process::exit(1); @@ -143,12 +124,12 @@ pub(crate) fn kill_session(name: &str) { pub(crate) fn list_sessions() { let exit_code = match get_sessions() { - Ok(sessions) => { - if sessions.is_empty() { - println!("No active zellij sessions found."); - } else { - print_sessions(sessions); - } + Ok(sessions) if !sessions.is_empty() => { + print_sessions(sessions); + 0 + } + Ok(_) => { + println!("No active zellij sessions found."); 0 } Err(e) => { @@ -161,12 +142,8 @@ pub(crate) fn list_sessions() { pub(crate) fn session_exists(name: &str) -> Result<bool, io::ErrorKind> { return match get_sessions() { - Ok(sessions) => { - if sessions.iter().any(|s| s == name) { - return Ok(true); - } - Ok(false) - } + Ok(sessions) if sessions.iter().any(|s| s == name) => Ok(true), + Ok(_) => Ok(false), Err(e) => Err(e), }; } @@ -191,13 +168,9 @@ pub(crate) fn assert_session(name: &str) { } pub(crate) fn assert_session_ne(name: &str) { - 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); - } + match session_exists(name) { + Ok(result) if !result => return, + Ok(_) => println!("Session with name {:?} already exists. Use attach command to connect to it or specify a different name.", name), Err(e) => eprintln!("Error occurred: {:?}", e), }; process::exit(1); diff --git a/src/tests/e2e/snapshots/zellij__tests__e2e__cases__scrolling_inside_a_pane.snap b/src/tests/e2e/snapshots/zellij__tests__e2e__cases__scrolling_inside_a_pane.snap index fdea92b7a..664e881b4 100644 --- a/src/tests/e2e/snapshots/zellij__tests__e2e__cases__scrolling_inside_a_pane.snap +++ b/src/tests/e2e/snapshots/zellij__tests__e2e__cases__scrolling_inside_a_pane.snap @@ -26,4 +26,4 @@ expression: last_snapshot │ ││line19 00000000000000000000000000000000000000000000000000█│ └──────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────┘ Ctrl + <g> LOCK <p> PANE <t> TAB <n> RESIZE <h> MOVE <s> SCROLL <o> SESSION <q> QUIT - <↓↑> Scroll / <PgUp/PgDn> Scroll Page / <ENTER> Select pane + <↓↑> Scroll / <PgUp/PgDn> Scroll Page / <u/d> Scroll Half Page / <ENTER> Select pane |