[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.
This commit is contained in:
2 files changed
+39
-4
No files matched your search
@@ -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; }
|
function italicize(text) { return has_italics ? ("\033[3m" text "\033[23m") : text; }
|
||||||
BEGIN {
|
BEGIN {
|
||||||
named_alias_list = ENVIRON["NVM_NAMED_ALIASES"];
|
named_alias_list = ENVIRON["NVM_NAMED_ALIASES"];
|
||||||
fmt_installed = has_colors ? (installed_color ? "\033[" installed_color "%15s\033[0m" : "%15s") : "%15s *";
|
# wide enough for the longest version, like a nightly, plus the arrow before the current version
|
||||||
fmt_system = has_colors ? (system_color ? "\033[" system_color "%15s\033[0m" : "%15s") : "%15s *";
|
column = 15;
|
||||||
fmt_current = has_colors ? (current_color ? "\033[" current_color "->%13s\033[0m" : "%15s") : "->%13s *";
|
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;
|
latest_lts_color = current_color;
|
||||||
sub(/0;/, "1;", latest_lts_color);
|
sub(/0;/, "1;", latest_lts_color);
|
||||||
@@ -2677,7 +2689,7 @@ BEGIN {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt_version = "%15s";
|
fmt_version = fmt_column;
|
||||||
if (version == current) {
|
if (version == current) {
|
||||||
fmt_version = fmt_current;
|
fmt_version = fmt_current;
|
||||||
} else if (version == "system") {
|
} else if (version == "system") {
|
||||||
|
|||||||
@@ -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}<"
|
||||||
Reference in new issue
Block a user