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

tty: add hasColors function #26247

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions doc/api/tty.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
<!-- YAML
added: REPLACEME
-->

* `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
<!-- YAML
added: v0.5.8
Expand Down Expand Up @@ -217,3 +246,4 @@ integer.
[`process.stderr`]: process.html#process_process_stderr
[`process.stdin`]: process.html#process_process_stdin
[`process.stdout`]: process.html#process_process_stdout
[`writeStream.getColorDepth()`]: #tty_writestream_getcolordepth_env
24 changes: 23 additions & 1 deletion lib/internal/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
};
7 changes: 6 additions & 1 deletion lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const net = require('net');
const { TTY, isTTY } = internalBinding('tty_wrap');
const errors = require('internal/errors');
const { ERR_INVALID_FD, ERR_TTY_INIT_FAILED } = errors.codes;
const { getColorDepth } = require('internal/tty');
const {
getColorDepth,
hasColors
} = require('internal/tty');

// Lazy loaded for startup performance.
let readline;
Expand Down Expand Up @@ -109,6 +112,8 @@ WriteStream.prototype.isTTY = true;

WriteStream.prototype.getColorDepth = getColorDepth;

WriteStream.prototype.hasColors = hasColors;

WriteStream.prototype._refreshSize = function() {
const oldCols = this.columns;
const oldRows = this.rows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@ const writeStream = new WriteStream(fd);
const depth = writeStream.getColorDepth();
assert.strictEqual(typeof depth, 'number');
assert(depth >= 1 && depth <= 24);

const support = writeStream.hasColors();
assert.strictEqual(support, depth !== 1);
}

// Validate invalid input.
[true, null, () => {}, Symbol(), 5n].forEach((input) => {
assert.throws(
() => writeStream.hasColors(input),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});

[-1, 1].forEach((input) => {
assert.throws(
() => writeStream.hasColors(input),
{ code: 'ERR_OUT_OF_RANGE' }
);
});

// Check different environment variables.
[
[{ COLORTERM: '1' }, 4],
Expand Down Expand Up @@ -54,6 +72,10 @@ const writeStream = new WriteStream(fd);
`i: ${i}, expected: ${depth}, ` +
`actual: ${actual}, env: ${inspect(env)}`
);
const colors = 2 ** actual;
assert(writeStream.hasColors(colors, env));
assert(!writeStream.hasColors(colors + 1, env));
assert(depth >= 4 ? writeStream.hasColors(env) : !writeStream.hasColors(env));
});

// OS settings
Expand Down