Implement the minimal list-sessions command

This commit is contained in:
Kunal Mohan 2021-05-19 20:01:09 +05:30
parent d6fc7b04d1
commit d231d28d7c
2 changed files with 45 additions and 10 deletions

View file

@ -2,11 +2,13 @@
mod tests; mod tests;
use std::convert::TryFrom; 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_client::{os_input_output::get_client_os_input, start_client};
use zellij_server::{os_input_output::get_server_os_input, start_server}; use zellij_server::{os_input_output::get_server_os_input, start_server};
use zellij_utils::{ use zellij_utils::{
cli::{CliArgs, Command}, cli::{CliArgs, Command, Sessions},
consts::{ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR}, consts::{ZELLIJ_SOCK_DIR, ZELLIJ_TMP_DIR, ZELLIJ_TMP_LOG_DIR},
input::config::Config, input::config::Config,
logging::*, logging::*,
setup::Setup, setup::Setup,
@ -16,7 +18,9 @@ use zellij_utils::{
pub fn main() { pub fn main() {
let opts = CliArgs::from_args(); 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"); Setup::from_cli(setup, &opts).expect("Failed to print to stdout");
} }
@ -24,7 +28,7 @@ pub fn main() {
Ok(config) => config, Ok(config) => config,
Err(e) => { Err(e) => {
eprintln!("There was an error in the config file:\n{}", 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(); atomic_create_dir(&*ZELLIJ_TMP_DIR).unwrap();
@ -34,7 +38,7 @@ pub fn main() {
Ok(server_os_input) => server_os_input, Ok(server_os_input) => server_os_input,
Err(e) => { Err(e) => {
eprintln!("failed to open terminal:\n{}", e); eprintln!("failed to open terminal:\n{}", e);
std::process::exit(1); process::exit(1);
} }
}; };
start_server(Box::new(os_input), path); start_server(Box::new(os_input), path);
@ -43,9 +47,36 @@ pub fn main() {
Ok(os_input) => os_input, Ok(os_input) => os_input,
Err(e) => { Err(e) => {
eprintln!("failed to open terminal:\n{}", e); eprintln!("failed to open terminal:\n{}", e);
std::process::exit(1); process::exit(1);
} }
}; };
start_client(Box::new(os_input), opts, config); 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);
}

View file

@ -27,7 +27,7 @@ lazy_static! {
pub static ref SESSION_NAME: String = names::Generator::default().next().unwrap(); pub static ref SESSION_NAME: String = names::Generator::default().next().unwrap();
pub static ref ZELLIJ_PROJ_DIR: ProjectDirs = pub static ref ZELLIJ_PROJ_DIR: ProjectDirs =
ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap();
pub static ref ZELLIJ_IPC_PIPE: PathBuf = { pub static ref ZELLIJ_SOCK_DIR: PathBuf = {
let mut ipc_dir = env::var("ZELLIJ_SOCKET_DIR").map_or_else( let mut ipc_dir = env::var("ZELLIJ_SOCKET_DIR").map_or_else(
|_| { |_| {
ZELLIJ_PROJ_DIR ZELLIJ_PROJ_DIR
@ -37,11 +37,15 @@ lazy_static! {
PathBuf::from, PathBuf::from,
); );
ipc_dir.push(VERSION); ipc_dir.push(VERSION);
fs::create_dir_all(&ipc_dir).unwrap();
set_permissions(&ipc_dir).unwrap();
ipc_dir.push(&*SESSION_NAME);
ipc_dir ipc_dir
}; };
pub static ref ZELLIJ_IPC_PIPE: PathBuf = {
let mut sock_dir = ZELLIJ_SOCK_DIR.clone();
fs::create_dir_all(&sock_dir).unwrap();
set_permissions(&sock_dir).unwrap();
sock_dir.push(&*SESSION_NAME);
sock_dir
};
pub static ref ZELLIJ_TMP_DIR: PathBuf = pub static ref ZELLIJ_TMP_DIR: PathBuf =
PathBuf::from("/tmp/zellij-".to_string() + &format!("{}", *UID)); PathBuf::from("/tmp/zellij-".to_string() + &format!("{}", *UID));
pub static ref ZELLIJ_TMP_LOG_DIR: PathBuf = ZELLIJ_TMP_DIR.join("zellij-log"); pub static ref ZELLIJ_TMP_LOG_DIR: PathBuf = ZELLIJ_TMP_DIR.join("zellij-log");