lowfat 0.1.0

CLI binary for lowfat
use anyhow::Result;

/// Print shell init script for eval.
/// Usage: eval "$(lowfat shell-init zsh)"
pub fn run(shell: &str) -> Result<()> {
    match shell {
        "zsh" | "bash" => print_posix_init(),
        "fish" => print_fish_init(),
        _ => {
            eprintln!("lowfat: unsupported shell: {shell} (supported: bash, zsh, fish)");
        }
    }
    Ok(())
}

fn print_posix_init() {
    // Auto-wrap: create shell functions for each known command
    // These shadow the real commands and pipe through lowfat
    let commands = [
        "git", "docker", "cargo", "go", "npm", "yarn", "pnpm", "pip", "uv",
        "pytest", "kubectl", "ls", "curl", "wget",
    ];

    println!("# lowfat shell init — auto-wrap commands for LLM token savings");
    println!("# Usage: eval \"$(lowfat shell-init zsh)\"");
    println!();

    // Only auto-wrap inside LLM environments
    println!("if [[ \"$CLAUDECODE\" == \"1\" ]] || [[ -n \"$CODEX_ENV\" ]] || [[ \"$RUNF_AUTO\" == \"1\" ]]; then");

    for cmd in &commands {
        println!("  {cmd}() {{ command lowfat \"${{FUNCNAME[0]:-{cmd}}}\" \"$@\"; }}");
    }

    println!("fi");
    println!();

    // Session summary on exit
    println!("if [[ \"${{RUNF_SESSION:-1}}\" == \"1\" ]]; then");
    println!("  _lowfat_session_exit() {{");
    println!("    command lowfat status 2>/dev/null || true");
    println!("  }}");
    println!("  trap '_lowfat_session_exit' EXIT");
    println!("fi");
}

fn print_fish_init() {
    let commands = [
        "git", "docker", "cargo", "go", "npm", "yarn", "pnpm", "pip", "uv",
        "pytest", "kubectl", "ls", "curl", "wget",
    ];

    println!("# lowfat shell init for fish");
    println!();

    println!("if test \"$CLAUDECODE\" = 1; or test -n \"$CODEX_ENV\"; or test \"$RUNF_AUTO\" = 1");
    for cmd in &commands {
        println!("  function {cmd}; command lowfat {cmd} $argv; end");
    }
    println!("end");
}