[perf] `nvm_print_versions`: re-implement using awk

reducing `nvm ls-remote` from almost 20s to below 2s.

Signed-off-by: ryenus <ryenus@gmail.com>
ryenus 2022-06-05 13:27:03 +08:00 committed by Jordan Harband
parent 7c929f8742
commit 5e9791c4f8
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
1 changed files with 65 additions and 63 deletions

128
nvm.sh
View File

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