Skip to content

Commit

Permalink
substitute newlines when passed through awk -v
Browse files Browse the repository at this point in the history
and adjusted variable names for clarity.

Signed-off-by: ryenus <ryenus@gmail.com>
  • Loading branch information
ryenus committed Jun 6, 2022
1 parent 355595c commit d24005d
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,8 @@ nvm_print_versions() {
NVM_HAS_COLORS=1
fi

awk -v _input="${1-}" -v _nvm_ls="$(nvm_ls)" -v current="$NVM_CURRENT" \
command awk -v remote_versions="$(printf '%s' "${1-}" | tr '\n' '|')" \
-v installed_versions="$(nvm_ls | tr '\n' '|')" -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 old_lts_color="$LTS_COLOR" -v has_colors="$NVM_HAS_COLORS" '
Expand All @@ -1679,14 +1680,13 @@ BEGIN {
fmt_aux[1] = " ";
fmt_aux[2] = " * ";
split(_input, lines, "\n");
split(_nvm_ls, installed, "\n");
total = length(lines);
split(remote_versions, lines, "|");
split(installed_versions, installed, "|");
rows = length(lines);
for (n = 1; n <= total; n++) {
split(lines[n], fields, /\s+/);
len = length(fields);
for (n = 1; n <= rows; n++) {
split(lines[n], fields, "[[:blank:]]+");
cols = length(fields);
version = fields[1];
is_installed = 0;
Expand All @@ -1707,18 +1707,18 @@ BEGIN {
}
aux = (!has_colors && is_installed) + 1;
if (len == 1) {
if (cols == 1) {
formatted = sprintf(fmt_version, version);
} else if (len == 2) {
} else if (cols == 2) {
formatted = sprintf((fmt_version fmt_aux[aux] fmt_old_lts), version, fields[2]);
} else if (len == 3 && fields[3] == "*") {
} else if (cols == 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++) {
for (n = 1; n <= rows; n++) {
print output[n]
}
Expand Down

0 comments on commit d24005d

Please sign in to comment.