From 42af1e0a2e2203bec6b6180d81be9ee3246dcb7c Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 19 Jan 2019 15:37:38 +0100 Subject: [PATCH] tty: add hasColors function This adds a small wrapper around the `getColorDepth` function to check if the stream supports at least a specific amount of colors. This is convenient as the other API is not as straight forward and most use cases likely only want to know if a specific amount of colors is supported or not. --- doc/api/tty.md | 30 +++++++++++++++++++ lib/internal/tty.js | 24 ++++++++++++++- lib/tty.js | 7 ++++- ...lor-depth.js => test-tty-color-support.js} | 22 ++++++++++++++ ...r-depth.out => test-tty-color-support.out} | 0 5 files changed, 81 insertions(+), 2 deletions(-) rename test/pseudo-tty/{test-tty-get-color-depth.js => test-tty-color-support.js} (77%) rename test/pseudo-tty/{test-tty-get-color-depth.out => test-tty-color-support.out} (100%) diff --git a/doc/api/tty.md b/doc/api/tty.md index 26761442a50865..12a34b14eb7cc0 100644 --- a/doc/api/tty.md +++ b/doc/api/tty.md @@ -176,6 +176,35 @@ corresponding to this `WriteStream`. The array is of the type `[numColumns, numRows]` where `numColumns` and `numRows` represent the number of columns and rows in the corresponding [TTY](tty.html). +### writeStream.hasColors([count][, env]) + + +* `count` {integer} The number of colors that are requested (minimum 2). + **Default:** 16. +* `env` {Object} An object containing the environment variables to check. This + enables simulating the usage of a specific terminal. **Default:** + `process.env`. +* Returns: {boolean} + +Returns `true` if the `writeStream` supports at least as many colors as provided +in `count`. Minimum support is 2 (black and white). + +This has the same false positives and negatives as described in +[`writeStream.getColorDepth()`][]. + +```js +process.stdout.hasColors(); +// Returns true or false depending on if `stdout` supports at least 16 colors. +process.stdout.hasColors(256); +// Returns true or false depending on if `stdout` supports at least 256 colors. +process.stdout.hasColors({ TMUX: '1' }); +// Returns true. +process.stdout.hasColors(2 ** 24, { TMUX: '1' }); +// Returns false (the environment setting pretends to support 2 ** 8 colors). +``` + ### writeStream.isTTY