repl: add a 'useColors' option to the repl
This should only be minimally used, since the `terminal` value will usually be what you are expecting. This option is specifically for the case where `terminal` is false, but you still want colors to be output (or vice-versa).
This commit is contained in:
parent
a33d1c959a
commit
208b230744
@ -47,6 +47,10 @@ takes the following values:
|
|||||||
- `eval` - function that will be used to eval each given line. Defaults to
|
- `eval` - function that will be used to eval each given line. Defaults to
|
||||||
an async wrapper for `eval()`. See below for an example of a custom `eval`.
|
an async wrapper for `eval()`. See below for an example of a custom `eval`.
|
||||||
|
|
||||||
|
- `useColors` - a boolean which specifies whether or not the `writer` function
|
||||||
|
should output colors. If a different `writer` function is set then this does
|
||||||
|
nothing. Defaults to the repl's `terminal` value.
|
||||||
|
|
||||||
- `useGlobal` - if set to `true`, then the repl will use the `global` object,
|
- `useGlobal` - if set to `true`, then the repl will use the `global` object,
|
||||||
instead of running scripts in a separate context. Defaults to `false`.
|
instead of running scripts in a separate context. Defaults to `false`.
|
||||||
|
|
||||||
|
@ -690,14 +690,14 @@ var helpMessage = 'Commands: ' + commands.map(function(group) {
|
|||||||
}).join(',\n');
|
}).join(',\n');
|
||||||
|
|
||||||
|
|
||||||
function SourceUnderline(sourceText, position, tty) {
|
function SourceUnderline(sourceText, position, repl) {
|
||||||
if (!sourceText) return '';
|
if (!sourceText) return '';
|
||||||
|
|
||||||
var head = sourceText.slice(0, position),
|
var head = sourceText.slice(0, position),
|
||||||
tail = sourceText.slice(position);
|
tail = sourceText.slice(position);
|
||||||
|
|
||||||
// Colourize char if stdout supports colours
|
// Colourize char if stdout supports colours
|
||||||
if (tty && !repl.disableColors) {
|
if (repl.useColors) {
|
||||||
tail = tail.replace(/(.+?)([^\w]|$)/, '\033[32m$1\033[39m$2');
|
tail = tail.replace(/(.+?)([^\w]|$)/, '\033[32m$1\033[39m$2');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -758,6 +758,9 @@ function Interface(stdin, stdout, args) {
|
|||||||
if (parseInt(process.env['NODE_NO_READLINE'], 10)) {
|
if (parseInt(process.env['NODE_NO_READLINE'], 10)) {
|
||||||
opts.terminal = false;
|
opts.terminal = false;
|
||||||
}
|
}
|
||||||
|
if (parseInt(process.env['NODE_DISABLE_COLORS'], 10)) {
|
||||||
|
opts.useColors = false;
|
||||||
|
}
|
||||||
this.repl = repl.start(opts);
|
this.repl = repl.start(opts);
|
||||||
|
|
||||||
// Do not print useless warning
|
// Do not print useless warning
|
||||||
@ -1134,7 +1137,7 @@ Interface.prototype.list = function(delta) {
|
|||||||
if (current) {
|
if (current) {
|
||||||
line = SourceUnderline(lines[i],
|
line = SourceUnderline(lines[i],
|
||||||
client.currentSourceColumn,
|
client.currentSourceColumn,
|
||||||
self.stdout.isTTY);
|
self.repl);
|
||||||
} else {
|
} else {
|
||||||
line = lines[i];
|
line = lines[i];
|
||||||
}
|
}
|
||||||
|
10
lib/repl.js
10
lib/repl.js
@ -58,8 +58,6 @@ function hasOwnProperty(obj, prop) {
|
|||||||
|
|
||||||
var context;
|
var context;
|
||||||
|
|
||||||
exports.disableColors = process.env.NODE_DISABLE_COLORS ? true : false;
|
|
||||||
|
|
||||||
// hack for require.resolve("./relative") to work properly.
|
// hack for require.resolve("./relative") to work properly.
|
||||||
module.filename = path.resolve('repl');
|
module.filename = path.resolve('repl');
|
||||||
|
|
||||||
@ -163,8 +161,12 @@ function REPLServer(prompt, stream, eval, useGlobal, ignoreUndefined) {
|
|||||||
// figure out which "writer" function to use
|
// figure out which "writer" function to use
|
||||||
self.writer = options.writer || exports.writer;
|
self.writer = options.writer || exports.writer;
|
||||||
|
|
||||||
if (rli.terminal && !exports.disableColors &&
|
if (typeof options.useColors === 'undefined') {
|
||||||
self.writer === util.inspect) {
|
options.useColors = rli.terminal;
|
||||||
|
}
|
||||||
|
self.useColors = !!options.useColors;
|
||||||
|
|
||||||
|
if (self.useColors && self.writer === util.inspect) {
|
||||||
// Turn on ANSI coloring.
|
// Turn on ANSI coloring.
|
||||||
self.writer = function(obj, showHidden, depth) {
|
self.writer = function(obj, showHidden, depth) {
|
||||||
return util.inspect(obj, showHidden, depth, true);
|
return util.inspect(obj, showHidden, depth, true);
|
||||||
|
@ -107,6 +107,9 @@
|
|||||||
if (parseInt(process.env['NODE_NO_READLINE'], 10)) {
|
if (parseInt(process.env['NODE_NO_READLINE'], 10)) {
|
||||||
opts.terminal = false;
|
opts.terminal = false;
|
||||||
}
|
}
|
||||||
|
if (parseInt(process.env['NODE_DISABLE_COLORS'], 10)) {
|
||||||
|
opts.useColors = false;
|
||||||
|
}
|
||||||
var repl = Module.requireRepl().start(opts);
|
var repl = Module.requireRepl().start(opts);
|
||||||
repl.on('exit', function() {
|
repl.on('exit', function() {
|
||||||
process.exit();
|
process.exit();
|
||||||
|
@ -40,6 +40,7 @@ assert.equal(r1.rli.output, stream);
|
|||||||
assert.equal(r1.rli.input, r1.inputStream);
|
assert.equal(r1.rli.input, r1.inputStream);
|
||||||
assert.equal(r1.rli.output, r1.outputStream);
|
assert.equal(r1.rli.output, r1.outputStream);
|
||||||
assert.equal(r1.rli.terminal, true);
|
assert.equal(r1.rli.terminal, true);
|
||||||
|
assert.equal(r1.useColors, r1.rli.terminal);
|
||||||
assert.equal(r1.useGlobal, false);
|
assert.equal(r1.useGlobal, false);
|
||||||
assert.equal(r1.ignoreUndefined, false);
|
assert.equal(r1.ignoreUndefined, false);
|
||||||
|
|
||||||
@ -50,6 +51,7 @@ var r2 = repl.start({
|
|||||||
input: stream,
|
input: stream,
|
||||||
output: stream,
|
output: stream,
|
||||||
terminal: false,
|
terminal: false,
|
||||||
|
useColors: true,
|
||||||
useGlobal: true,
|
useGlobal: true,
|
||||||
ignoreUndefined: true,
|
ignoreUndefined: true,
|
||||||
eval: evaler,
|
eval: evaler,
|
||||||
@ -60,6 +62,7 @@ assert.equal(r2.rli.output, stream);
|
|||||||
assert.equal(r2.rli.input, r2.inputStream);
|
assert.equal(r2.rli.input, r2.inputStream);
|
||||||
assert.equal(r2.rli.output, r2.outputStream);
|
assert.equal(r2.rli.output, r2.outputStream);
|
||||||
assert.equal(r2.rli.terminal, false);
|
assert.equal(r2.rli.terminal, false);
|
||||||
|
assert.equal(r2.useColors, true);
|
||||||
assert.equal(r2.useGlobal, true);
|
assert.equal(r2.useGlobal, true);
|
||||||
assert.equal(r2.ignoreUndefined, true);
|
assert.equal(r2.ignoreUndefined, true);
|
||||||
assert.equal(r2.eval, evaler);
|
assert.equal(r2.eval, evaler);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user