1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
24 var inherits = require('util').inherits;
25 var net = require('net');
26 var TTY = process.binding('tty_wrap').TTY;
27 var isTTY = process.binding('tty_wrap').isTTY;
28 var util = require('util');
30 var errnoException = util._errnoException;
33 exports.isatty = function(fd) {
39 exports.setRawMode = util.deprecate(function(flag) {
40 if (!process.stdin.isTTY) {
41 throw new Error('can\'t set raw mode on non-tty');
43 process.stdin.setRawMode(flag);
44 }, 'tty.setRawMode: Use `process.stdin.setRawMode()` instead.');
47 function ReadStream(fd, options) {
48 if (!(this instanceof ReadStream))
49 return new ReadStream(fd, options);
51 options = util._extend({
55 handle: new TTY(fd, true)
58 net.Socket.call(this, options);
63 inherits(ReadStream, net.Socket);
65 exports.ReadStream = ReadStream;
67 ReadStream.prototype.setRawMode = function(flag) {
69 this._handle.setRawMode(flag);
75 function WriteStream(fd) {
76 if (!(this instanceof WriteStream)) return new WriteStream(fd);
77 net.Socket.call(this, {
78 handle: new TTY(fd, false),
84 var err = this._handle.getWindowSize(winSize);
86 this.columns = winSize[0];
87 this.rows = winSize[1];
90 inherits(WriteStream, net.Socket);
91 exports.WriteStream = WriteStream;
94 WriteStream.prototype.isTTY = true;
97 WriteStream.prototype._refreshSize = function() {
98 var oldCols = this.columns;
99 var oldRows = this.rows;
101 var err = this._handle.getWindowSize(winSize);
103 this.emit('error', errnoException(err, 'getWindowSize'));
106 var newCols = winSize[0];
107 var newRows = winSize[1];
108 if (oldCols !== newCols || oldRows !== newRows) {
109 this.columns = newCols;
117 WriteStream.prototype.cursorTo = function(x, y) {
118 require('readline').cursorTo(this, x, y);
120 WriteStream.prototype.moveCursor = function(dx, dy) {
121 require('readline').moveCursor(this, dx, dy);
123 WriteStream.prototype.clearLine = function(dir) {
124 require('readline').clearLine(this, dir);
126 WriteStream.prototype.clearScreenDown = function() {
127 require('readline').clearScreenDown(this);
129 WriteStream.prototype.getWindowSize = function() {
130 return [this.columns, this.rows];