From c156277acd1923dc8f7501fcf567dafc308e0b13 Mon Sep 17 00:00:00 2001 From: Salman Aljammaz Date: Sat, 5 Sep 2015 18:03:46 +0300 Subject: [PATCH] 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. http://invisible-island.net/ncurses/terminfo.ti.html#toc-_Specials Fixes nodejs/node-v0.x-archive#5344 --- lib/internal/repl.js | 5 ++- test/parallel/test-repl-envvars.js | 52 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-repl-envvars.js diff --git a/lib/internal/repl.js b/lib/internal/repl.js index b2c74e179c59d0..c1beb85cefd795 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -34,7 +34,10 @@ function createRepl(env, opts, cb) { if (parseInt(env.NODE_NO_READLINE)) { opts.terminal = false; } - if (parseInt(env.NODE_DISABLE_COLORS)) { + // the "dumb" special terminal, as defined by terminfo, doesn't support + // ANSI colour control codes. + // see http://invisible-island.net/ncurses/terminfo.ti.html#toc-_Specials + if (parseInt(env.NODE_DISABLE_COLORS) || env.TERM === 'dumb') { opts.useColors = false; } diff --git a/test/parallel/test-repl-envvars.js b/test/parallel/test-repl-envvars.js new file mode 100644 index 00000000000000..d765b69b4ba7be --- /dev/null +++ b/test/parallel/test-repl-envvars.js @@ -0,0 +1,52 @@ +'use strict'; + +// Flags: --expose-internals + +const common = require('../common'); +const stream = require('stream'); +const REPL = require('internal/repl'); +const assert = require('assert'); + +const tests = [{ + env: {}, + expected: { terminal: true, useColors: true } +}, +{ + env: { NODE_DISABLE_COLORS: "1" }, + expected: { terminal: true, useColors: false } +}, +{ + env: { NODE_NO_READLINE: "1" }, + expected: { terminal: false, useColors: false } +}, +{ + env: { TERM: "dumb" }, + expected: { terminal: true, useColors: false } +}, +{ + env: { NODE_NO_READLINE: "1", NODE_DISABLE_COLORS: "1" }, + expected: { terminal: false, useColors: false } +}, +{ + env: { NODE_NO_READLINE: "0" }, + expected: { terminal: true, useColors: true } +}]; + +function run(test) { + const env = test.env; + const expected = test.expected; + const opts = { + terminal: true, + input: new stream.Readable({ read() {} }), + output: new stream.Writable({ write() {} }) + } + + REPL.createInternalRepl(env, opts, function(err, repl) { + if (err) throw err; + assert.equal(expected.terminal, repl.terminal); + assert.equal(expected.useColors, repl.useColors); + repl.close(); + }); +} + +tests.forEach(run)