Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: support dumb terminals #26261

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ const consoleMethods = {
clear() {
// It only makes sense to clear if _stdout is a TTY.
// Otherwise, do nothing.
if (this._stdout.isTTY) {
if (this._stdout.isTTY && process.env.TERM !== 'dumb') {
// The require is here intentionally to avoid readline being
// required too early when console is first loaded.
const { cursorTo, clearScreenDown } = require('readline');
Expand Down
58 changes: 51 additions & 7 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const {
const { validateString } = require('internal/validators');
const { debug } = require('util');
const { emitExperimentalWarning } = require('internal/util');
const { Buffer } = require('buffer');
const EventEmitter = require('events');
const {
CSI,
Expand Down Expand Up @@ -161,6 +160,10 @@ function Interface(input, output, completer, terminal) {

this.terminal = !!terminal;

if (process.env.TERM === 'dumb') {
this._ttyWrite = _ttyWriteDumb.bind(this);
}

function ondata(data) {
self._normalWrite(data);
}
Expand Down Expand Up @@ -276,7 +279,7 @@ Interface.prototype._setRawMode = function(mode) {

Interface.prototype.prompt = function(preserveCursor) {
if (this.paused) this.resume();
if (this.terminal) {
if (this.terminal && process.env.TERM !== 'dumb') {
if (!preserveCursor) this.cursor = 0;
this._refreshLine();
} else {
Expand Down Expand Up @@ -417,7 +420,11 @@ Interface.prototype.resume = function() {

Interface.prototype.write = function(d, key) {
if (this.paused) this.resume();
this.terminal ? this._ttyWrite(d, key) : this._normalWrite(d);
if (this.terminal) {
this._ttyWrite(d, key);
} else {
this._normalWrite(d);
}
};

Interface.prototype._normalWrite = function(b) {
Expand Down Expand Up @@ -789,6 +796,46 @@ Interface.prototype._moveCursor = function(dx) {
}
};

function _ttyWriteDumb(s, key) {
key = key || {};

if (key.name === 'escape') return;

if (this._sawReturnAt && key.name !== 'enter')
this._sawReturnAt = 0;

if (key.ctrl && key.name === 'c') {
if (this.listenerCount('SIGINT') > 0) {
this.emit('SIGINT');
} else {
// This readline instance is finished
this.close();
}
}

switch (key.name) {
case 'return': // carriage return, i.e. \r
this._sawReturnAt = Date.now();
this._line();
break;

case 'enter':
// When key interval > crlfDelay
if (this._sawReturnAt === 0 ||
Date.now() - this._sawReturnAt > this.crlfDelay) {
this._line();
}
this._sawReturnAt = 0;
break;

default:
if (typeof s === 'string' && s) {
this.line += s;
this.cursor += s.length;
this._writeToOutput(s);
}
}
}

// handle a write from the tty
Interface.prototype._ttyWrite = function(s, key) {
Expand Down Expand Up @@ -1007,10 +1054,7 @@ Interface.prototype._ttyWrite = function(s, key) {
// falls through

default:
if (s instanceof Buffer)
s = s.toString('utf-8');

if (s) {
if (typeof s === 'string' && s) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
var lines = s.split(/\r\n|\n|\r/);
for (var i = 0, len = lines.length; i < len; i++) {
if (i > 0) {
Expand Down
4 changes: 3 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,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() > 2 : true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be > 1?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch while it won't have any actual impact (the return values are either 1, 4, 8 or 24).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got that check from lib/internal/console/constructor.js:256-258, should I change the check there too in this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wlodzislav I think it’s okay to leave that for a separate PR, but here it would be good to replace it with > 1 before this is being merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this while landing.

}
self.useColors = !!options.useColors;

Expand Down
9 changes: 9 additions & 0 deletions test/pseudo-tty/console-dumb-tty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';
require('../common');

process.env.TERM = 'dumb';

console.log({ foo: 'bar' });
console.dir({ foo: 'bar' });
console.log('%s q', 'string');
console.log('%o with object format param', { foo: 'bar' });
4 changes: 4 additions & 0 deletions test/pseudo-tty/console-dumb-tty.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ foo: 'bar' }
{ foo: 'bar' }
string q
{ foo: 'bar' } with object format param
21 changes: 21 additions & 0 deletions test/pseudo-tty/readline-dumb-tty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
require('../common');

process.env.TERM = 'dumb';

const readline = require('readline');

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.write('text');
rl.write(null, { ctrl: true, name: 'u' });
rl.write(null, { name: 'return' });
rl.write('text');
rl.write(null, { name: 'backspace' });
rl.write(null, { name: 'escape' });
rl.write(null, { name: 'enter' });
rl.write('text');
rl.write(null, { ctrl: true, name: 'c' });
3 changes: 3 additions & 0 deletions test/pseudo-tty/readline-dumb-tty.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
text
text
text
15 changes: 15 additions & 0 deletions test/pseudo-tty/repl-dumb-tty.js
Original file line number Diff line number Diff line change
@@ -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');
14 changes: 14 additions & 0 deletions test/pseudo-tty/repl-dumb-tty.out
Original file line number Diff line number Diff line change
@@ -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