fix(install): respect NO_COLOR and non-TTY output

say/warn/err now detect isatty(1) and the NO_COLOR env convention. When
stdout is redirected to a log file or NO_COLOR is set, ANSI escape codes
are suppressed. Interactive activation prompt also gated.
This commit is contained in:
Parfii-bot 2026-04-21 04:12:11 +08:00
parent ef4d573e81
commit 4e9cbb01f1

View file

@ -28,9 +28,21 @@ EOF
esac
done
say() { printf '\033[1;36m[install]\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m[warn]\033[0m %s\n' "$*"; }
err() { printf '\033[1;31m[error]\033[0m %s\n' "$*" >&2; }
# ANSI on iff stdout is a TTY and NO_COLOR is unset (respect no-color.org).
if [ -t 1 ] && [ "${NO_COLOR:-}" = "" ]; then
COLOR=1
else
COLOR=0
fi
if [ "$COLOR" = "1" ]; then
say() { printf '\033[1;36m[install]\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m[warn]\033[0m %s\n' "$*"; }
err() { printf '\033[1;31m[error]\033[0m %s\n' "$*" >&2; }
else
say() { printf '[install] %s\n' "$*"; }
warn() { printf '[warn] %s\n' "$*"; }
err() { printf '[error] %s\n' "$*" >&2; }
fi
# --- rollback bookkeeping ---------------------------------------------------
# Every successful backup_dir / per-file backup appends a "ORIGINAL|BACKUP"
@ -266,7 +278,11 @@ elif [ ! -f "$SETTINGS_FILE" ]; then
say "no existing settings.json; installing snippet"
activate_hooks && DID_ACTIVATE=1
elif [ -t 0 ] && [ -t 1 ]; then
printf '\033[1;36m[install]\033[0m activate hooks now? [y/N] '
if [ "$COLOR" = "1" ]; then
printf '\033[1;36m[install]\033[0m activate hooks now? [y/N] '
else
printf '[install] activate hooks now? [y/N] '
fi
read -r reply
case "$reply" in
y|Y|yes|YES) activate_hooks && DID_ACTIVATE=1 ;;