tty: refactor to es6

PR-URL: https://github.com/nodejs/node/pull/17615
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Ruben Bridgewater
2018-01-17 19:38:51 +01:00
parent bb9cedb0f0
commit 373e893ee0

View File

@@ -21,11 +21,9 @@
'use strict';
const util = require('util');
const { inherits, _errnoException, _extend } = require('util');
const net = require('net');
const { TTY, isTTY } = process.binding('tty_wrap');
const { inherits } = util;
const errnoException = util._errnoException;
const errors = require('internal/errors');
const readline = require('readline');
const { release } = require('os');
@@ -41,7 +39,6 @@ function isatty(fd) {
return Number.isInteger(fd) && fd >= 0 && isTTY(fd);
}
function ReadStream(fd, options) {
if (!(this instanceof ReadStream))
return new ReadStream(fd, options);
@@ -54,7 +51,7 @@ function ReadStream(fd, options) {
throw new errors.SystemError(ctx);
}
options = util._extend({
options = _extend({
highWaterMark: 0,
readable: true,
writable: false,
@@ -74,7 +71,6 @@ ReadStream.prototype.setRawMode = function(flag) {
this.isRaw = flag;
};
function WriteStream(fd) {
if (!(this instanceof WriteStream))
return new WriteStream(fd);
@@ -100,8 +96,8 @@ function WriteStream(fd) {
// Ref: https://github.com/nodejs/node/pull/1771#issuecomment-119351671
this._handle.setBlocking(true);
var winSize = new Array(2);
var err = this._handle.getWindowSize(winSize);
const winSize = new Array(2);
const err = this._handle.getWindowSize(winSize);
if (!err) {
this.columns = winSize[0];
this.rows = winSize[1];
@@ -109,7 +105,6 @@ function WriteStream(fd) {
}
inherits(WriteStream, net.Socket);
WriteStream.prototype.isTTY = true;
WriteStream.prototype.getColorDepth = function(env = process.env) {
@@ -178,16 +173,15 @@ WriteStream.prototype.getColorDepth = function(env = process.env) {
};
WriteStream.prototype._refreshSize = function() {
var oldCols = this.columns;
var oldRows = this.rows;
var winSize = new Array(2);
var err = this._handle.getWindowSize(winSize);
const oldCols = this.columns;
const oldRows = this.rows;
const winSize = new Array(2);
const err = this._handle.getWindowSize(winSize);
if (err) {
this.emit('error', errnoException(err, 'getWindowSize'));
this.emit('error', _errnoException(err, 'getWindowSize'));
return;
}
var newCols = winSize[0];
var newRows = winSize[1];
const [newCols, newRows] = winSize;
if (oldCols !== newCols || oldRows !== newRows) {
this.columns = newCols;
this.rows = newRows;
@@ -195,8 +189,7 @@ WriteStream.prototype._refreshSize = function() {
}
};
// backwards-compat
// Backwards-compat
WriteStream.prototype.cursorTo = function(x, y) {
readline.cursorTo(this, x, y);
};
@@ -213,5 +206,4 @@ WriteStream.prototype.getWindowSize = function() {
return [this.columns, this.rows];
};
module.exports = { isatty, ReadStream, WriteStream };