chalk: chalk.enabled=true or { enabled: true} does not work when color support is initially detected as false

Issuehunt badges

Environment

Chalk: 2.3.0 Node.js: 6.12.0 and 8.9.3

Issue

chalk.js

const chalk = require('chalk');
chalk.enabled = true;
console.log(chalk`{green hello}`);

–or–

const chalk = new require('chalk').constructor({ enabled: true });

If you run chalk.js as:

node chalk.js

The output is a green hello.

If you run this in a clean environment (which causes supports-color to report that colors are not supported):

env -i node chalk.js

The output is a un-colored hello.

Details

I traced this to https://github.com/chalk/chalk/blob/master/index.js#L177, where this.enabled is false, and this is builder. It seems like, somehow, the initial value for enabled is not getting changed or is not filtering down to the enabled property defined on builder.

As expected, using the environment variable FORCE_COLOR or the --color parameter does work, as it gets enabled to the right value, initially.

Interestingly, running it as (to force it on from the start):

env -i node chalk.js --color

…and then doing:

chalk.enabled = false;

does actually turn it off. Now I’m thinking there’s some issue with truthy and falsy. I’m not sure.

Aside

It would definitely be easier for me to dig more on this if there weren’t so much indirection in the way this is implemented 😇; but, I’m sure some or most of that is necessary to get this to work so cleanly.


IssueHunt Summary

qix- qix- has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 23 (17 by maintainers)

Commits related to this issue

Most upvoted comments

chalk.level is 0 when supports-color reports that colors are not supported. This makes output un-colored.

const chalk = require('chalk')                                                  
chalk.enabled = true                                                            
chalk.level = 3                                                                 
console.log(chalk`{green hello}`) 

outputs green text.

It seems not good to have un-colored output when chalk.enable is true , but I am not sure how to make it better.

Yeah, I think we should remove .enabled for Chalk 3. PR welcome.

It would surely make sense to only use level instead of having to set both level and enabled (as @nazrhyn pointed out) because it seems redundant and overly-complex to use two variables that do the same thing. If there’s a real need to keep enabled then having it as a setter/getter that would only depend on whether level !== 0 and not a separate field (such as enabled) then it would make it easier on both internal and external sides.

Ohh, I see. You have to do both? I’m not sure why I wasn’t parsing that. Tested that, and it works.

It really does sound like enabled should be deprecated, and level should just control this. I suppose setters for both would be needed to avoid the breaking change, but where to put the actual value is always a problem in JavaScript…

Anyway, it sounds like this issue is invalid, then. At the very least, adding something into the documentation would be helpful. Would you like me to submit a PR for that? I can also remove the failing test (or replace it with one that verifies that it can be turned on by setting both level and enabled).