diff --git a/nvm.sh b/nvm.sh index d6af5332c4..76fc033ef5 100644 --- a/nvm.sh +++ b/nvm.sh @@ -1642,20 +1642,16 @@ nvm_get_checksum() { } nvm_print_versions() { - local VERSION - local LTS - local FORMAT local NVM_CURRENT - local NVM_LATEST_LTS_COLOR - local NVM_OLD_LTS_COLOR - local INSTALLED_COLOR local SYSTEM_COLOR local CURRENT_COLOR local NOT_INSTALLED_COLOR local DEFAULT_COLOR local LTS_COLOR + local NVM_HAS_COLORS + NVM_CURRENT=$(nvm_ls_current) INSTALLED_COLOR=$(nvm_get_colors 1) SYSTEM_COLOR=$(nvm_get_colors 2) CURRENT_COLOR=$(nvm_get_colors 3) @@ -1663,67 +1659,71 @@ nvm_print_versions() { DEFAULT_COLOR=$(nvm_get_colors 5) LTS_COLOR=$(nvm_get_colors 6) - NVM_CURRENT=$(nvm_ls_current) - NVM_LATEST_LTS_COLOR=$(nvm_echo "${CURRENT_COLOR}" | command tr '0;' '1;') - NVM_OLD_LTS_COLOR="${DEFAULT_COLOR}" - local NVM_HAS_COLORS if [ -z "${NVM_NO_COLORS-}" ] && nvm_has_colors; then NVM_HAS_COLORS=1 fi - local LTS_LENGTH - local LTS_FORMAT - nvm_echo "${1-}" \ - | command sed '1!G;h;$!d' \ - | command awk '{ if ($2 && $3 && $3 == "*") { print $1, "(Latest LTS: " $2 ")" } else if ($2) { print $1, "(LTS: " $2 ")" } else { print $1 } }' \ - | command sed '1!G;h;$!d' \ - | while read -r VERSION_LINE; do - VERSION="${VERSION_LINE%% *}" - LTS="${VERSION_LINE#* }" - FORMAT='%15s' - if [ "_${VERSION}" = "_${NVM_CURRENT}" ]; then - if [ "${NVM_HAS_COLORS-}" = '1' ]; then - FORMAT="\033[${CURRENT_COLOR}-> %12s\033[0m" - else - FORMAT='-> %12s *' - fi - elif [ "${VERSION}" = "system" ]; then - if [ "${NVM_HAS_COLORS-}" = '1' ]; then - FORMAT="\033[${SYSTEM_COLOR}%15s\033[0m" - else - FORMAT='%15s *' - fi - elif nvm_is_version_installed "${VERSION}"; then - if [ "${NVM_HAS_COLORS-}" = '1' ]; then - FORMAT="\033[${INSTALLED_COLOR}%15s\033[0m" - else - FORMAT='%15s *' - fi - fi - if [ "${LTS}" != "${VERSION}" ]; then - case "${LTS}" in - *Latest*) - LTS="${LTS##Latest }" - LTS_LENGTH="${#LTS}" - if [ "${NVM_HAS_COLORS-}" = '1' ]; then - LTS_FORMAT=" \\033[${NVM_LATEST_LTS_COLOR}%${LTS_LENGTH}s\\033[0m" - else - LTS_FORMAT=" %${LTS_LENGTH}s" - fi - ;; - *) - LTS_LENGTH="${#LTS}" - if [ "${NVM_HAS_COLORS-}" = '1' ]; then - LTS_FORMAT=" \\033[${NVM_OLD_LTS_COLOR}%${LTS_LENGTH}s\\033[0m" - else - LTS_FORMAT=" %${LTS_LENGTH}s" - fi - ;; - esac - command printf -- "${FORMAT}${LTS_FORMAT}\\n" "${VERSION}" " ${LTS}" - else - command printf -- "${FORMAT}\\n" "${VERSION}" - fi - done + + command awk -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \ + -v installed_versions="$(nvm_ls | tr '\n' '|')" -v current="$NVM_CURRENT" \ + -v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \ + -v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \ + -v old_lts_color="$LTS_COLOR" -v has_colors="$NVM_HAS_COLORS" ' +BEGIN { + fmt_installed = has_colors ? ("\033[" installed_color "%15s\033[0m") : "%15s"; + fmt_system = has_colors ? ("\033[" system_color "%15s\033[0m") : "%15s"; + fmt_current = has_colors ? ("\033[" current_color "->%13s\033[0m") : "->%13s"; + + latest_lts_color = current_color; + sub(/0;/, "1;", latest_lts_color); + fmt_latest_lts = has_colors ? ("\033[" latest_lts_color "(Latest LTS: %s)\033[0m") : "(Latest LTS: %s)"; + fmt_old_lts = has_colors ? ("\033[" old_lts_color "(LTS: %s)\033[0m") : "(LTS: %s)"; + fmt_aux[1] = " "; + fmt_aux[2] = " * "; + + split(remote_versions, lines, "|"); + split(installed_versions, installed, "|"); + rows = length(lines); + + for (n = 1; n <= rows; n++) { + split(lines[n], fields, "[[:blank:]]+"); + cols = length(fields); + version = fields[1]; + is_installed = 0; + + for (i in installed) { + if (version == installed[i]) { + is_installed = 1; + break; + } + } + + fmt_version = "%15s"; + if (version == current) { + fmt_version = fmt_current; + } else if (version == "system") { + fmt_version = fmt_system; + } else if (is_installed) { + fmt_version = fmt_installed; + } + + aux = (!has_colors && is_installed) + 1; + if (cols == 1) { + formatted = sprintf(fmt_version, version); + } else if (cols == 2) { + formatted = sprintf((fmt_version fmt_aux[aux] fmt_old_lts), version, fields[2]); + } else if (cols == 3 && fields[3] == "*") { + formatted = sprintf((fmt_version fmt_aux[aux] fmt_latest_lts), version, fields[2]); + } + + output[n] = formatted; + } + + for (n = 1; n <= rows; n++) { + print output[n] + } + + exit +}' } nvm_validate_implicit_alias() {