2023-06-15 11:33:08 -04:00
|
|
|
use std::{
|
|
|
|
env::var,
|
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-02-28 09:35:30 -05:00
|
|
|
Session, Sessions, TmuxCommand,
|
2023-04-06 13:34:32 -04:00
|
|
|
variables::session::session::SESSION_ALL
|
|
|
|
};
|
|
|
|
|
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>> {
|
|
|
|
let i_sessions = Sessions::get(SESSION_ALL);
|
|
|
|
if i_sessions.is_err() { return None; }
|
|
|
|
let sessions = i_sessions.ok();
|
|
|
|
if sessions.is_none() { return None; }
|
|
|
|
|
|
|
|
Some(sessions.unwrap().0)
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
let tmux = var("TMUX").ok();
|
|
|
|
if tmux.is_some() && tmux.unwrap() != "" {
|
2024-02-04 16:07:24 -05:00
|
|
|
println!("Sessions should be nested with care; unset TMUX or use the '-n' flag to allow.");
|
2023-06-15 11:33:08 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-28 09:35:30 -05:00
|
|
|
TmuxCommand::new()
|
|
|
|
.has_session()
|
2024-02-28 17:49:08 -05:00
|
|
|
.target_session(target.into())
|
2024-02-28 09:35:30 -05:00
|
|
|
.output().unwrap()
|
|
|
|
.success()
|
|
|
|
}
|
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
|