2023-06-15 11:33:08 -04:00
|
|
|
use std::{
|
2024-06-10 12:02:02 -04:00
|
|
|
env::current_dir,
|
2024-06-10 12:01:15 -04:00
|
|
|
io::{ stdout, IsTerminal },
|
2024-02-28 14:26:25 -05:00
|
|
|
path::PathBuf,
|
2023-06-15 11:33:08 -04:00
|
|
|
process::exit
|
|
|
|
};
|
2023-04-06 13:34:32 -04:00
|
|
|
|
|
|
|
use tmux_interface::{
|
2024-03-06 15:54:41 -05:00
|
|
|
Session, Tmux,
|
|
|
|
|
|
|
|
commands,
|
|
|
|
variables::session::SessionsCtl
|
2023-04-06 13:34:32 -04:00
|
|
|
};
|
|
|
|
|
2024-06-10 12:02:02 -04:00
|
|
|
use crate::{
|
|
|
|
env,
|
|
|
|
error
|
|
|
|
};
|
2024-02-28 17:55:33 -05:00
|
|
|
|
2023-06-19 10:42:52 -04:00
|
|
|
/// return a Vec of all sessions or None
|
2023-04-06 13:34:32 -04:00
|
|
|
pub fn get_sessions() -> Option<Vec<Session>> {
|
2024-03-06 15:54:41 -05:00
|
|
|
let sessions = SessionsCtl::new().get_all();
|
|
|
|
if let Ok(sessions) = sessions {
|
|
|
|
return Some(sessions.0);
|
|
|
|
} else { return None; }
|
2023-04-06 13:34:32 -04:00
|
|
|
}
|
|
|
|
|
2023-06-19 10:42:52 -04:00
|
|
|
/// show the tmux nest text if env var is not unset
|
2023-06-15 11:33:08 -04:00
|
|
|
pub fn prevent_nest() {
|
2024-06-10 12:02:02 -04:00
|
|
|
if env::tmux() {
|
2024-06-10 12:02:57 -04:00
|
|
|
println!("To nest sessions, use the -n flag.");
|
2024-06-10 12:02:02 -04:00
|
|
|
exit(6);
|
2023-06-15 11:33:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 10:04:24 -05:00
|
|
|
/// enforce a command is being used in-session
|
|
|
|
pub fn session_enforce(cmd: &'static str) {
|
2024-06-10 12:02:57 -04:00
|
|
|
if !env::tmux() { error::not_in_session(cmd); }
|
2024-03-08 10:04:24 -05:00
|
|
|
}
|
|
|
|
|
2024-02-28 09:35:30 -05:00
|
|
|
/// check whether a target session exists
|
2024-02-28 17:49:08 -05:00
|
|
|
pub fn session_exists<S: Into<String>>(target: S) -> bool {
|
2024-03-06 15:54:41 -05:00
|
|
|
let has_session = commands::HasSession::new()
|
|
|
|
.target_session(target.into());
|
|
|
|
Tmux::new().add_command(has_session)
|
|
|
|
.status()
|
|
|
|
.unwrap()
|
2024-02-28 09:35:30 -05:00
|
|
|
.success()
|
|
|
|
}
|
|
|
|
|
2024-06-10 12:01:15 -04:00
|
|
|
/// enforce a command is being run in a terminal
|
|
|
|
pub fn terminal_enforce() {
|
|
|
|
if !stdout().is_terminal() { error::not_terminal(); }
|
|
|
|
}
|
|
|
|
|
2024-02-28 17:55:33 -05:00
|
|
|
/// attempt to return the repo name or exit
|
|
|
|
pub fn repo_fallback() -> String {
|
|
|
|
let repo = repo_root(current_dir().unwrap());
|
|
|
|
if repo.is_none() { error::missing_target(); }
|
|
|
|
|
|
|
|
let target = repo.unwrap().file_name().unwrap().to_string_lossy().to_string();
|
|
|
|
target
|
|
|
|
}
|
|
|
|
|
2024-02-28 14:26:25 -05:00
|
|
|
/// recursively attempt to find a git root directory
|
2024-02-28 14:42:24 -05:00
|
|
|
pub fn repo_root(path: PathBuf) -> Option<PathBuf> {
|
2024-02-28 14:26:25 -05:00
|
|
|
// if .git dir is found, return
|
|
|
|
if path.join(".git").exists() { return Some(path); }
|
|
|
|
|
|
|
|
// otherwise, attempt to traverse
|
|
|
|
let parent = path.parent();
|
2024-02-28 14:42:24 -05:00
|
|
|
if let Some(parent) = parent { repo_root(parent.to_path_buf()) }
|
2024-02-28 14:26:25 -05:00
|
|
|
else { None }
|
|
|
|
}
|
|
|
|
|