chalk: chalk.enabled=true or { enabled: true} does not work when color support is initially detected as false
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- has been rewarded.
Backers (Total: $40.00)
issuehunt ($40.00)
Submitted pull Requests
Tips
- Checkout the Issuehunt explorer to discover more funded issues.
- Need some help from other developers? Add your repositories on IssueHunt to raise funds.
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
- Add failing test for #234 (#235) — committed to chalk/chalk by nazrhyn 6 years ago
- :space_invader: added level to chalk to see if it fixes color in heroku per https://github.com/chalk/chalk/issues/234 — committed to mgiraldo/mud-of-babel by mgiraldo 5 years ago
- remove the .enabled property in lieu of .level (closes #234) — committed to chalk/chalk by Qix- 5 years ago
chalk.levelis 0 when supports-color reports that colors are not supported. This makes output un-colored.outputs green text.
It seems not good to have un-colored output when
chalk.enableistrue, but I am not sure how to make it better.Yeah, I think we should remove
.enabledfor Chalk 3. PR welcome.It would surely make sense to only use
levelinstead of having to set bothlevelandenabled(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 keepenabledthen having it as a setter/getter that would only depend on whetherlevel !== 0and not a separate field (such asenabled) 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
enabledshould be deprecated, andlevelshould 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
levelandenabled).