tmux-tango 2.7.6

A CLI tool for managing tmux sessions - dance between your sessions!
Documentation
//! Runtime-only tmux key bindings installed for the lifetime of a tango process.
//!
//! Installs session-scoped Alt-Left detach bindings via `tmux bind-key -n` and
//! removes them on drop. The bindings are gated on a per-session user option
//! (`@tango_active`); only sessions tango has marked active will detach on press.
//! All other sessions pass the key through to the active pane unchanged.
//!
//! Two bindings are installed because macOS terminals (Terminal.app, iTerm2, Ghostty
//! with default Option behavior) rewrite the physical Option+Left key to `ESC b`
//! (`M-b`) — the readline backward-word sequence — instead of the CSI modified-arrow
//! sequence tmux's `M-Left` matches. Binding both forms covers either terminal mode.

use std::process::{Command, Stdio};

const ACTIVE_FLAG: &str = "@tango_active";
const BINDING_COND: &str = "#{@tango_active}";
const BINDING_THEN: &str = "detach-client";

/// Keys we bind, paired with their `send-keys` passthrough form for the else branch.
/// `M-Left` — terminals that send the CSI modified-arrow sequence for Alt+Left.
/// `M-b`    — macOS terminals that rewrite Alt+Left to the readline backward-word
///            sequence (`ESC b`). In non-tango sessions this is forwarded so shells
///            still get word-back.
const BINDINGS: &[(&str, &str)] = &[
    ("M-Left", "send-keys M-Left"),
    ("M-b",    "send-keys M-b"),
];

fn server_running() -> bool {
    Command::new("tmux")
        .arg("info")
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

fn run_tmux(args: &[&str]) {
    let _ = Command::new("tmux")
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
}

/// Install the detach bindings on the running tmux server.
/// No-op when no server is running yet — call again after attaching to one.
pub fn install_binding() {
    if !server_running() {
        return;
    }
    for (key, passthrough) in BINDINGS {
        run_tmux(&[
            "bind-key", "-n", key,
            "if-shell", "-F", BINDING_COND,
            BINDING_THEN, passthrough,
        ]);
    }
}

/// Remove tango's detach bindings. Best-effort: silently ignores errors and absent servers.
pub fn unbind_binding() {
    if !server_running() {
        return;
    }
    for (key, _) in BINDINGS {
        run_tmux(&["unbind-key", "-n", key]);
    }
}

/// Mark `session` as a tango-managed attachment so the binding fires there.
pub fn set_active(session: &str) {
    run_tmux(&["set-option", "-t", session, ACTIVE_FLAG, "1"]);
}

/// Clear the tango-active flag on `session`. Called after the attach returns.
pub fn clear_active(session: &str) {
    run_tmux(&["set-option", "-t", session, ACTIVE_FLAG, "0"]);
}

/// RAII guard: installs the binding on construction and unbinds it on drop.
/// Drop is best-effort — a panic or kill mid-attach will still run the unbind.
pub struct BindingGuard;

impl BindingGuard {
    pub fn new() -> Self {
        install_binding();
        BindingGuard
    }
}

impl Drop for BindingGuard {
    fn drop(&mut self) {
        unbind_binding();
    }
}