Skip to main content

BASH_HOOK

Constant BASH_HOOK 

Source
pub const BASH_HOOK: &str = r#"
# rec shell hooks for Bash
# Add to ~/.bashrc: eval "$(rec init bash)"

# Shell wrapper: automatically manages REC_RECORDING for start/stop
rec() {
    if [[ "${1:-}" == "start" ]]; then
        command rec "$@" && export REC_RECORDING=1
    elif [[ "${1:-}" == "stop" ]]; then
        command rec "$@" && unset REC_RECORDING
    else
        command rec "$@"
    fi
}

# Source bash-preexec if not already loaded
if [[ -z "${bash_preexec_imported:-}" ]]; then
    # bash-preexec is output first by rec init bash
    :
fi

# Capture command before execution
__rec_preexec() {
    # $1 is the command (aliases expanded by bash-preexec)
    if [[ -n "${REC_RECORDING:-}" ]]; then
        rec _hook preexec "$1" 2>/dev/null || true
    fi
}

# Capture exit code after command completes
__rec_precmd() {
    local exit_code=$?
    if [[ -n "${REC_RECORDING:-}" ]]; then
        rec _hook precmd "$exit_code" 2>/dev/null || true
    fi
}

# Register hooks with bash-preexec
preexec_functions+=(__rec_preexec)
precmd_functions+=(__rec_precmd)

# Recording indicator for prompt (optional, disable with REC_NO_PROMPT=1)
__rec_prompt_indicator() {
    if [[ -n "${REC_RECORDING:-}" && -z "${REC_NO_PROMPT:-}" ]]; then
        printf '\[\e[31m\]● \[\e[0m\]'
    fi
}

# Prepend indicator to PS1 if not disabled
if [[ -z "${REC_NO_PROMPT:-}" ]]; then
    PS1='$(__rec_prompt_indicator)'"${PS1}"
fi
"#;
Expand description

Bash hook script.

Requires bash-preexec to be sourced first. Registers preexec/precmd functions that call rec _hook for command capture.