diff options
author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-19 20:01:09 +0530 |
---|---|---|
committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-22 22:19:50 +0530 |
commit | d231d28d7cdf7df1f44268627f5630edb19b21a0 (patch) | |
tree | c9d74e7844b8007c8bcd3d721bd56bd9c064a3f7 /src | |
parent | d6fc7b04d14e8eeb5157cace639cc1d157766987 (diff) |
Implement the minimal list-sessions command
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs index eb7f476b9..122747b65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,13 @@ mod tests; use std::convert::TryFrom; +use std::os::unix::fs::FileTypeExt; +use std::{fs, io, process}; use zellij_client::{os_input_output::get_client_os_input, start_client}; use zellij_server::{os_input_output::get_server_os_input, start_server}; use zellij_utils::{ - cli::{CliArgs, Command}, - consts::{ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR}, + cli::{CliArgs, Command, Sessions}, + consts::{ZELLIJ_SOCK_DIR, ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR}, input::config::Config, logging::*, setup::Setup, @@ -16,7 +18,9 @@ use zellij_utils::{ pub fn main() { let opts = CliArgs::from_args(); - if let Some(Command::Setup(ref setup)) = opts.command { + if let Some(Command::Sessions(Sessions::ListSessions)) = opts.command { + list_sessions(); + } else if let Some(Command::Setup(ref setup)) = opts.command { Setup::from_cli(setup, &opts).expect("Failed to print to stdout"); } @@ -24,7 +28,7 @@ pub fn main() { Ok(config) => config, Err(e) => { eprintln!("There was an error in the config file:\n{}", e); - std::process::exit(1); + process::exit(1); } }; atomic_create_dir(&*ZELLIJ_TMP_DIR).unwrap(); @@ -34,7 +38,7 @@ pub fn main() { Ok(server_os_input) => server_os_input, Err(e) => { eprintln!("failed to open terminal:\n{}", e); - std::process::exit(1); + process::exit(1); } }; start_server(Box::new(os_input), path); @@ -43,9 +47,36 @@ pub fn main() { Ok(os_input) => os_input, Err(e) => { eprintln!("failed to open terminal:\n{}", e); - std::process::exit(1); + process::exit(1); } }; start_client(Box::new(os_input), opts, config); } } + +fn list_sessions() { + match fs::read_dir(&*ZELLIJ_SOCK_DIR) { + Ok(files) => { + let mut is_empty = true; + files.for_each(|file| { + let file = file.unwrap(); + if file.file_type().unwrap().is_socket() { + println!("{}", file.file_name().into_string().unwrap()); + is_empty = false; + } + }); + if is_empty { + println!("No active zellij sessions found."); + } + } + Err(err) => { + if let io::ErrorKind::NotFound = err.kind() { + println!("No active zellij sessions found."); + } else { + eprintln!("Error occured: {}", err); + process::exit(1); + } + } + } + process::exit(0); +} |