Quickie: Color console output with parallel command
I’ve got a Lerna monorepos with a few microservices. I needed to run the dev version a few of these microservices so I decided to use parallel to run them all at the same time in my terminal:
parallel --tagstring '{/}' --linebuffer 'cd {} && npm run dev' ::: packages/service-a packages/service-b packages/service-c
This worked, but I noticed that the color output was stripped when doing this. This is really annoying. The color really helps during development to spot errors.
The reason is mostly that scripts don’t think they’re in a tty, so they disable coloring. Sometimes you can force-enable colors (such as with ls --color=always
), but not all scripts and programs have a flag like that.
I figured that the unbuffer
utility can be used to basically “trick” scripts into thinking they’re in a tty.
parallel --tagstring '{/}' --linebuffer 'cd {} && unbuffer npm run dev' ::: packages/service-a packages/service-b packages/service-c
Boom. Done. 🎉
Installing unbuffer
It’s part of the expect
package. So brew install expect
, apt-get install expect
, yum install expect
etc.