aboutsummaryrefslogtreecommitdiff
path: root/lib/repl.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-03-08 12:21:35 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2019-03-25 16:28:07 +0100
commit97737fd5fb4a6b36c2e65905b8cf3518499a8a5e (patch)
tree0028cc7ca3c42d3474097f86d8b0c026a34d633d /lib/repl.js
parent96204c3c71f6cc571be56269ba9d584d615f0a06 (diff)
downloadandroid-node-v8-97737fd5fb4a6b36c2e65905b8cf3518499a8a5e.tar.gz
android-node-v8-97737fd5fb4a6b36c2e65905b8cf3518499a8a5e.tar.bz2
android-node-v8-97737fd5fb4a6b36c2e65905b8cf3518499a8a5e.zip
repl: fix terminal default setting
This makes sure that the described default behavior for the `terminal` option is actually always used and not only when running the REPL as standalone program. The options code is now logically combined instead of being spread out in the big REPL constructor. PR-URL: https://github.com/nodejs/node/pull/26518 Reviewed-By: Lance Ball <lball@redhat.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/repl.js')
-rw-r--r--lib/repl.js97
1 files changed, 50 insertions, 47 deletions
diff --git a/lib/repl.js b/lib/repl.js
index 7e186778a4..ea4f6ac7e7 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -140,47 +140,70 @@ function REPLServer(prompt,
replMode);
}
- var options, input, output, dom, breakEvalOnSigint;
+ let options;
if (prompt !== null && typeof prompt === 'object') {
- // an options object was given
- options = prompt;
+ // An options object was given.
+ options = { ...prompt };
stream = options.stream || options.socket;
- input = options.input;
- output = options.output;
eval_ = options.eval;
useGlobal = options.useGlobal;
ignoreUndefined = options.ignoreUndefined;
prompt = options.prompt;
- dom = options.domain;
replMode = options.replMode;
- breakEvalOnSigint = options.breakEvalOnSigint;
} else {
options = {};
}
- if (breakEvalOnSigint && eval_) {
+ if (!options.input && !options.output) {
+ // Legacy API, passing a 'stream'/'socket' option.
+ if (!stream) {
+ // Use stdin and stdout as the default streams if none were given.
+ stream = process;
+ }
+ // We're given a duplex readable/writable Stream, like a `net.Socket`
+ // or a custom object with 2 streams, or the `process` object.
+ options.input = stream.stdin || stream;
+ options.output = stream.stdout || stream;
+ }
+
+ if (options.terminal === undefined) {
+ options.terminal = options.output.isTTY;
+ }
+ options.terminal = !!options.terminal;
+
+ if (options.terminal && options.useColors === undefined) {
+ // If possible, check if stdout supports colors or not.
+ if (options.output.hasColors) {
+ options.useColors = options.output.hasColors();
+ } else if (process.env.NODE_DISABLE_COLORS === undefined) {
+ options.useColors = true;
+ }
+ }
+
+ this.inputStream = options.input;
+ this.outputStream = options.output;
+ this.useColors = !!options.useColors;
+ this._domain = options.domain || domain.create();
+ this.useGlobal = !!useGlobal;
+ this.ignoreUndefined = !!ignoreUndefined;
+ this.replMode = replMode || exports.REPL_MODE_SLOPPY;
+ this.underscoreAssigned = false;
+ this.last = undefined;
+ this.underscoreErrAssigned = false;
+ this.lastError = undefined;
+ this.breakEvalOnSigint = !!options.breakEvalOnSigint;
+ this.editorMode = false;
+ // Context id for use with the inspector protocol.
+ this[kContextId] = undefined;
+
+ if (this.breakEvalOnSigint && eval_) {
// Allowing this would not reflect user expectations.
// breakEvalOnSigint affects only the behavior of the default eval().
throw new ERR_INVALID_REPL_EVAL_CONFIG();
}
- var self = this;
-
- self._domain = dom || domain.create();
- self.useGlobal = !!useGlobal;
- self.ignoreUndefined = !!ignoreUndefined;
- self.replMode = replMode || exports.REPL_MODE_SLOPPY;
- self.underscoreAssigned = false;
- self.last = undefined;
- self.underscoreErrAssigned = false;
- self.lastError = undefined;
- self.breakEvalOnSigint = !!breakEvalOnSigint;
- self.editorMode = false;
- // Context id for use with the inspector protocol.
- self[kContextId] = undefined;
-
- let rli = self;
- Object.defineProperty(self, 'rli', {
+ let rli = this;
+ Object.defineProperty(this, 'rli', {
get: util.deprecate(() => rli,
'REPLServer.rli is deprecated', 'DEP0124'),
set: util.deprecate((val) => rli = val,
@@ -197,6 +220,8 @@ function REPLServer(prompt,
eval_ = eval_ || defaultEval;
+ const self = this;
+
// Pause taking in new input, and store the keys in a buffer.
const pausedBuffer = [];
let paused = false;
@@ -452,21 +477,6 @@ function REPLServer(prompt,
top.displayPrompt();
});
- if (!input && !output) {
- // legacy API, passing a 'stream'/'socket' option
- if (!stream) {
- // Use stdin and stdout as the default streams if none were given
- stream = process;
- }
- // We're given a duplex readable/writable Stream, like a `net.Socket`
- // or a custom object with 2 streams, or the `process` object
- input = stream.stdin || stream;
- output = stream.stdout || stream;
- }
-
- self.inputStream = input;
- self.outputStream = output;
-
self.resetContext();
self.lines.level = [];
@@ -503,13 +513,6 @@ function REPLServer(prompt,
// Figure out which "writer" function to use
self.writer = options.writer || exports.writer;
- if (options.useColors === undefined) {
- options.useColors = self.terminal && (
- typeof self.outputStream.getColorDepth === 'function' ?
- self.outputStream.getColorDepth() > 1 : true);
- }
- self.useColors = !!options.useColors;
-
if (self.writer === writer) {
// Conditionally turn on ANSI coloring.
writer.options.colors = self.useColors;