nvm icon indicating copy to clipboard operation
nvm copied to clipboard

`nvm ls` outputs color codes when piped to another command

Open unikitty37 opened this issue 4 years ago • 5 comments

Operating system and version: macOS 10.14.6 (18G8022)

nvm debug output:

nvm --version: v0.35.0
$TERM_PROGRAM: iTerm.app
$SHELL: /usr/local/bin/zsh
$SHLVL: 1
${HOME}: /Users/user
${NVM_DIR}: '${HOME}/.nvm'
${PATH}: ${HOME}/.nodenv/shims:${HOME}/.rbenv/shims:${HOME}/.rbenv/bin:${NVM_DIR}/versions/node/v16.0.0/bin:${HOME}/.rbenv/shims:${HOME}/bin:/usr/local/git/bin:/opt/local/bin:/opt/local/sbin:${HOME}/go/bin:/usr/local/sbin:/usr/local/bin:/usr/local/opt/gnu-sed/libexec/gnubin:/usr/local/opt/sqlite/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/mysql/bin:${HOME}/.composer/vendor/bin:/usr/local/opt/go/libexec/bin
$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.8 (x86_64-apple-darwin18.7.0)'
uname -a: 'Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Jan 12 22:04:47 PST 2021; root:xnu-4903.278.56~1/RELEASE_X86_64 x86_64'
OS version: Mac 10.14.6 18G8022
curl: /usr/bin/curl, curl 7.54.0 (x86_64-apple-darwin18.0) libcurl/7.54.0 LibreSSL/2.6.5 zlib/1.2.11 nghttp2/1.24.1
wget: not found
sed: /usr/local/opt/gnu-sed/libexec/gnubin/sed
cut: /usr/bin/cut
basename: /usr/bin/basename
rm: /bin/rm
mkdir: /bin/mkdir
xargs: /usr/bin/xargs
git: /usr/local/bin/git, git version 2.30.2
ls: grep:: No such file or directory
grep: grep: aliased to grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} (grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}), grep (BSD grep) 2.5.1-FreeBSD
awk: /usr/bin/awk, awk version 20070501
nvm current: system
which node: ${HOME}/.nodenv/shims/node
which iojs: iojs not found
which npm: ${HOME}/.nodenv/shims/npm
npm config get prefix: ${HOME}/.nodenv/versions/14.9.0
npm root -g: ${HOME}/.nodenv/versions/14.9.0/lib/node_modules

nvm ls output:

[0;34m        v16.0.0[0m
[0;32m->       system[0m
[0;34mdefault[0m [0;90m->[0m [0;34mv16.0.0[0m
[0;34mnode[0m [0;90m->[0m [0;34mstable[0m ([0;90m->[0m [0;34mv16.0.0[0m) [0;37m(default)[0m
[0;34mstable[0m [0;90m->[0m [0;34m16.0[0m ([0;90m->[0m [0;34mv16.0.0[0m) [0;37m(default)[0m
[1;31miojs[0m [0;90m->[0m [1;31mN/A[0m [0;37m(default)[0m
[1;31munstable[0m [0;90m->[0m [1;31mN/A[0m [0;37m(default)[0m
[1;33mlts/*[0m [0;90m->[0m [1;33mlts/fermium[0m ([0;90m->[0m [1;31mN/A[0m)
[1;33mlts/argon[0m [0;90m->[0m [1;31mv4.9.1[0m ([0;90m->[0m [1;31mN/A[0m)
[1;33mlts/boron[0m [0;90m->[0m [1;31mv6.17.1[0m ([0;90m->[0m [1;31mN/A[0m)
[1;33mlts/carbon[0m [0;90m->[0m [1;31mv8.17.0[0m ([0;90m->[0m [1;31mN/A[0m)
[1;33mlts/dubnium[0m [0;90m->[0m [1;31mv10.24.1[0m ([0;90m->[0m [1;31mN/A[0m)
[1;33mlts/erbium[0m [0;90m->[0m [1;31mv12.22.1[0m ([0;90m->[0m [1;31mN/A[0m)
[1;33mlts/fermium[0m [0;90m->[0m [1;31mv14.16.1[0m ([0;90m->[0m [1;31mN/A[0m)

How did you install nvm?

Homebrew

What steps did you perform?

  • nvm ls | pbcopy
  • Paste into a new issue

What happened?

All the ANSI codes were copied to the clipboard, as you can see from the section above.

What did you expect to happen?

nvm ls should omit ANSI colour codes and outputs plain text when being piped to another command. This is in line with how other colour-using commands work, so would be generally expected behaviour. Having to add --no-colors each time is a workaround, but it does not mirror the behaviour of other commands, which do the right thing automatically.

Perhaps the default behaviour could be to use colours when outputting to a TTY, and no colours when not outputting to a TTY, and adding a new --colors flag which could be used to force the issue when colours are required in a pipe?

Is there anything in any of your profile files that modifies the PATH?

Yes; they all execute after eval "$(nodenv init -)", which is at the top of the file.

unikitty37 avatar Apr 23 '21 15:04 unikitty37

nvm is not supported when installed from homebrew (altho this issue seems unrelated); please brew uninstall it and reinstall it from the only supported install method (the install script in the readme).

I'm not sure why nodenv init would matter, since this is nvm, not nodenv? Similarly, I'm not sure why there's nodenv in your path anywhere if you're using nvm. Can you elaborate?


I can reproduce this issue, and I completely agree that when it's in a pipe, it shouldn't output colors. I don't think "TTY-ness" should dictate that, since it's not about interactivity (but your suggestion would certainly work, albeit with a lot of added complexity in nvm).

Using some form of this technique should work here - when I naively use it in nvm_has_colors, the "ls" part of the nvm ls output works properly, but in an actual terminal it causes the "alias" part of the nvm ls output to drop its colorization, so some finesse might be needed. tagging in @gitburd and @naomiquinones for help.

ljharb avatar Apr 23 '21 15:04 ljharb

Thanks — I'm not 100% sure why nodenv was installed; it must have been left over from some time ago. For some reason zsh initialisation was working with it at the end of the file right up until I installed nvm, but removing nodenv entirely fixed it, and seems to be the more sensible option :)

unikitty37 avatar Apr 23 '21 15:04 unikitty37

I've cover help, ls, ls-remote and alias. Let me know if some issue with this PR.

heinthanth avatar Oct 18 '21 14:10 heinthanth