lib: turn on strict mode
[platform/upstream/nodejs.git] / lib / tty.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 'use strict';
23
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');
29
30 var errnoException = util._errnoException;
31
32
33 exports.isatty = function(fd) {
34   return isTTY(fd);
35 };
36
37
38 // backwards-compat
39 exports.setRawMode = util.deprecate(function(flag) {
40   if (!process.stdin.isTTY) {
41     throw new Error('can\'t set raw mode on non-tty');
42   }
43   process.stdin.setRawMode(flag);
44 }, 'tty.setRawMode: Use `process.stdin.setRawMode()` instead.');
45
46
47 function ReadStream(fd, options) {
48   if (!(this instanceof ReadStream))
49     return new ReadStream(fd, options);
50
51   options = util._extend({
52     highWaterMark: 0,
53     readable: true,
54     writable: false,
55     handle: new TTY(fd, true)
56   }, options);
57
58   net.Socket.call(this, options);
59
60   this.isRaw = false;
61   this.isTTY = true;
62 }
63 inherits(ReadStream, net.Socket);
64
65 exports.ReadStream = ReadStream;
66
67 ReadStream.prototype.setRawMode = function(flag) {
68   flag = !!flag;
69   this._handle.setRawMode(flag);
70   this.isRaw = flag;
71 };
72
73
74
75 function WriteStream(fd) {
76   if (!(this instanceof WriteStream)) return new WriteStream(fd);
77   net.Socket.call(this, {
78     handle: new TTY(fd, false),
79     readable: false,
80     writable: true
81   });
82
83   var winSize = [];
84   var err = this._handle.getWindowSize(winSize);
85   if (!err) {
86     this.columns = winSize[0];
87     this.rows = winSize[1];
88   }
89 }
90 inherits(WriteStream, net.Socket);
91 exports.WriteStream = WriteStream;
92
93
94 WriteStream.prototype.isTTY = true;
95
96
97 WriteStream.prototype._refreshSize = function() {
98   var oldCols = this.columns;
99   var oldRows = this.rows;
100   var winSize = [];
101   var err = this._handle.getWindowSize(winSize);
102   if (err) {
103     this.emit('error', errnoException(err, 'getWindowSize'));
104     return;
105   }
106   var newCols = winSize[0];
107   var newRows = winSize[1];
108   if (oldCols !== newCols || oldRows !== newRows) {
109     this.columns = newCols;
110     this.rows = newRows;
111     this.emit('resize');
112   }
113 };
114
115
116 // backwards-compat
117 WriteStream.prototype.cursorTo = function(x, y) {
118   require('readline').cursorTo(this, x, y);
119 };
120 WriteStream.prototype.moveCursor = function(dx, dy) {
121   require('readline').moveCursor(this, dx, dy);
122 };
123 WriteStream.prototype.clearLine = function(dir) {
124   require('readline').clearLine(this, dir);
125 };
126 WriteStream.prototype.clearScreenDown = function() {
127   require('readline').clearScreenDown(this);
128 };
129 WriteStream.prototype.getWindowSize = function() {
130   return [this.columns, this.rows];
131 };