diff --git a/index.js b/index.js index 3cb1573..79d47bb 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,10 @@ +import tty from 'node:tty'; + +// TODO: Use a better method when it's added to Node.js (https://github.com/nodejs/node/pull/40240) +const hasColors = tty.WriteStream.prototype.hasColors(); + // 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); diff --git a/package.json b/package.json index 35948e1..f42b9ed 100644 --- a/package.json +++ b/package.json @@ -46,5 +46,10 @@ "ava": "^3.15.0", "tsd": "^0.17.0", "xo": "^0.44.0" + }, + "ava": { + "environmentVariables": { + "FORCE_COLOR": "1" + } } } diff --git a/readme.md b/readme.md index be87f38..1a98868 100644 --- a/readme.md +++ b/readme.md @@ -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](https://nodejs.org/api/tty.html#writestreamhascolorscount-env). Colors can be forcefully enabled by setting the `FORCE_COLOR` environment variable to `1` and can be forcefully disabled by setting `NO_COLOR` or `NODE_DISABLE_COLORS` to any value. [More info.](https://nodejs.org/api/tty.html#writestreamgetcolordepthenv)* ## Styles