Skip to content
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

Add basic color detection #5

Merged
merged 15 commits into from
Oct 27, 2021
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import tty from 'node:tty';

const hasColors = tty.WriteStream.prototype.hasColors();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Richienb

Was there a reason for doing this instead of process.stdout.hasColors()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember.

@sindresorhus

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the answer is in the TODO comment. process.stdout.hasColors() was not available then.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed that.

Seems process.stdout.hasColors() has been available. However, they're trying to deprecate it because:

In the PR, they're trying to expose a standalone function but is blocked by deprecation strategy for the prototype method.


// Intentionally not using template literal for performance.
const format = (startCode, endCode) => string => '\u001B[' + startCode + 'm' + string + '\u001B[' + endCode + 'm';
const format = (startCode, endCode) => hasColors ? string => '\u001B[' + startCode + 'm' + string + '\u001B[' + endCode + 'm' : string => string;

export const reset = format(0, 0);
export const bold = format(1, 22);
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as colors from 'yoctocolors';
console.log(colors.red('Yo!'));
```

*This package does not do color support detection. Check out [`supports-color`](https://github.com/chalk/supports-color) if you need that. Node.js will hopefully get [built-in support](https://github.com/nodejs/node/pull/40240) for color support detection at some point.*
*This package supports basic color detection. Node.js will hopefully get [better support](https://github.com/nodejs/node/pull/40240) for color support detection at some point.*
Richienb marked this conversation as resolved.
Show resolved Hide resolved

## Styles

Expand Down