diff options
-rw-r--r-- | src/main.rs | 7 | ||||
-rw-r--r-- | src/tmux.rs | 22 | ||||
-rw-r--r-- | src/tmux/error.rs | 3 |
3 files changed, 31 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 523ee82..c32c246 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use clap::Parser; use sshr::{Config, HostName, Hosts, KnownHosts, Sessions, Stdout}; fn main() -> anyhow::Result<()> { - let config = Config::parse(); + let mut config = Config::parse(); tracing_subscriber::fmt::fmt() .with_max_level(&config.verbosity) @@ -20,6 +20,11 @@ fn main() -> anyhow::Result<()> { if config.enabled.tmux() { let tmux_sessions = config.tmux.list().context("Failed to list tmux sessions")?; + if config.tmux.exclude_attached { + if let Ok(session) = config.tmux.attached() { + config.stdio.exclude.push(session.name) + } + } sessions.extend(tmux_sessions); } diff --git a/src/tmux.rs b/src/tmux.rs index e3efbd5..b1f1af2 100644 --- a/src/tmux.rs +++ b/src/tmux.rs @@ -13,6 +13,9 @@ pub struct Tmux { /// tmux socket-name, equivelent to `tmux -L <socket-name>` #[arg(short = 'L', long = "tmux_socket", default_value = "ssh")] pub socket: String, + + #[arg(short = 'E', long)] + pub exclude_attached: bool, } impl Tmux { @@ -34,6 +37,24 @@ impl Tmux { .collect::<Result<_, _>>() .map_err(Into::into) } + + pub fn attached(&self) -> Result<Session, Error> { + let stdout = Command::new("tmux") + .arg("-L") + .arg(&self.socket) + .arg("display") + .arg("-p") + .arg(Self::SESSION_FORMAT) + .output()? + .stdout; + + std::str::from_utf8(&stdout)? + .lines() + .map(ron::from_str) + .last() + .transpose()? + .ok_or(Error::NotFound) + } } #[cfg(test)] @@ -59,6 +80,7 @@ mod tests { let tmux = Tmux { socket: SOCKET.to_owned(), + exclude_attached: false, }; let sessions: Vec<_> = tmux.list()?.into_iter().map(|s| s.name).collect(); diff --git a/src/tmux/error.rs b/src/tmux/error.rs index 6da5830..4891c7d 100644 --- a/src/tmux/error.rs +++ b/src/tmux/error.rs @@ -8,4 +8,7 @@ pub enum Error { #[error("Parsing error: {0}")] Parse(#[from] std::str::Utf8Error), + + #[error("Session not found")] + NotFound, } |