Marak / colors.js

get colors in your node.js console
https://github.com/Marak/colors.js
Other
5.17k stars 446 forks source link

Not working with Docker logs #241

Closed danielfaust closed 6 years ago

danielfaust commented 6 years ago

I'm having an issue here, it's the first time I'm trying out this module.

My setup is a little bit complex. Basically it is a node server in an Alpine Docker container on an Ubuntu host, and the docker logs are being tailed in a tmux port. The tmux port is displayed via ssh in a cygwin64\bin\mintty.exe terminal on a Windows 7 machine, but the problem also exists on a standard Ubuntu 18.04 Desktop terminal.

Executing the following in the terminal for i in {0..255}; do printf "\x1b[38;5;${i}mcolour${i}\x1b[0m\n"; done through a plain ssh connection to the server without attaching to tmux does show all the 256 colors. When attaching to tmux and running it in that panel, it also shows all the colors. So colors aren't really the issue, I've also been printing colors through Python programs, and vim also handles all the colors.

It boils down to this problem:

 // next is NOT RED (but default)
console.log('SHOULD BE RED'.red)

 // next is NOT RED (but default)
console.log(colors.red('SHOULD BE RED'))

 // next is YELLOW (ok, works)
console.log(colors.styles['yellow'].open, 'SHOULD BE YELLOW', colors.styles['yellow'].close)

 // next is GREEN (ok, works)
console.log(colors.styles['green'].open + 'SHOULD BE GREEN' + colors.styles['green'].close)

// all next are BLUE (ok, works)
console.log('\u001b[34m' + 'SHOULD BE BLUE' + '\u001b[0m')
console.log('\u001b[94m' + 'SHOULD BE BLUE' + '\u001b[0m')
console.log('\033[34m' + 'SHOULD BE BLUE' + '\033[0m')
console.log('\033[94m' + 'SHOULD BE BLUE' + '\033[0m')

This happens regardless of me being attached to tmux or just on plain bash.

All my terminals use base16-shell ( https://github.com/chriskempson/base16-shell )

Regarding the multiple blue variants, see https://gist.github.com/chrisopedia/8754917 for 34 vs 94 (I've always been using 94)

DABH commented 6 years ago

It's possible the complexity of your setup is making colors unable to detect colors support (see https://github.com/Marak/colors.js/blob/master/lib/system/supports-colors.js). Do things work if you set the environment variable FORCE_COLORS=1? That is supposed to be a catch-all solution to force colors to appear.

danielfaust commented 6 years ago

I've just stumbled across the following line

https://github.com/Marak/colors.js/blob/4a6d75d01c4389a9e9f7288cc2434b95decbcd58/lib/colors.js#L61

and issuing a colors.enabled = true; before using the module fixes the issue.

For some reason it is disabled automatically.

@DABH Right, stream.isTTY returns false.

Maybe this should get added to the README? That if it doesn't work, one should first try to set colors.enabled = true;? I've spent around two hours on this issue (tmux and all that chain...)

DABH commented 6 years ago

That's another solution -- colors.enabled is only set to true if the supports-colors module detects color support (so, FORCE_COLORS=1 would also solve your problem). If you figure out a generic case that's missing in supports-colors feel free to let us know, although your setup is complex enough that I'm not sure if there is a good generalizable rule :) (hence why we have these manual overrides!)