• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert').strict;
5const { WriteStream } = require('tty');
6const { inspect } = require('util');
7
8const fd = common.getTTYfd();
9const writeStream = new WriteStream(fd);
10
11{
12  const depth = writeStream.getColorDepth();
13  assert.strictEqual(typeof depth, 'number');
14  assert(depth >= 1 && depth <= 24);
15
16  const support = writeStream.hasColors();
17  assert.strictEqual(support, depth !== 1);
18}
19
20// Validate invalid input.
21[true, null, () => {}, Symbol(), 5n].forEach((input) => {
22  assert.throws(
23    () => writeStream.hasColors(input),
24    { code: 'ERR_INVALID_ARG_TYPE' }
25  );
26});
27
28[-1, 1].forEach((input) => {
29  assert.throws(
30    () => writeStream.hasColors(input),
31    { code: 'ERR_OUT_OF_RANGE' }
32  );
33});
34
35// Check different environment variables.
36[
37  [{ COLORTERM: '1' }, 4],
38  [{ COLORTERM: 'truecolor' }, 24],
39  [{ COLORTERM: '24bit' }, 24],
40  [{ TMUX: '1' }, 8],
41  [{ CI: '1' }, 1],
42  [{ CI: '1', TRAVIS: '1' }, 8],
43  [{ CI: '1', CIRCLECI: '1' }, 8],
44  [{ CI: '1', APPVEYOR: '1' }, 8],
45  [{ CI: '1', GITLAB_CI: '1' }, 8],
46  [{ CI: '1', CI_NAME: 'codeship' }, 8],
47  [{ TEAMCITY_VERSION: '1.0.0' }, 1],
48  [{ TEAMCITY_VERSION: '9.11.0' }, 4],
49  [{ TERM_PROGRAM: 'iTerm.app' }, 8],
50  [{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '3.0' }, 24],
51  [{ TERM_PROGRAM: 'iTerm.app', TERM_PROGRAM_VERSION: '2.0' }, 8],
52  [{ TERM_PROGRAM: 'HyperTerm' }, 24],
53  [{ TERM_PROGRAM: 'Hyper' }, 1],
54  [{ TERM_PROGRAM: 'MacTerm' }, 24],
55  [{ TERM_PROGRAM: 'Apple_Terminal' }, 8],
56  [{ TERM: 'xterm-256' }, 8],
57  [{ TERM: 'ansi' }, 4],
58  [{ TERM: 'ANSI' }, 4],
59  [{ TERM: 'color' }, 4],
60  [{ TERM: 'linux' }, 4],
61  [{ TERM: 'fail' }, 1],
62  [{ TERM: 'color', NODE_DISABLE_COLORS: '1' }, 1],
63  [{ TERM: 'dumb' }, 1],
64  [{ TERM: 'dumb', COLORTERM: '1' }, 1],
65  [{ TERM: 'terminator' }, 24],
66  [{ TERM: 'console' }, 4],
67  [{ COLORTERM: '24bit', FORCE_COLOR: '' }, 4],
68  [{ NO_COLOR: '1', FORCE_COLOR: '2' }, 8],
69  [{ NODE_DISABLE_COLORS: '1', FORCE_COLOR: '3' }, 24],
70  [{ NO_COLOR: '1', COLORTERM: '24bit' }, 1],
71  [{ NO_COLOR: '', COLORTERM: '24bit' }, 1],
72  [{ TMUX: '1', FORCE_COLOR: 0 }, 1],
73  [{ NO_COLOR: 'true', FORCE_COLOR: 0, COLORTERM: 'truecolor' }, 1],
74  [{ TERM: 'xterm-256color', COLORTERM: 'truecolor' }, 24],
75].forEach(([env, depth], i) => {
76  const actual = writeStream.getColorDepth(env);
77  assert.strictEqual(
78    actual,
79    depth,
80    `i: ${i}, expected: ${depth}, ` +
81      `actual: ${actual}, env: ${inspect(env)}`
82  );
83  const colors = 2 ** actual;
84  assert(writeStream.hasColors(colors, env));
85  assert(!writeStream.hasColors(colors + 1, env));
86  assert(depth >= 4 ? writeStream.hasColors(env) : !writeStream.hasColors(env));
87});
88
89// OS settings
90{
91  const platform = Object.getOwnPropertyDescriptor(process, 'platform');
92  const [ value, depth1, depth2 ] = process.platform !== 'win32' ?
93    ['win32', 1, 4] : ['linux', 4, 1];
94
95  assert.strictEqual(writeStream.getColorDepth({}), depth1);
96  Object.defineProperty(process, 'platform', { value });
97  assert.strictEqual(writeStream.getColorDepth({}), depth2);
98  Object.defineProperty(process, 'platform', platform);
99}
100