Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colors not displayed when child process output is piped #127

Closed
bcaudan opened this issue May 28, 2015 · 6 comments
Closed

Colors not displayed when child process output is piped #127

bcaudan opened this issue May 28, 2015 · 6 comments

Comments

@bcaudan
Copy link
Contributor

bcaudan commented May 28, 2015

Hi,

It seems that colors.js does not work when a child process output is piped.

For example, if I try to execute this code:

require('colors')

console.log('\x1b[31mansi\x1b[0m'); // red with ansi escaped codes
console.log('colors'.red);          // red with colors.js

With this runner:

var spawn = require('child_process').spawn;

var child = spawn('node', ['test.js'], { stdio: 'pipe' });

child.stdout.pipe(process.stdout)

Only the first line is in red.
But if I change the stdio option to 'inherit' then both lines are in red.

Is there a way that colors.js also works when output is piped?

@derrickpelletier
Copy link

@bcaudan when you spawn your script, include an argument for --color.

colors.js is set to automatically detect if it is on a TTY, if it's being piped it assumes no and strips out colors.

@bcaudan
Copy link
Contributor Author

bcaudan commented May 29, 2015

In my real use case (see referenced issue), I don't have control on how the script is spawn. Is there a way to force it programatically?

@bcaudan
Copy link
Contributor Author

bcaudan commented May 29, 2015

OK, I figured it out. I can set the enabled property like this:

var colors = require('colors');
colors.enabled = true;

And then colors are displayed!

Thanks for your help @derrickpelletier.

@bcaudan bcaudan closed this as completed May 30, 2015
skimi pushed a commit to naoned/naoned-cz-conventional-changelog that referenced this issue Mar 4, 2016
colors detects if it's a child process and disables colors.
We now force them.

See Marak/colors.js#127
skimi pushed a commit to naoned/conventions-npm that referenced this issue Mar 4, 2016
colors detects if it's a child process and disables colors.
We now force them.

See Marak/colors.js#127
@lixinliang
Copy link

Thank godness!
I find what I need here!
It's missing argument --color!

I believed that it's the shelljs(child process module)'s problem.
Or there was something wrong I wrote.

I had no idea of the required argument --color!

@ORESoftware
Copy link

ORESoftware commented Mar 3, 2018

anyone know how to do this with bash?

in my child process, I have echo -e "some control chars";

they colors/styling show up in the child, but when the parent inherits the stdout, the colors don't show up.

@Akryum
Copy link

Akryum commented May 26, 2018

You can try setting this env variable: FORCE_COLOR=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants