3 const util = require('util');
4 const internalUtil = require('internal/util');
5 const net = require('net');
6 const TTY = process.binding('tty_wrap').TTY;
7 const isTTY = process.binding('tty_wrap').isTTY;
8 const inherits = util.inherits;
9 const errnoException = util._errnoException;
12 exports.isatty = function(fd) {
18 exports.setRawMode = internalUtil.deprecate(function(flag) {
19 if (!process.stdin.isTTY) {
20 throw new Error('can\'t set raw mode on non-tty');
22 process.stdin.setRawMode(flag);
23 }, 'tty.setRawMode is deprecated. ' +
24 'Use process.stdin.setRawMode instead.');
27 function ReadStream(fd, options) {
28 if (!(this instanceof ReadStream))
29 return new ReadStream(fd, options);
31 options = util._extend({
35 handle: new TTY(fd, true)
38 net.Socket.call(this, options);
43 inherits(ReadStream, net.Socket);
45 exports.ReadStream = ReadStream;
47 ReadStream.prototype.setRawMode = function(flag) {
49 this._handle.setRawMode(flag);
54 function WriteStream(fd) {
55 if (!(this instanceof WriteStream)) return new WriteStream(fd);
56 net.Socket.call(this, {
57 handle: new TTY(fd, false),
63 var err = this._handle.getWindowSize(winSize);
65 this.columns = winSize[0];
66 this.rows = winSize[1];
69 inherits(WriteStream, net.Socket);
70 exports.WriteStream = WriteStream;
73 WriteStream.prototype.isTTY = true;
76 WriteStream.prototype._refreshSize = function() {
77 var oldCols = this.columns;
78 var oldRows = this.rows;
80 var err = this._handle.getWindowSize(winSize);
82 this.emit('error', errnoException(err, 'getWindowSize'));
85 var newCols = winSize[0];
86 var newRows = winSize[1];
87 if (oldCols !== newCols || oldRows !== newRows) {
88 this.columns = newCols;
96 WriteStream.prototype.cursorTo = function(x, y) {
97 require('readline').cursorTo(this, x, y);
99 WriteStream.prototype.moveCursor = function(dx, dy) {
100 require('readline').moveCursor(this, dx, dy);
102 WriteStream.prototype.clearLine = function(dir) {
103 require('readline').clearLine(this, dir);
105 WriteStream.prototype.clearScreenDown = function() {
106 require('readline').clearScreenDown(this);
108 WriteStream.prototype.getWindowSize = function() {
109 return [this.columns, this.rows];