Skip to content

Commit

Permalink
[perf] re-implement nvm_print_versions using awk
Browse files Browse the repository at this point in the history
reducing `nvm ls-remote` from almost 20s to below 2s.

Signed-off-by: ryenus <ryenus@gmail.com>
  • Loading branch information
ryenus committed Jun 5, 2022
1 parent a82edf4 commit 88cde8b
Showing 1 changed file with 64 additions and 66 deletions.
130 changes: 64 additions & 66 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1642,88 +1642,86 @@ 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)
NOT_INSTALLED_COLOR=$(nvm_get_colors 4)
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

awk -v _input="${1-}" -v _nvm_ls="$(nvm_ls)" -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 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[" default_color "(LTS: %s)\033[0m") : "(LTS: %s)";
fmt_aux[1] = " ";
fmt_aux[2] = " * ";
split(_input, lines, "\n");
split(_nvm_ls, installed, "\n");
total = length(lines);
for (n = 1; n <= total; n++) {
split(lines[n], fields, /\s+/);
len = 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 (len == 1) {
formatted = sprintf(fmt_version, version);
} else if (len == 2) {
formatted = sprintf((fmt_version fmt_aux[aux] fmt_old_lts), version, fields[2]);
} else if (len == 3 && fields[3] == "*") {
formatted = sprintf((fmt_version fmt_aux[aux] fmt_latest_lts), version, fields[2]);
}
output[n] = formatted;
}
for (n = 1; n <= total; n++) {
print output[n]
}
exit
}'
}

nvm_validate_implicit_alias() {
Expand Down

0 comments on commit 88cde8b

Please sign in to comment.