use std::collections::HashMap;
use crate::contexts::Context;
use crate::layout::Node;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MultiplexerKind {
Tmux,
Zellij,
}
impl MultiplexerKind {
const ALL: &[(&str, MultiplexerKind)] = &[
("tmux", MultiplexerKind::Tmux),
("zellij", MultiplexerKind::Zellij),
];
pub fn parse(raw: &str) -> Option<MultiplexerKind> {
Self::ALL
.iter()
.find(|(name, _)| *name == raw)
.map(|(_, kind)| *kind)
}
pub fn names() -> String {
Self::ALL
.iter()
.map(|(name, _)| *name)
.collect::<Vec<_>>()
.join(", ")
}
}
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct MultiplexerError(pub String);
pub(crate) fn env_var(key: &str) -> Option<String> {
#[cfg(test)]
if let Some(value) = crate::testutil::get_env(key) {
return Some(value);
}
std::env::var(key).ok()
}
pub(crate) fn env_truthy(key: &str) -> bool {
env_var(key).is_some_and(|value| !value.is_empty())
}
pub trait Multiplexer: Send + Sync {
fn can_open_in_place(&self) -> bool;
fn exists(&self, ctx: &Context) -> bool;
fn is_current(&self, ctx: &Context) -> bool;
fn create(
&self,
ctx: &Context,
values: Option<&HashMap<String, String>>,
) -> Result<(), MultiplexerError>;
fn open(
&self,
ctx: &Context,
values: Option<&HashMap<String, String>>,
) -> Result<(), MultiplexerError>;
fn kill(&self, ctx: &Context) -> Result<(), MultiplexerError>;
}
pub fn get_multiplexer(kind: MultiplexerKind, layout: Node) -> std::sync::Arc<dyn Multiplexer> {
match kind {
MultiplexerKind::Tmux => {
std::sync::Arc::new(crate::multiplexers::tmux::TmuxMultiplexer::new(layout))
}
MultiplexerKind::Zellij => {
std::sync::Arc::new(crate::multiplexers::zellij::ZellijMultiplexer::new(layout))
}
}
}