kaeru 0.1.3

Reopens your terminal tabs, and the coding agent sessions in them, after the terminal closes or the computer restarts.
//! kaeru reopens terminal tabs after the terminal closes or the computer
//! restarts. docs/architecture.md describes how the pieces fit together.

mod agents;
mod engine;
mod install;
mod os;
mod procs;
mod shells;
mod store;
mod terminals;
mod tui;

use std::process::ExitCode;

use anyhow::Result;
use clap::{Parser, Subcommand};

/// The command line. Its help text is the description in `Cargo.toml`.
#[derive(Parser)]
#[command(name = "kaeru", version, about)]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
}

/// The commands. Their doc comments are the `--help` text.
#[derive(Subcommand)]
enum Command {
    /// Show the tabs kaeru is tracking and the ones waiting to come back
    List,
    /// Set up the shell snippet and the agent hooks, asking before each change
    Install,
    /// Remove everything `kaeru install` added
    Uninstall,
    /// Check that everything is set up
    Doctor,
    /// Show and change kaeru's options
    Options,
    /// Print the snippet that your shell profile loads
    Init {
        /// The shell to print the snippet for
        #[arg(value_enum)]
        shell: shells::Shell,
    },
    /// Called by the shell snippet, not by you
    #[command(hide = true)]
    Shell {
        #[command(subcommand)]
        event: ShellEvent,
    },
    /// Resume this tab's agent session, after the terminal restored the tab
    Resume,
    /// Called by an agent's hook, not by you
    #[command(hide = true)]
    Hook {
        /// The agent whose hook is calling
        #[arg(value_enum)]
        agent: agents::Agent,
    },
}

/// What the shell snippet reports.
#[derive(Subcommand)]
enum ShellEvent {
    /// Record a shell that started in a tab
    Start {
        /// The shell's process id
        #[arg(long)]
        pid: u32,
        /// The shell's parent, when the OS doesn't record it
        #[arg(long)]
        parent: Option<u32>,
    },
}

/// Runs the command given on the command line, and prints its error if it
/// fails.
fn main() -> ExitCode {
    match run(Cli::parse()) {
        Ok(code) => code,
        Err(error) => {
            eprintln!("kaeru: {error:#}");
            ExitCode::FAILURE
        }
    }
}

/// Runs one command.
fn run(cli: Cli) -> Result<ExitCode> {
    match cli.command {
        None => tui::pick(),
        Some(Command::List) => tui::list().map(|()| ExitCode::SUCCESS),
        Some(Command::Install) => install::install().map(|()| ExitCode::SUCCESS),
        Some(Command::Uninstall) => install::uninstall().map(|()| ExitCode::SUCCESS),
        Some(Command::Doctor) => install::doctor(),
        Some(Command::Options) => tui::options().map(|()| ExitCode::SUCCESS),
        Some(Command::Resume) => tui::resume(),
        Some(Command::Init { shell }) => shells::print_init(shell).map(|()| ExitCode::SUCCESS),
        Some(Command::Shell {
            event: ShellEvent::Start { pid, parent },
        }) => Ok(shells::start(pid, parent)),
        Some(Command::Hook { agent }) => agents::run_hook(agent).map(|()| ExitCode::SUCCESS),
    }
}