From af0b1387830ec946240bd1d661d514fcbe3b9198 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 7 Mar 2019 01:48:02 +0100 Subject: [PATCH 1/2] tty: add NO_COLOR and FORCE_COLOR support This adds support to enforce a specific color depth by checking the `FORCE_COLOR` environment variable similar to `chalk`. On top of that we also add support for the `NO_COLOR` environment variable as suggested by https://no-color.org/. --- doc/api/tty.md | 20 +++++++---- lib/internal/tty.js | 40 +++++++++++++++++++++- test/pseudo-tty/test-tty-color-support.js | 11 ++++-- test/pseudo-tty/test-tty-color-support.out | 8 +++++ 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/doc/api/tty.md b/doc/api/tty.md index 12a34b14eb7cc0..c2e9371f2ff74e 100644 --- a/doc/api/tty.md +++ b/doc/api/tty.md @@ -144,8 +144,9 @@ position. added: v9.9.0 --> -* `env` {Object} An object containing the environment variables to check. - **Default:** `process.env`. +* `env` {Object} An object containing the environment variables to check. This + enables simulating the usage of a specific terminal. **Default:** + `process.env`. * Returns: {number} Returns: @@ -159,11 +160,18 @@ Use this to determine what colors the terminal supports. Due to the nature of colors in terminals it is possible to either have false positives or false negatives. It depends on process information and the environment variables that may lie about what terminal is used. -To enforce a specific behavior without relying on `process.env` it is possible -to pass in an object with different settings. +It is possible to pass in an `env` object to simulate the usage of a specific +terminal. This can be useful to check how specific environment settings behave. + +To enforce a specific color support, use one of the below environment settings. + +* 2 colors: FORCE_COLOR = 0 (Disables colors) +* 16 colors: FORCE_COLOR = 1 +* 256 colors: FORCE_COLOR = 2 +* 16,777,216 colors: FORCE_COLOR = 3 -Use the `NODE_DISABLE_COLORS` environment variable to enforce this function to -always return 1. +Disabling color support is also possible by using the `NO_COLOR` and +`NODE_DISABLE_COLORS` environment variables. ### writeStream.getWindowSize()