Skip to content

Commit 9760e04

Browse files
saljamrvagg
authored andcommitted
repl: don't use tty control codes when $TERM is set to "dumb"
This change stops the REPL from using ANSI control codes for colours when the TERM environment variable is set to "dumb". "dumb" is the terminal type with the smallest set of capabilities as described by terminfo. See: http://invisible-island.net/ncurses/terminfo.ti.html#toc-_Specials Related: nodejs/node-v0.x-archive#5344 Related: nodejs/node-v0.x-archive#25506 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Roman Reiss <[email protected]> PR-URL: #2712
1 parent f68fed2 commit 9760e04

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/internal/repl.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ function createRepl(env, opts, cb) {
3434
if (parseInt(env.NODE_NO_READLINE)) {
3535
opts.terminal = false;
3636
}
37-
if (parseInt(env.NODE_DISABLE_COLORS)) {
37+
// the "dumb" special terminal, as defined by terminfo, doesn't support
38+
// ANSI colour control codes.
39+
// see http://invisible-island.net/ncurses/terminfo.ti.html#toc-_Specials
40+
if (parseInt(env.NODE_DISABLE_COLORS) || env.TERM === 'dumb') {
3841
opts.useColors = false;
3942
}
4043

test/parallel/test-repl-envvars.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
// Flags: --expose-internals
4+
5+
const common = require('../common');
6+
const stream = require('stream');
7+
const REPL = require('internal/repl');
8+
const assert = require('assert');
9+
const inspect = require('util').inspect;
10+
11+
const tests = [{
12+
env: {},
13+
expected: { terminal: true, useColors: true }
14+
},
15+
{
16+
env: { NODE_DISABLE_COLORS: '1' },
17+
expected: { terminal: true, useColors: false }
18+
},
19+
{
20+
env: { NODE_NO_READLINE: '1' },
21+
expected: { terminal: false, useColors: false }
22+
},
23+
{
24+
env: { TERM: 'dumb' },
25+
expected: { terminal: true, useColors: false }
26+
},
27+
{
28+
env: { NODE_NO_READLINE: '1', NODE_DISABLE_COLORS: '1' },
29+
expected: { terminal: false, useColors: false }
30+
},
31+
{
32+
env: { NODE_NO_READLINE: '0' },
33+
expected: { terminal: true, useColors: true }
34+
}];
35+
36+
function run(test) {
37+
const env = test.env;
38+
const expected = test.expected;
39+
const opts = {
40+
terminal: true,
41+
input: new stream.Readable({ read() {} }),
42+
output: new stream.Writable({ write() {} })
43+
};
44+
45+
REPL.createInternalRepl(env, opts, function(err, repl) {
46+
if (err) throw err;
47+
assert.equal(expected.terminal, repl.terminal,
48+
'Expected ' + inspect(expected) + ' with ' + inspect(env));
49+
assert.equal(expected.useColors, repl.useColors,
50+
'Expected ' + inspect(expected) + ' with ' + inspect(env));
51+
repl.close();
52+
});
53+
}
54+
55+
tests.forEach(run);

0 commit comments

Comments
 (0)