tmux-tango 2.13.0

A CLI tool for managing tmux sessions - dance between your sessions!
Documentation
use anyhow::Result;
use clap::{Parser, Subcommand};

mod attach;
mod config;
mod error;
mod hooks;
mod host;
mod remote;
mod status;
mod tmux;
mod tmux_bindings;
mod interactive;

use error::TmuxFzfError;
use tmux::TmuxClient;
use interactive::InteractiveSelector;

#[derive(Parser)]
#[command(name = "tango")]
#[command(about = "A CLI tool for managing tmux sessions - dance between your sessions!")]
#[command(version)]
struct Cli {
    /// Remote ssh host alias to show in the TUI (repeatable; adds to config.toml hosts)
    #[arg(long = "host", value_name = "ALIAS")]
    host: Vec<String>,

    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// List all tmux sessions
    List,
    /// Attach to a session
    Attach {
        /// Session to attach to, optionally with a window index (`name:2`)
        session: String,
    },
    /// Kill a session
    Kill {
        /// Session name to kill
        session: String,
    },
    /// Create a new session
    New {
        /// Optional session name
        name: Option<String>,
    },
    /// Rename a session
    Rename {
        /// Current session name
        old_name: String,
        /// New session name
        new_name: String,
    },
    /// Manage Claude Code hooks for status monitoring
    Hooks {
        #[command(subcommand)]
        action: HooksAction,
    },
    /// Manage tango's runtime tmux key bindings
    Tmux {
        #[command(subcommand)]
        action: TmuxAction,
    },
}

#[derive(Subcommand)]
enum HooksAction {
    /// Install Claude Code hooks for pane status detection
    Install,
    /// Remove TmuxTango hooks from Claude Code settings
    Uninstall,
    /// Show hook installation status
    Status,
}

#[derive(Subcommand)]
enum TmuxAction {
    /// Remove any tango-installed key bindings from the running tmux server.
    /// Use this if tango crashed before it could clean up after itself.
    Cleanup,
}

fn check_dependencies() -> Result<(), TmuxFzfError> {
    which::which("tmux")
        .map_err(|_| TmuxFzfError::DependencyMissing("tmux".to_string()))?;

    Ok(())
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    check_dependencies()?;

    if let Err(e) = run_cli(&cli) {
        match e {
            TmuxFzfError::UserCancelled => {
                std::process::exit(0);
            }
            _ => {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
        }
    }

    Ok(())
}

fn run_cli(cli: &Cli) -> Result<(), TmuxFzfError> {
    let mut tmux_client = TmuxClient::new();

    match &cli.command {
        Some(Commands::List) => {
            let sessions = tmux_client.list_sessions()?;
            if sessions.is_empty() {
                println!("No tmux sessions found");
            } else {
                for session in sessions {
                    let status = if session.is_attached() { " (attached)" } else { "" };
                    println!("{}: {} windows{}", session.name, session.windows, status);
                }
            }
        },
        Some(Commands::Attach { session }) => {
            // Same ceremony as the TUI: viewer grouping and the Alt-Left detach binding. This
            // is the path a remote tango runs under `ssh -t host tango attach <s>`.
            let _binding_guard = tmux_bindings::BindingGuard::new();
            let (name, window) = attach::parse_attach_target(session)?;
            attach::attach_managed(&mut tmux_client, &name, window, None)?;
        },
        Some(Commands::Kill { session }) => {
            tmux_client.kill_session(session)?;
            println!("Killed session '{}'", session);
        },
        Some(Commands::New { name }) => {
            let session_name = tmux_client.new_session(name.as_deref())?;
            if !tmux_client.is_inside_tmux() {
                tmux_client.attach_session(&session_name)?;
            }
            println!("Created session '{}'", session_name);
        },
        Some(Commands::Rename { old_name, new_name }) => {
            tmux_client.rename_session(old_name, new_name)?;
            println!("Renamed session '{}' to '{}'", old_name, new_name);
        },
        Some(Commands::Hooks { action }) => {
            let result = match action {
                HooksAction::Install => hooks::install(),
                HooksAction::Uninstall => hooks::uninstall(),
                HooksAction::Status => hooks::status(),
            };
            result.map_err(|e| TmuxFzfError::TmuxError(e.to_string()))?;
        },
        Some(Commands::Tmux { action }) => match action {
            TmuxAction::Cleanup => {
                tmux_bindings::unbind_binding();
                println!("Removed tango's tmux key bindings (if present).");
            }
        },
        None => {
            for alias in &cli.host {
                config::validate_alias(alias)?;
            }
            let hosts = config::merge_hosts(&config::load()?.hosts, &cli.host);
            hooks::ensure_installed();
            let _binding_guard = tmux_bindings::BindingGuard::new();
            let mut selector = InteractiveSelector::new(hosts);
            selector.run()?;
        }
    }

    Ok(())
}