aboutsummaryrefslogtreecommitdiffstats
path: root/src/sessions.rs
diff options
context:
space:
mode:
authorKunal Mohan <kunalmohan99@gmail.com>2021-05-22 22:34:00 +0530
committerKunal Mohan <kunalmohan99@gmail.com>2021-05-22 22:34:00 +0530
commitcba7c07cd66c15be82e32c3e6766feac88dce3ff (patch)
treeb564291e7af16b8b8cdb68fd1341636c210aee19 /src/sessions.rs
parentAllow user to specify session name (diff)
downloadsl0ppij-cba7c07cd66c15be82e32c3e6766feac88dce3ff.tar.gz
sl0ppij-cba7c07cd66c15be82e32c3e6766feac88dce3ff.tar.xz
sl0ppij-cba7c07cd66c15be82e32c3e6766feac88dce3ff.zip
rename list_sessions file to sessions
Diffstat (limited to 'src/sessions.rs')
-rw-r--r--src/sessions.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/sessions.rs b/src/sessions.rs
new file mode 100644
index 00000000..ee72a34d
--- /dev/null
+++ b/src/sessions.rs
@@ -0,0 +1,86 @@
+use std::os::unix::fs::FileTypeExt;
+use std::{fs, io, process};
+use zellij_utils::consts::ZELLIJ_SOCK_DIR;
+
+fn get_sessions() -> Result<Vec<String>, io::ErrorKind> {
+ match fs::read_dir(&*ZELLIJ_SOCK_DIR) {
+ Ok(files) => {
+ let mut sessions = Vec::new();
+ files.for_each(|file| {
+ let file = file.unwrap();
+ if file.file_type().unwrap().is_socket() {
+ sessions.push(file.file_name().into_string().unwrap());
+ }
+ });
+ Ok(sessions)
+ }
+ Err(err) => {
+ if let io::ErrorKind::NotFound = err.kind() {
+ Ok(Vec::with_capacity(0))
+ } else {
+ Err(err.kind())
+ }
+ }
+ }
+}
+
+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);
+ })
+ }
+ 0
+ }
+ Err(e) => {
+ eprintln!("Error occured: {:?}", e);
+ 1
+ }
+ };
+ process::exit(exit_code);
+}
+
+pub(crate) fn assert_session(name: &str) {
+ let exit_code = 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
+ }
+ };
+ 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);
+}