diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-01-19 15:37:38 +0100 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2019-03-05 18:37:39 +0100 |
commit | d4fdec6b6593659cb38e1db9c8083d4fde9bcacb (patch) | |
tree | ecaf80b2517373e0cdb264c945209b7d12f65766 /lib/internal/tty.js | |
parent | a52c1ead0221a0c913d55d86ddddbe2ff3d04d5b (diff) | |
download | android-node-v8-d4fdec6b6593659cb38e1db9c8083d4fde9bcacb.tar.gz android-node-v8-d4fdec6b6593659cb38e1db9c8083d4fde9bcacb.tar.bz2 android-node-v8-d4fdec6b6593659cb38e1db9c8083d4fde9bcacb.zip |
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.
PR-URL: https://github.com/nodejs/node/pull/26247
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Diffstat (limited to 'lib/internal/tty.js')
-rw-r--r-- | lib/internal/tty.js | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/lib/internal/tty.js b/lib/internal/tty.js index 45f278d542..486dbbeaa5 100644 --- a/lib/internal/tty.js +++ b/lib/internal/tty.js @@ -22,6 +22,11 @@ 'use strict'; +const { + ERR_INVALID_ARG_TYPE, + ERR_OUT_OF_RANGE +} = require('internal/errors').codes; + let OSRelease; const COLORS_2 = 1; @@ -151,6 +156,23 @@ function getColorDepth(env = process.env) { return COLORS_2; } +function hasColors(count, env) { + if (env === undefined && + (count === undefined || typeof count === 'object' && count !== null)) { + env = count; + count = 16; + } else { + if (typeof count !== 'number') { + throw new ERR_INVALID_ARG_TYPE('count', 'number', count); + } + if (count < 2) { + throw new ERR_OUT_OF_RANGE('count', '>= 2', count); + } + } + return count <= 2 ** getColorDepth(env); +} + module.exports = { - getColorDepth + getColorDepth, + hasColors }; |