[Refactor] add `nvm_wrap_with_color_code`; allow no color code
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
15 files+76 added−95 removed
- tests:+1 added−0 removed
- other:+75 added−95 removed
Provenance
- Commit
df01d81- Tree
1318838- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
0ccd099
15 files changed+76 added−95 removed
This commit is too large to show whole: its files are listed by path, without their changes.
modified:test/fast/Unit tests/mocks/nodejs.org-dist-index.tab+1 added
1 version date files npm v8 uv zlib openssl modules lts security2+v18.11.0 2022-10-13 aix-ppc64,headers,linux-arm64,linux-armv7l,linux-ppc64le,linux-s390x,linux-x64,osx-arm64-tar,osx-x64-pkg,osx-x64-tar,src,win-x64-7z,win-x64-exe,win-x64-msi,win-x64-zip,win-x86-7z,win-x86-exe,win-x86-msi,win-x86-zip 8.19.2 10.2.154.15 1.43.0 1.2.11 3.0.5+quic 108 - -3 v18.10.0 2022-09-28 aix-ppc64,headers,linux-arm64,linux-armv7l,linux-ppc64le,linux-s390x,linux-x64,osx-arm64-tar,osx-x64-pkg,osx-x64-tar,src,win-x64-7z,win-x64-exe,win-x64-msi,win-x64-zip,win-x86-7z,win-x86-exe,win-x86-msi,win-x86-zip 8.19.2 10.2.154.15 1.43.0 1.2.11 3.0.5+quic 108 - -4 v18.9.1 2022-09-23 aix-ppc64,headers,linux-arm64,linux-armv7l,linux-ppc64le,linux-s390x,linux-x64,osx-arm64-tar,osx-x64-pkg,osx-x64-tar,src,win-x64-7z,win-x64-exe,win-x64-msi,win-x64-zip,win-x86-7z,win-x86-exe,win-x86-msi,win-x86-zip 8.19.1 10.2.154.15 1.43.0 1.2.11 3.0.5+quic 108 - true5 v18.9.0 2022-09-07 aix-ppc64,headers,linux-arm64,linux-armv7l,linux-ppc64le,linux-s390x,linux-x64,osx-arm64-tar,osx-x64-pkg,osx-x64-tar,src,win-x64-7z,win-x64-exe,win-x64-msi,win-x64-zip,win-x86-7z,win-x86-exe,win-x86-msi,win-x86-zip 8.19.1 10.2.154.15 1.43.0 1.2.11 3.0.5+quic 108 - -6 v18.8.0 2022-08-24 aix-ppc64,headers,linux-arm64,linux-armv7l,linux-ppc64le,linux-s390x,linux-x64,osx-arm64-tar,osx-x64-pkg,osx-x64-tar,src,win-x64-7z,win-x64-exe,win-x64-msi,win-x64-zip,win-x86-7z,win-x86-exe,win-x86-msi,win-x86-zip 8.18.0 10.2.154.13 1.43.0 1.2.11 3.0.5+quic 108 - -653 unmodified linesmodified:test/fast/Unit tests/mocks/nodejs.org-download-nightly-index.tabnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm ls-remote iojs.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm ls-remote lts.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm ls-remote node.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm ls-remote.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm_ls_remote nightly.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm_ls_remote stable nightly.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm_ls_remote stable.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm_ls_remote.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/mocks/nvm_print_implicit_alias remote stable.txtnot shown
Its text is not shown, modified.
modified:test/fast/Unit tests/nvm_print_color_codenot shown
Its text is not shown, modified.
added:test/fast/Unit tests/nvm_wrap_with_color_codenot shown
Its text is not shown, added.
modified:nvm.sh+75 added-95 removed
840 unmodified lines841 if ! nvm_has_colors; then842 nvm_echo "Setting colors to: ${INSTALLED_COLOR} ${LTS_AND_SYSTEM_COLOR} ${CURRENT_COLOR} ${NOT_INSTALLED_COLOR} ${DEFAULT_COLOR}"843 nvm_echo "WARNING: Colors may not display because they are not supported in this shell."844 else845- nvm_echo_with_colors "Setting colors to: \033[$(nvm_print_color_code "${INSTALLED_COLOR}") ${INSTALLED_COLOR}\033[$(nvm_print_color_code "${LTS_AND_SYSTEM_COLOR}") ${LTS_AND_SYSTEM_COLOR}\033[$(nvm_print_color_code "${CURRENT_COLOR}") ${CURRENT_COLOR}\033[$(nvm_print_color_code "${NOT_INSTALLED_COLOR}") ${NOT_INSTALLED_COLOR}\033[$(nvm_print_color_code "${DEFAULT_COLOR}") ${DEFAULT_COLOR}\033[0m"845+ nvm_echo_with_colors "Setting colors to: $(nvm_wrap_with_color_code "${INSTALLED_COLOR}" "${INSTALLED_COLOR}")$(nvm_wrap_with_color_code "${LTS_AND_SYSTEM_COLOR}" "${LTS_AND_SYSTEM_COLOR}")$(nvm_wrap_with_color_code "${CURRENT_COLOR}" "${CURRENT_COLOR}")$(nvm_wrap_with_color_code "${NOT_INSTALLED_COLOR}" "${NOT_INSTALLED_COLOR}")$(nvm_wrap_with_color_code "${DEFAULT_COLOR}" "${DEFAULT_COLOR}")"846 fi847 export NVM_COLORS="$1"848 else849 return 172 unmodified lines852 853 nvm_get_colors() {854 local COLOR855 local SYS_COLOR856- if [ -n "${NVM_COLORS-}" ]; then857- case $1 in858- 1) COLOR=$(nvm_print_color_code "$(echo "$NVM_COLORS" | awk '{ print substr($0, 1, 1); }')");;859- 2) COLOR=$(nvm_print_color_code "$(echo "$NVM_COLORS" | awk '{ print substr($0, 2, 1); }')");;860- 3) COLOR=$(nvm_print_color_code "$(echo "$NVM_COLORS" | awk '{ print substr($0, 3, 1); }')");;861- 4) COLOR=$(nvm_print_color_code "$(echo "$NVM_COLORS" | awk '{ print substr($0, 4, 1); }')");;862- 5) COLOR=$(nvm_print_color_code "$(echo "$NVM_COLORS" | awk '{ print substr($0, 5, 1); }')");;863- 6)864- SYS_COLOR=$(nvm_print_color_code "$(echo "$NVM_COLORS" | awk '{ print substr($0, 2, 1); }')")865- COLOR=$(nvm_echo "$SYS_COLOR" | command tr '0;' '1;')866- ;;867- *)868- nvm_err "Invalid color index, ${1-}"869- return 1856+ local COLORS857+ COLORS="${NVM_COLORS:-bygre}"858+ case $1 in859+ 1) COLOR=$(nvm_print_color_code "$(echo "$COLORS" | awk '{ print substr($0, 1, 1); }')");;860+ 2) COLOR=$(nvm_print_color_code "$(echo "$COLORS" | awk '{ print substr($0, 2, 1); }')");;861+ 3) COLOR=$(nvm_print_color_code "$(echo "$COLORS" | awk '{ print substr($0, 3, 1); }')");;862+ 4) COLOR=$(nvm_print_color_code "$(echo "$COLORS" | awk '{ print substr($0, 4, 1); }')");;863+ 5) COLOR=$(nvm_print_color_code "$(echo "$COLORS" | awk '{ print substr($0, 5, 1); }')");;864+ 6)865+ SYS_COLOR=$(nvm_print_color_code "$(echo "$COLORS" | awk '{ print substr($0, 2, 1); }')")866+ COLOR=$(nvm_echo "$SYS_COLOR" | command tr '0;' '1;')867 ;;868+ *)869+ nvm_err "Invalid color index, ${1-}"870+ return 1871+ ;;871- esac872+ esac873+874+ nvm_echo "$COLOR"875+}876+877+nvm_wrap_with_color_code() {878+ local CODE879+ CODE="$(nvm_print_color_code "${1}" 2>/dev/null ||:)"880+ local TEXT881+ TEXT="${2-}"882+ if nvm_has_colors && [ -n "${CODE}" ]; then883+ nvm_echo_with_colors "\033[${CODE}${TEXT}\033[0m"884 else873- case $1 in874- 1) COLOR='0;34m';;875- 2) COLOR='0;33m';;876- 3) COLOR='0;32m';;877- 4) COLOR='0;31m';;878- 5) COLOR='0;37m';;879- 6) COLOR='1;33m';;880- *)881- nvm_err "Invalid color index, ${1-}"882- return 1883- ;;884- esac885+ nvm_echo "${TEXT}"886 fi887+}888 887- echo "$COLOR"889+nvm_wrap_with_color_code() {890+ local CODE891+ CODE="$(nvm_print_color_code "${1}" 2>/dev/null ||:)"892+ local TEXT893+ TEXT="${2-}"894+ if nvm_has_colors && [ -n "${CODE}" ]; then895+ nvm_echo_with_colors "\033[${CODE}${TEXT}\033[0m"896+ else897+ nvm_echo "${TEXT}"898+ fi899 }900 901 nvm_print_color_code() {902 case "${1-}" in903+ '0') return 0 ;;892- 'r') nvm_echo '0;31m';;893- 'R') nvm_echo '1;31m';;894- 'g') nvm_echo '0;32m';;895- 'G') nvm_echo '1;32m';;896- 'b') nvm_echo '0;34m';;897- 'B') nvm_echo '1;34m';;898- 'c') nvm_echo '0;36m';;899- 'C') nvm_echo '1;36m';;900- 'm') nvm_echo '0;35m';;901- 'M') nvm_echo '1;35m';;902- 'y') nvm_echo '0;33m';;903- 'Y') nvm_echo '1;33m';;904- 'k') nvm_echo '0;30m';;905- 'K') nvm_echo '1;30m';;906- 'e') nvm_echo '0;37m';;907- 'W') nvm_echo '1;37m';;908- *) nvm_err 'Invalid color code';909- return 1904+ 'r') nvm_echo '0;31m' ;;905+ 'R') nvm_echo '1;31m' ;;906+ 'g') nvm_echo '0;32m' ;;907+ 'G') nvm_echo '1;32m' ;;908+ 'b') nvm_echo '0;34m' ;;909+ 'B') nvm_echo '1;34m' ;;910+ 'c') nvm_echo '0;36m' ;;911+ 'C') nvm_echo '1;36m' ;;912+ 'm') nvm_echo '0;35m' ;;913+ 'M') nvm_echo '1;35m' ;;914+ 'y') nvm_echo '0;33m' ;;915+ 'Y') nvm_echo '1;33m' ;;916+ 'k') nvm_echo '0;30m' ;;917+ 'K') nvm_echo '1;30m' ;;918+ 'e') nvm_echo '0;37m' ;;919+ 'W') nvm_echo '1;37m' ;;920+ *)921+ nvm_err "Invalid color code: ${1-}";922+ return 1923 ;;924 esac925 }926 784 unmodified lines1711 -v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \1712 -v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \1713 -v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" '1714 BEGIN {1702- fmt_installed = has_colors ? ("\033[" installed_color "%15s\033[0m") : "%15s *";1703- fmt_system = has_colors ? ("\033[" system_color "%15s\033[0m") : "%15s *";1704- fmt_current = has_colors ? ("\033[" current_color "->%13s\033[0m") : "->%13s *";1715+ fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";1716+ fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";1717+ fmt_current = has_colors ? (current_color ? "\033[" current_color "->%13s\033[0m" : "%15s") : "->%13s *";1718 1719 latest_lts_color = current_color;1720 sub(/0;/, "1;", latest_lts_color);1721 1709- fmt_latest_lts = has_colors ? ("\033[" latest_lts_color " (Latest LTS: %s)\033[0m") : " (Latest LTS: %s)";1710- fmt_old_lts = has_colors ? ("\033[" old_lts_color " (LTS: %s)\033[0m") : " (LTS: %s)";1722+ fmt_latest_lts = has_colors && latest_lts_color ? ("\033[" latest_lts_color " (Latest LTS: %s)\033[0m") : " (Latest LTS: %s)";1723+ fmt_old_lts = has_colors && old_lts_color ? ("\033[" old_lts_color " (LTS: %s)\033[0m") : " (LTS: %s)";1724 1725 split(remote_versions, lines, "|");1726 split(installed_versions, installed, "|");1727 rows = length(lines);1095 unmodified lines2823 break2824 fi2825 done2826 2814- local INITIAL_COLOR_INFO2815- local RED_INFO2816- local GREEN_INFO2817- local BLUE_INFO2818- local CYAN_INFO2819- local MAGENTA_INFO2820- local YELLOW_INFO2821- local BLACK_INFO2822- local GREY_WHITE_INFO2823-2824- if [ -z "${NVM_NO_COLORS-}" ] && nvm_has_colors; then2825- INITIAL_COLOR_INFO='\033[0;34m b\033[0m \033[0;34m y\033[0m \033[0;32m g\033[0m \033[0;31m r\033[0m \033[0;37m e\033[0m'2826- RED_INFO='\033[0;31m r\033[0m/\033[1;31mR\033[0m = \033[0;31mred\033[0m / \033[1;31mbold red\033[0m'2827- GREEN_INFO='\033[0;32m g\033[0m/\033[1;32mG\033[0m = \033[0;32mgreen\033[0m / \033[1;32mbold green\033[0m'2828- BLUE_INFO='\033[0;34m b\033[0m/\033[1;34mB\033[0m = \033[0;34mblue\033[0m / \033[1;34mbold blue\033[0m'2829- CYAN_INFO='\033[0;36m c\033[0m/\033[1;36mC\033[0m = \033[0;36mcyan\033[0m / \033[1;36mbold cyan\033[0m'2830- MAGENTA_INFO='\033[0;35m m\033[0m/\033[1;35mM\033[0m = \033[0;35mmagenta\033[0m / \033[1;35mbold magenta\033[0m'2831- YELLOW_INFO='\033[0;33m y\033[0m/\033[1;33mY\033[0m = \033[0;33myellow\033[0m / \033[1;33mbold yellow\033[0m'2832- BLACK_INFO='\033[0;30m k\033[0m/\033[1;30mK\033[0m = \033[0;30mblack\033[0m / \033[1;30mbold black\033[0m'2833- GREY_WHITE_INFO='\033[0;37m e\033[0m/\033[1;37mW\033[0m = \033[0;37mlight grey\033[0m / \033[1;37mwhite\033[0m'2834- else2835- INITIAL_COLOR_INFO='bygre'2836- RED_INFO='r/R = red / bold red'2837- GREEN_INFO='g/G = green / bold green'2838- BLUE_INFO='b/B = blue / bold blue'2839- CYAN_INFO='c/C = cyan / bold cyan'2840- MAGENTA_INFO='m/M = magenta / bold magenta'2841- YELLOW_INFO='y/Y = yellow / bold yellow'2842- BLACK_INFO='k/K = black / bold black'2843- GREY_WHITE_INFO='e/W = light grey / white'2844- fi2845-2827 local NVM_IOJS_PREFIX2828 NVM_IOJS_PREFIX="$(nvm_iojs_prefix)"2829 local NVM_NODE_PREFIX2830 NVM_NODE_PREFIX="$(nvm_node_prefix)"68 unmodified lines2899 nvm_echo ' nvm cache dir Display path to the cache directory for nvm'2900 nvm_echo ' nvm cache clear Empty cache directory for nvm'2901 nvm_echo ' nvm set-colors [<color codes>] Set five text colors using format "yMeBg". Available when supported.'2902 nvm_echo ' Initial colors are:'2922- nvm_echo_with_colors " ${INITIAL_COLOR_INFO}"2903+ nvm_echo_with_colors " $(nvm_wrap_with_color_code b b)$(nvm_wrap_with_color_code y y)$(nvm_wrap_with_color_code g g)$(nvm_wrap_with_color_code r r)$(nvm_wrap_with_color_code e e)"2904 nvm_echo ' Color codes:'2924- nvm_echo_with_colors " ${RED_INFO}"2925- nvm_echo_with_colors " ${GREEN_INFO}"2926- nvm_echo_with_colors " ${BLUE_INFO}"2927- nvm_echo_with_colors " ${CYAN_INFO}"2928- nvm_echo_with_colors " ${MAGENTA_INFO}"2929- nvm_echo_with_colors " ${YELLOW_INFO}"2930- nvm_echo_with_colors " ${BLACK_INFO}"2931- nvm_echo_with_colors " ${GREY_WHITE_INFO}"2932- nvm_echo2905+ nvm_echo_with_colors " $(nvm_wrap_with_color_code r r)/$(nvm_wrap_with_color_code R R) = $(nvm_wrap_with_color_code r red) / $(nvm_wrap_with_color_code R 'bold red')"2906+ nvm_echo_with_colors " $(nvm_wrap_with_color_code g g)/$(nvm_wrap_with_color_code G G) = $(nvm_wrap_with_color_code g green) / $(nvm_wrap_with_color_code G 'bold green')"2907+ nvm_echo_with_colors " $(nvm_wrap_with_color_code b b)/$(nvm_wrap_with_color_code B B) = $(nvm_wrap_with_color_code b blue) / $(nvm_wrap_with_color_code B 'bold blue')"2908+ nvm_echo_with_colors " $(nvm_wrap_with_color_code c c)/$(nvm_wrap_with_color_code C C) = $(nvm_wrap_with_color_code c cyan) / $(nvm_wrap_with_color_code C 'bold cyan')"2909+ nvm_echo_with_colors " $(nvm_wrap_with_color_code m m)/$(nvm_wrap_with_color_code M M) = $(nvm_wrap_with_color_code m magenta) / $(nvm_wrap_with_color_code M 'bold magenta')"2910+ nvm_echo_with_colors " $(nvm_wrap_with_color_code y y)/$(nvm_wrap_with_color_code Y Y) = $(nvm_wrap_with_color_code y yellow) / $(nvm_wrap_with_color_code Y 'bold yellow')"2911+ nvm_echo_with_colors " $(nvm_wrap_with_color_code k k)/$(nvm_wrap_with_color_code K K) = $(nvm_wrap_with_color_code k black) / $(nvm_wrap_with_color_code K 'bold black')"2912+ nvm_echo_with_colors " $(nvm_wrap_with_color_code e e)/$(nvm_wrap_with_color_code W W) = $(nvm_wrap_with_color_code e 'light grey') / $(nvm_wrap_with_color_code W white)"2913 nvm_echo 'Example:'2914 nvm_echo ' nvm install 8.0.0 Install a specific version number'2915 nvm_echo ' nvm use 8.0 Use the latest available 8.0.x release'2916 nvm_echo ' nvm run 6.10.3 app.js Run app.js using node 6.10.3'1281 unmodified lines4198 nvm_sanitize_path nvm_has_colors nvm_process_parameters \4199 nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \4200 nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \4201 nvm_npmrc_bad_news_bears \4222- nvm_get_colors nvm_set_colors nvm_print_color_code nvm_format_help_message_colors \4202+ nvm_get_colors nvm_set_colors nvm_print_color_code nvm_wrap_with_color_code nvm_format_help_message_colors \4203 nvm_echo_with_colors nvm_err_with_colors \4204 nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \4205 >/dev/null 2>&14206 unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \173 unmodified linesmodified:update_test_mocks.shnot shown
Its text is not shown, modified.