From 82b3ee776ba87481b5fa3d00cbe7f3c4f32c3ec3 Mon Sep 17 00:00:00 2001 From: Vladislav Kaminsky Date: Fri, 22 Feb 2019 16:13:52 +0400 Subject: [PATCH] repl: check colors with .getColorDepth() PR-URL: https://github.com/nodejs/node/pull/26261 Fixes: https://github.com/nodejs/node/issues/26187 Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Roman Reiss Reviewed-By: Rich Trott Reviewed-By: Jeremiah Senkpiel --- lib/repl.js | 4 +++- test/pseudo-tty/repl-dumb-tty.js | 15 +++++++++++++++ test/pseudo-tty/repl-dumb-tty.out | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/pseudo-tty/repl-dumb-tty.js create mode 100644 test/pseudo-tty/repl-dumb-tty.out diff --git a/lib/repl.js b/lib/repl.js index 60e07104dca11d..7e186778a48f8c 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -504,7 +504,9 @@ function REPLServer(prompt, self.writer = options.writer || exports.writer; if (options.useColors === undefined) { - options.useColors = self.terminal; + options.useColors = self.terminal && ( + typeof self.outputStream.getColorDepth === 'function' ? + self.outputStream.getColorDepth() > 1 : true); } self.useColors = !!options.useColors; diff --git a/test/pseudo-tty/repl-dumb-tty.js b/test/pseudo-tty/repl-dumb-tty.js new file mode 100644 index 00000000000000..08c63881d38b97 --- /dev/null +++ b/test/pseudo-tty/repl-dumb-tty.js @@ -0,0 +1,15 @@ +'use strict'; +require('../common'); + +process.env.TERM = 'dumb'; + +const repl = require('repl'); + +repl.start('> '); +process.stdin.push('console.log("foo")\n'); +process.stdin.push('1 + 2\n'); +process.stdin.push('"str"\n'); +process.stdin.push('console.dir({ a: 1 })\n'); +process.stdin.push('{ a: 1 }\n'); +process.stdin.push('\n'); +process.stdin.push('.exit\n'); diff --git a/test/pseudo-tty/repl-dumb-tty.out b/test/pseudo-tty/repl-dumb-tty.out new file mode 100644 index 00000000000000..69eb4e5da6313e --- /dev/null +++ b/test/pseudo-tty/repl-dumb-tty.out @@ -0,0 +1,14 @@ +> console.log("foo") +foo +undefined +> 1 + 2 +3 +> "str" +'str' +> console.dir({ a: 1 }) +{ a: 1 } +undefined +> { a: 1 } +{ a: 1 } +> +> .exit