Skip to main content

ZSH_HOOK

Constant ZSH_HOOK 

Source
pub const ZSH_HOOK: &str = r#"
# rec shell hooks for Zsh
# Add to ~/.zshrc: eval "$(rec init zsh)"

# 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
}

autoload -Uz add-zsh-hook

# Capture command before execution
# $1 = typed command, $2 = expanded aliases only, $3 = full expansion
__rec_preexec() {
    if [[ -n "${REC_RECORDING:-}" ]]; then
        rec _hook preexec "$3" 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
add-zsh-hook preexec __rec_preexec
add-zsh-hook precmd __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
        print -n '%{\e[31m%}● %{\e[0m%}'
    fi
}

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

Zsh hook script.

Uses native Zsh hooks via add-zsh-hook. Note: $3 in preexec is the fully expanded command (aliases expanded), while $1 is what was typed.