summaryrefslogtreecommitdiff
path: root/lib/tty_win32.js
blob: 6ca011a33300651fb20b2577257887dbf2a05c96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

var binding = process.binding('stdio'),
    Stream = require('stream').Stream,
    inherits = require('util').inherits,
    writeTTY = binding.writeTTY,
    closeTTY = binding.closeTTY;


function ReadStream(fd) {
  if (!(this instanceof ReadStream)) return new ReadStream(fd);
  Stream.call(this);

  var self = this;
  this.fd = fd;
  this.paused = true;
  this.readable = true;

  var dataListeners = this.listeners('data'),
      dataUseString = false;

  function onError(err) {
    self.emit('error', err);
  }
  function onKeypress(char, key) {
    self.emit('keypress', char, key);
    if (dataListeners.length && char) {
      self.emit('data', dataUseString ? char : new Buffer(char, 'utf-8'));
    }
  }
  function onResize() {
    process.emit('SIGWINCH');
  }

  // A windows console stream is always UTF-16, actually
  // So it makes no sense to set the encoding, however someone could call
  // setEncoding to tell us he wants strings not buffers
  this.setEncoding = function(encoding) {
    dataUseString = !!encoding;
  }

  binding.initTTYWatcher(fd, onError, onKeypress, onResize);
}
inherits(ReadStream, Stream);
exports.ReadStream = ReadStream;

ReadStream.prototype.isTTY = true;

ReadStream.prototype.pause = function() {
  if (this.paused)
    return;

  this.paused = true;
  binding.stopTTYWatcher();
};

ReadStream.prototype.resume = function() {
  if (!this.readable)
    throw new Error('Cannot resume() closed tty.ReadStream.');
  if (!this.paused)
    return;

  this.paused = false;
  binding.startTTYWatcher();
};

ReadStream.prototype.destroy = function() {
  if (!this.readable)
    return;

  this.readable = false;
  binding.destroyTTYWatcher();

  var self = this;
  process.nextTick(function() {
    try {
      closeTTY(self.fd);
      self.emit('close');
    } catch (err) {
      self.emit('error', err);
    }
  });
};

ReadStream.prototype.destroySoon = ReadStream.prototype.destroy;


function WriteStream(fd) {
  if (!(this instanceof WriteStream)) return new WriteStream(fd);
  Stream.call(this);

  var self = this;
  this.fd = fd;
  this.writable = true;
}
inherits(WriteStream, Stream);
exports.WriteStream = WriteStream;

WriteStream.prototype.isTTY = true;

WriteStream.prototype.write = function(data, encoding) {
  if (!this.writable) {
    throw new Error('stream not writable');
  }

  if (Buffer.isBuffer(data)) {
    data = data.toString('utf-8');
  }

  try {
    writeTTY(this.fd, data);
  } catch (err) {
    this.emit('error', err);
  }
  return true;
};

WriteStream.prototype.end = function(data, encoding) {
  if (data) {
    this.write(data, encoding);
  }
  this.destroy();
};

WriteStream.prototype.destroy = function() {
  if (!this.writable)
    return;

  this.writable = false;

  var self = this;
  process.nextTick(function() {
    var closed = false;
    try {
      closeTTY(self.fd);
      closed = true;
    } catch (err) {
      self.emit('error', err);
    }
    if (closed) {
      self.emit('close');
    }
  });
};

WriteStream.prototype.moveCursor = function(dx, dy) {
  if (!this.writable)
    throw new Error('Stream not writable');

  binding.moveCursor(this.fd, dx, dy);
};

WriteStream.prototype.cursorTo = function(x, y) {
  if (!this.writable)
    throw new Error('Stream not writable');

  binding.cursorTo(this.fd, x, y);
};

WriteStream.prototype.clearLine = function(direction) {
  if (!this.writable)
    throw new Error('Stream not writable');

  binding.clearLine(this.fd, direction || 0);
};