use std::{ io::{ stdout, IsTerminal }, path::PathBuf }; use tmux_interface::{ Session, StdIO, Tmux, commands, variables::session::SessionsCtl }; use crate::error; pub const NULL: Option = Some(StdIO::Null); pub fn session_name() -> Option { let message = commands::DisplayMessage::new().print().message("#{session_name}"); let result = Tmux::new().add_command(message).output(); if let Ok(output) = result { let text = String::from_utf8(output.0.stdout); if let Ok(title) = text { Some(title[0..title.len() - 1].to_owned()) } else { None } } else { None } } /// return a Vec of all sessions or None pub fn get_sessions() -> Option> { let sessions = SessionsCtl::new().get_all(); if let Ok(sessions) = sessions { return Some(sessions.0); } else { return None; } } /// check whether a target session exists pub fn session_exists>(target: S) -> bool { let has_session = commands::HasSession::new() .target_session(target.into()); Tmux::new().add_command(has_session) .stderr(NULL) .status() .unwrap() .success() } /// enforce a command is being run in a terminal pub fn terminal_enforce() { if !stdout().is_terminal() { error::not_terminal(); } } /// recursively propagate up directories to find a child pub fn find(target: &str, path: PathBuf) -> Option { if path.join(target).exists() { return Some(path); } let parent = path.parent(); if let Some(parent) = parent { return find(target, parent.to_path_buf()) } else { None } }