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
enabled
is global state
#46
Comments
Chalk is a singleton by design. You either want colors outputted or you don't. This doesn't interfere with other libraries as they need to handle (usually not do anything special) both enabled and not anyways. // @jbnicolai |
ok, that's too bad. This means chalk is not really usable for adding colour |
What do you need that for? |
In my case it's a logger for which you can customise the colours for each stream: file, stderr and what not. I don't really want to second guess the user and disable colours after they specifically requested it. However using this in a application that logs to stderr and usually have stdout redirected, colours gets disabled and I get boring text in my terminal. But at the same time I don't want to force it on in case someone is actually printing coloured stuff to stdout outside the logger and expect the auto-detect bahaviour. |
Hmm, I hadn't really thought of this kind of use-case. I'll need some time to think about this. @jbnicolai Thoughts? |
Current usage would be the same and @keis could call it to get its own instance: var chalk = require('chalk')();
chalk.enabled = true; |
Turning the main export into a factory would be the proper fix I believe, but that means breaking back-compat. |
Why not make chalk optionally instance-based? var chalk = require("chalk").instance(); // or .new() if it's allowed
chalk.enabled = true; // only in this module I had a similar concern with one of my libraries and just opted for forced instantiation via Upon second glance, this is barely any different from the "factory" suggestion above. I suck with technical terms. |
I'm leaning towards making the default export be a factory. I'm ok with breaking backwards compat. Now is the time to do it. Is there any downsides with making it a factory that I'm not seeing? Like, would it impact performance? |
I think making a backwards incompatible change would be bad form, since it's possible to keep the current interface (this is clearly the majority of chalk's use cases), as well as allow for @keis's use case (which is an edge case). @julien-f this way is very cool and compact, but it's not clear what it does when looking at the code, plus if someone logs an var chalkDefault = require('chalk');
var chalkAdditional = require('chalk')(); @stevenvachon's suggestion is more explicit and clean in what it does. It is longer but obviously most of your users won't be using it this way. My vote goes to this one: var chalkDefault = require('chalk');
var chalkAdditional = require('chalk').new() // or .instance() as suggested, or newInstance();
// then somewhere in your code you could
chalkAdditional.enabled = false; // leaving the default enabled |
So |
FWIW, that's the solution I came up with Inquirer: var inquirer = require('inquirer');
var independantInquirer = require('inquirer').createPromptModule(); |
But does it really make sense to support globally disabling |
Good point. How about just ignoring require('chalk').enabled = false; would have no effect, and on top of documenting it, And then: var chalkAdditional = require('chalk').instance();
chalkAdditional.enabled = false; would disable only that instance. This could work, no? |
I want as simple interface as possible. That's why I'm asking if ability to disable globally is really something that should stay? Otherwise I can just go with instance based by default and have a simpler API. |
IMO, no. Given the points above, you should not be able to disable on a global level. |
@sindresorhus the end-users will still need some way to disable globally, so maybe you should leave the ability to disable through something like |
|
That's true. Darnit, since having |
I think keeping the global If structuring the module so that you do 'module.exports = new Chalk();' to create the default context, then |
I agree that overwriting the globally shared I suggest we go with adding a function to the global Chalk object to create a new, not-shared instance. The syntax @keis proposed seems nice. |
Using chalk from a library I know I always want colours enabled as it is explicitly requested to begin with. But doing
require('chalk').enabled = true
would be bad pratice as this enforce the setting globally and possibly interfere with other libraries using chalk and depending on the auto-detection.It would be useful to be able to set this option in some isolated way.
The text was updated successfully, but these errors were encountered: