From 4e9cbb01f1cae51735047c239a044b6b0264cf7a Mon Sep 17 00:00:00 2001 From: Parfii-bot Date: Tue, 21 Apr 2026 04:12:11 +0800 Subject: [PATCH] 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. --- install.sh | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/install.sh b/install.sh index fdf4d4d..fa51d55 100755 --- a/install.sh +++ b/install.sh @@ -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 ;;