chalk.enabled=true or { enabled: true} does not work when color support is initially detected as false
See original GitHub issueEnvironment
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
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:23 (17 by maintainers)
Top GitHub Comments
chalk.level
is 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.enable
istrue
, but I am not sure how to make it better.Yeah, I think we should remove
.enabled
for Chalk 3. PR welcome.