show only versions newer than NVM_MIN_VER if set

This commit is contained in:
ryenus 2024-01-29 21:04:48 +08:00
parent 99352a64d2
commit 48fe276e36

21
nvm.sh
View File

@ -1883,12 +1883,14 @@ nvm_print_versions() {
fi fi
command awk \ command awk \
-v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \ -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" -v min_ver="${NVM_MIN_VER:-v0}" \
-v installed_versions="$(nvm_ls | tr '\n' '|')" -v current="$NVM_CURRENT" \ -v installed_versions="$(nvm_ls | tr '\n' '|')" -v current="$NVM_CURRENT" \
-v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \ -v installed_color="$INSTALLED_COLOR" -v system_color="$SYSTEM_COLOR" \
-v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \ -v current_color="$CURRENT_COLOR" -v default_color="$DEFAULT_COLOR" \
-v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" ' -v old_lts_color="$DEFAULT_COLOR" -v has_colors="$NVM_HAS_COLORS" '
function alen(arr, i, len) { len=0; for(i in arr) len++; return len; } function alen(arr, i, len) { len=0; for(i in arr) len++; return len; }
function v2a(v, a) { sub(/^(iojs-)?v/, "", v); split(v, a, "."); }
function vcmp(v1,v2,a1,a2,i,d) { v2a(v1,a1); v2a(v2,a2); for(i=1;i<4;i++) { d = a1[i] - a2[i]; if(d!=0) return d; } return 0; }
BEGIN { BEGIN {
fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *"; fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";
fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *"; fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";
@ -1903,13 +1905,15 @@ BEGIN {
split(remote_versions, lines, "|"); split(remote_versions, lines, "|");
split(installed_versions, installed, "|"); split(installed_versions, installed, "|");
rows = alen(lines); rows = alen(lines);
filter = (min_ver != "v0");
for (n = 1; n <= rows; n++) { for (m = n = 1; n <= rows; n++) {
split(lines[n], fields, "[[:blank:]]+"); split(lines[n], fields, "[[:blank:]]+");
cols = alen(fields); cols = alen(fields);
version = fields[1]; version = fields[1];
is_installed = 0; if (filter && comp(version, min_ver) < 0) continue;
filter = 0;
is_installed = 0;
for (i in installed) { for (i in installed) {
if (version == installed[i]) { if (version == installed[i]) {
is_installed = 1; is_installed = 1;
@ -1926,8 +1930,7 @@ BEGIN {
fmt_version = fmt_installed; fmt_version = fmt_installed;
} }
padding = (!has_colors && is_installed) ? "" : " "; padding = (is_installed && !has_colors) ? "" : " ";
if (cols == 1) { if (cols == 1) {
formatted = sprintf(fmt_version, version); formatted = sprintf(fmt_version, version);
} else if (cols == 2) { } else if (cols == 2) {
@ -1936,11 +1939,11 @@ BEGIN {
formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]); formatted = sprintf((fmt_version padding fmt_latest_lts), version, fields[2]);
} }
output[n] = formatted; output[m++] = formatted;
} }
for (n = 1; n <= rows; n++) { for (m in output) {
print output[n] print output[m]
} }
exit exit