From d65637f2e8068ee4f5f64ef2a5c13299ec311f90 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Wed, 21 Oct 2026 12:44:19 -0700 Subject: [PATCH] [Fix] `nvm_print_versions`: widen the version column for long versions Versions longer than 15 characters, like nightlies and release candidates, overflowed the fixed-width column, so `nvm ls` and `nvm ls-remote` misaligned them; the column now also fits the `->` before the current version. --- nvm.sh | 20 ++++++++++++---- .../nvm_print_versions aligns long versions | 23 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100755 test/fast/Unit tests/nvm_print_versions aligns long versions diff --git a/nvm.sh b/nvm.sh index dc9b5a53..feff68ff 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2596,9 +2596,21 @@ function spaces(count, result) { result = ""; while (count-- > 0) result = resul function italicize(text) { return has_italics ? ("\033[3m" text "\033[23m") : text; } BEGIN { named_alias_list = ENVIRON["NVM_NAMED_ALIASES"]; - 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_current = has_colors ? (current_color ? "\033[" current_color "->%13s\033[0m" : "%15s") : "->%13s *"; + # wide enough for the longest version, like a nightly, plus the arrow before the current version + column = 15; + column_rows = split(remote_versions, column_lines, "|"); + for (n = 1; n <= column_rows; n++) { + split(column_lines[n], column_fields, "[[:blank:]]+"); + column_width = length(column_fields[1]) + (column_fields[1] == current ? 2 : 0); + if (column_width > column) { + column = column_width; + } + } + fmt_column = "%" column "s"; + fmt_arrow = "->%" (column - 2) "s"; + fmt_installed = has_colors ? (installed_color ? "\033[" installed_color fmt_column "\033[0m" : fmt_column) : fmt_column " *"; + fmt_system = has_colors ? (system_color ? "\033[" system_color fmt_column "\033[0m" : fmt_column) : fmt_column " *"; + fmt_current = has_colors ? (current_color ? "\033[" current_color fmt_arrow "\033[0m" : fmt_column) : fmt_arrow " *"; latest_lts_color = current_color; sub(/0;/, "1;", latest_lts_color); @@ -2677,7 +2689,7 @@ BEGIN { } } - fmt_version = "%15s"; + fmt_version = fmt_column; if (version == current) { fmt_version = fmt_current; } else if (version == "system") { diff --git a/test/fast/Unit tests/nvm_print_versions aligns long versions b/test/fast/Unit tests/nvm_print_versions aligns long versions new file mode 100755 index 00000000..3f5ed4df --- /dev/null +++ b/test/fast/Unit tests/nvm_print_versions aligns long versions @@ -0,0 +1,23 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +nvm_ls_current() { echo 'none'; } +nvm_ls() { echo 'N/A'; } + +OUTPUT="$(NVM_NO_COLORS=--no-colors nvm_print_versions "$(printf 'v23.11.0\nv24.0.0-nightly20241031996708042b')")" +EXPECTED="$(printf '%33s\n%s' v23.11.0 v24.0.0-nightly20241031996708042b)" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "versions longer than 15 characters should widen the column; got >${OUTPUT}<" + +nvm_ls_current() { echo 'v24.0.0-nightly20241031996708042b'; } +OUTPUT="$(NVM_NO_COLORS=--no-colors nvm_print_versions "$(printf 'v23.11.0\nv24.0.0-nightly20241031996708042b')")" +EXPECTED="$(printf '%35s\n%s' v23.11.0 '->v24.0.0-nightly20241031996708042b *')" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "the column should also fit the current version's arrow; got >${OUTPUT}<" +nvm_ls_current() { echo 'none'; } + +OUTPUT="$(NVM_NO_COLORS=--no-colors nvm_print_versions "$(printf 'v23.11.0\nv24.11.0')")" +EXPECTED="$(printf '%15s\n%15s' v23.11.0 v24.11.0)" +[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "the column should stay 15 characters wide for short versions; got >${OUTPUT}<"