Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add 'auto' mode that only adds colors for the terminal #34

Closed
wants to merge 1 commit into from

7 participants

@danielbeardsley

If the mode is set to 'auto', then escaping will only happen if stdout is a TTY
(terminal). If stdout is redirected somewhere, strings will not be modified by
the colorize functions.

@danielbeardsley danielbeardsley Add 'auto' mode that only adds colors for the terminal
If the mode is set to 'auto', then escaping will only happen if stdout is a TTY
(terminal).  If stdout is redirected somewhere, strings will not be modified by
the colorize functions.
922384e
@clarkbox clarkbox referenced this pull request in clarkbox/testswarm-browserstack
Closed

Avoid outputting console color codes to log files #34

@Marak
Owner

@danielbeardsley - I'm not against adding this, but it seems it might be a bit more straight forward for the developer to handle this on their own instead of having colors do it.

@danielbeardsley

You're saying have an enabled option that you can set to false? We could support setting mode to always, never, and 'auto'. never mind that suggestion, that's what we already have.

I feel the like the 99% use-case is turning off colors if the output is being piped (which this does). So it makes sense to me to add a few lines of code to make it easy on most users.

@jakl

+1

@Ackar

+1

Also, I think it would be better if all of this was done automatically without having to set the mode to 'auto'.
In the vast majority of cases you don't want colors if you're not on a TTY, and you can still set the mode manually if you have particular needs.

It's a simple feature but it would be really nice to have...

@stevenvachon

@Marak hasn't this been implemented in v1.0 ?

@pksunkara
Collaborator

Yes, this has been implemented in v1.0. If the process.stdout is a TTY, the output will be colored, otherwise not.

@pksunkara pksunkara closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 1, 2012
  1. @danielbeardsley

    Add 'auto' mode that only adds colors for the terminal

    danielbeardsley authored
    If the mode is set to 'auto', then escaping will only happen if stdout is a TTY
    (terminal).  If stdout is redirected somewhere, strings will not be modified by
    the colorize functions.
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 0 deletions.
  1. +4 −0 ReadMe.md
  2. +6 −0 colors.js
View
4 ReadMe.md
@@ -34,6 +34,10 @@ console.log('hello'.green); // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse); // inverses the color
console.log('OMG Rainbows!'.rainbow); // rainbow (ignores spaces)
+
+colors.mode = 'auto';
+console.log('hello'.green); // outputs green text only if stdout is not being
+ // redirected
```
# Creating Custom themes
View
6 colors.js
@@ -41,6 +41,7 @@ if (!isHeadless) {
exports.mode = "console";
}
+
//
// Prototypes the string object to have additional method calls that add terminal colors
//
@@ -59,6 +60,11 @@ function stylize(str, style) {
var styles;
+ if (exports.mode === 'auto') {
+ var isStdoutTty = require('tty').isatty(process.stdout.fd);
+ exports.mode = isStdoutTty ? 'console' : 'none';
+ }
+
if (exports.mode === 'console') {
styles = {
//styles
Something went wrong with that request. Please try again.