mirror of
https://github.com/nvm-sh/nvm.git
synced 2026-04-03 11:34:50 +08:00
[Fix] nvm alias: fix colors not showing by default
Colors were lost because `nvm_has_colors` checks `[ -t 1 ]`, which is false inside the `(...) | sort` pipeline in `nvm_list_aliases`.
Evaluate `nvm_has_colors` before the pipe and propagate via `NVM_HAS_COLORS`, matching the approach used by `nvm_print_versions`.
Bug introduced in 35212c1346.
This commit is contained in:
14
nvm.sh
14
nvm.sh
@@ -1153,7 +1153,7 @@ nvm_print_formatted_alias() {
|
||||
fi
|
||||
local ARROW
|
||||
ARROW='->'
|
||||
if nvm_has_colors; then
|
||||
if [ "${NVM_HAS_COLORS-}" = 1 ] || nvm_has_colors; then
|
||||
ARROW='\033[0;90m->\033[0m'
|
||||
if [ "_${DEFAULT}" = '_true' ]; then
|
||||
NEWLINE=" \033[${DEFAULT_COLOR}(default)\033[0m\n"
|
||||
@@ -1254,11 +1254,17 @@ nvm_list_aliases() {
|
||||
return $?
|
||||
fi
|
||||
|
||||
local NVM_HAS_COLORS
|
||||
NVM_HAS_COLORS=0
|
||||
if nvm_has_colors; then
|
||||
NVM_HAS_COLORS=1
|
||||
fi
|
||||
|
||||
nvm_is_zsh && unsetopt local_options nomatch
|
||||
(
|
||||
local ALIAS_PATH
|
||||
for ALIAS_PATH in "${NVM_ALIAS_DIR}/${ALIAS}"*; do
|
||||
NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}" &
|
||||
NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_HAS_COLORS="${NVM_HAS_COLORS}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}" &
|
||||
done
|
||||
wait
|
||||
) | command sort
|
||||
@@ -1269,7 +1275,7 @@ nvm_list_aliases() {
|
||||
{
|
||||
# shellcheck disable=SC2030,SC2031 # (https://github.com/koalaman/shellcheck/issues/2217)
|
||||
if [ ! -f "${NVM_ALIAS_DIR}/${ALIAS_NAME}" ] && { [ -z "${ALIAS}" ] || [ "${ALIAS_NAME}" = "${ALIAS}" ]; }; then
|
||||
NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_default_alias "${ALIAS_NAME}"
|
||||
NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_HAS_COLORS="${NVM_HAS_COLORS}" NVM_CURRENT="${NVM_CURRENT}" nvm_print_default_alias "${ALIAS_NAME}"
|
||||
fi
|
||||
} &
|
||||
done
|
||||
@@ -1281,7 +1287,7 @@ nvm_list_aliases() {
|
||||
# shellcheck disable=SC2030,SC2031 # (https://github.com/koalaman/shellcheck/issues/2217)
|
||||
for ALIAS_PATH in "${NVM_ALIAS_DIR}/lts/${ALIAS}"*; do
|
||||
{
|
||||
LTS_ALIAS="$(NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_LTS=true nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}")"
|
||||
LTS_ALIAS="$(NVM_NO_COLORS="${NVM_NO_COLORS-}" NVM_HAS_COLORS="${NVM_HAS_COLORS}" NVM_LTS=true nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}")"
|
||||
if [ -n "${LTS_ALIAS}" ]; then
|
||||
nvm_echo "${LTS_ALIAS}"
|
||||
fi
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#!/bin/sh
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
\. ../../common.sh
|
||||
|
||||
die () { echo "$@" ; exit 1; }
|
||||
|
||||
set -e
|
||||
|
||||
# Force nvm_has_colors to return true so nvm_list_aliases sets NVM_HAS_COLORS=1
|
||||
nvm_has_colors() { return 0; }
|
||||
|
||||
nvm_alias_path() {
|
||||
nvm_echo "../../../alias"
|
||||
}
|
||||
|
||||
# nvm_list_aliases pipes through `sort`, which makes [ -t 1 ] false.
|
||||
# Before the fix in 2eb8bbd0, colors were lost inside the pipeline.
|
||||
# With the fix, NVM_HAS_COLORS is evaluated before the pipe and propagated.
|
||||
OUTPUT=$(nvm_list_aliases test-stable-1)
|
||||
COLORED_ARROW="$(command printf %b '\033[0;90m->\033[0m')"
|
||||
|
||||
case "${OUTPUT}" in
|
||||
*"${COLORED_ARROW}"*) : ;; # pass - colored arrow survived the pipeline
|
||||
*) die "Expected colored arrow in nvm_list_aliases output through pipeline, got >${OUTPUT}<" ;;
|
||||
esac
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/bin/sh
|
||||
|
||||
: nvm.sh
|
||||
\. ../../../nvm.sh
|
||||
|
||||
die () {
|
||||
echo "$@"
|
||||
exit 1
|
||||
}
|
||||
|
||||
set -e
|
||||
|
||||
# Override nvm_has_colors to always return false, simulating a pipeline context
|
||||
# where [ -t 1 ] is false (the bug condition from 2eb8bbd0)
|
||||
nvm_has_colors() { return 1; }
|
||||
|
||||
# Without NVM_HAS_COLORS, output should have no color codes (plain arrow)
|
||||
OUTPUT="$(nvm_print_formatted_alias fakealias fakedest)"
|
||||
case "${OUTPUT}" in
|
||||
*'\033['*) die "Expected no color codes without NVM_HAS_COLORS, got >${OUTPUT}<" ;;
|
||||
esac
|
||||
|
||||
# With NVM_HAS_COLORS=1, output should contain color codes even though
|
||||
# nvm_has_colors returns false (regression test for the pipeline fix)
|
||||
NVM_HAS_COLORS=1
|
||||
export NVM_HAS_COLORS
|
||||
OUTPUT="$(nvm_print_formatted_alias fakealias fakedest)"
|
||||
ARROW="$(command printf %b '\033[0;90m->\033[0m')"
|
||||
case "${OUTPUT}" in
|
||||
*"${ARROW}"*) : ;; # pass - colored arrow present
|
||||
*) die "Expected colored arrow with NVM_HAS_COLORS=1, got >${OUTPUT}<" ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user