bare-tty 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +44 -17
  2. package/package.json +3 -2
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /* global Bare */
2
- const { Readable, Writable } = require('streamx')
2
+ const { Readable, Writable } = require('bare-stream')
3
+ const Signal = require('bare-signals')
3
4
  const binding = require('./binding')
4
5
  const constants = require('./lib/constants')
5
6
 
@@ -37,19 +38,18 @@ exports.ReadStream = class TTYReadStream extends Readable {
37
38
 
38
39
  setMode (mode) {
39
40
  binding.setMode(this._handle, mode)
41
+ return this
40
42
  }
41
43
 
42
44
  setRawMode (mode) {
43
- this.setMode(mode ? constants.mode.RAW : constants.mode.NORMAL)
45
+ return this.setMode(mode ? constants.mode.RAW : constants.mode.NORMAL)
44
46
  }
45
47
 
46
- _read (cb) {
48
+ _read () {
47
49
  if ((this._state & constants.state.READING) === 0) {
48
50
  this._state |= constants.state.READING
49
51
  binding.resume(this._handle)
50
52
  }
51
-
52
- cb(null)
53
53
  }
54
54
 
55
55
  _predestroy () {
@@ -59,8 +59,8 @@ exports.ReadStream = class TTYReadStream extends Readable {
59
59
  TTYReadStream._streams.delete(this)
60
60
  }
61
61
 
62
- _destroy (cb) {
63
- if (this._state & constants.state.CLOSING) return cb(null)
62
+ _destroy (err, cb) {
63
+ if (this._state & constants.state.CLOSING) return cb(err)
64
64
  this._state |= constants.state.CLOSING
65
65
  this._pendingDestroy = cb
66
66
  binding.close(this._handle)
@@ -105,9 +105,10 @@ exports.ReadStream = class TTYReadStream extends Readable {
105
105
 
106
106
  exports.WriteStream = class TTYWriteStream extends Writable {
107
107
  constructor (fd, opts = {}) {
108
- super({ mapWritable })
108
+ super()
109
109
 
110
110
  this._state = 0
111
+ this._size = null
111
112
 
112
113
  this._pendingWrite = null
113
114
  this._pendingDestroy = null
@@ -118,6 +119,9 @@ exports.WriteStream = class TTYWriteStream extends Writable {
118
119
  this._onclose
119
120
  )
120
121
 
122
+ this._size = this.getWindowSize()
123
+
124
+ if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.start()
121
125
  TTYWriteStream._streams.add(this)
122
126
  }
123
127
 
@@ -125,13 +129,21 @@ exports.WriteStream = class TTYWriteStream extends Writable {
125
129
  return true
126
130
  }
127
131
 
132
+ get columns () {
133
+ return this._size[0]
134
+ }
135
+
136
+ get rows () {
137
+ return this._size[1]
138
+ }
139
+
128
140
  getWindowSize () {
129
141
  return binding.getWindowSize(this._handle)
130
142
  }
131
143
 
132
- _writev (datas, cb) {
133
- this._pendingWrite = [cb, datas]
134
- binding.writev(this._handle, datas)
144
+ _writev (batch, cb) {
145
+ this._pendingWrite = [cb, batch]
146
+ binding.writev(this._handle, batch.map(({ chunk }) => chunk))
135
147
  }
136
148
 
137
149
  _predestroy () {
@@ -139,14 +151,16 @@ exports.WriteStream = class TTYWriteStream extends Writable {
139
151
  this._state |= constants.state.CLOSING
140
152
  binding.close(this._handle)
141
153
  TTYWriteStream._streams.delete(this)
154
+ if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.stop()
142
155
  }
143
156
 
144
- _destroy (cb) {
145
- if (this._state & constants.state.CLOSING) return cb(null)
157
+ _destroy (err, cb) {
158
+ if (this._state & constants.state.CLOSING) return cb(err)
146
159
  this._state |= constants.state.CLOSING
147
160
  this._pendingDestroy = cb
148
161
  binding.close(this._handle)
149
162
  TTYWriteStream._streams.delete(this)
163
+ if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.stop()
150
164
  }
151
165
 
152
166
  _continueWrite (err) {
@@ -172,13 +186,30 @@ exports.WriteStream = class TTYWriteStream extends Writable {
172
186
  this._continueDestroy()
173
187
  }
174
188
 
189
+ _onresize () {
190
+ this._size = this.getWindowSize()
191
+ this.emit('resize')
192
+ }
193
+
175
194
  static _streams = new Set()
195
+
196
+ static _resize = new Signal('SIGWINCH')
176
197
  }
177
198
 
178
199
  exports.constants = constants
179
200
 
180
201
  exports.isTTY = binding.isTTY
181
202
 
203
+ exports.isatty = exports.isTTY // For Node.js compatibility
204
+
205
+ exports.WriteStream._resize
206
+ .on('signal', () => {
207
+ for (const stream of exports.WriteStream._streams) {
208
+ stream._onresize()
209
+ }
210
+ })
211
+ .unref()
212
+
182
213
  Bare
183
214
  .on('exit', () => {
184
215
  for (const stream of exports.ReadStream._streams) {
@@ -193,7 +224,3 @@ Bare
193
224
  const empty = Buffer.alloc(0)
194
225
 
195
226
  function noop () {}
196
-
197
- function mapWritable (buf) {
198
- return typeof buf === 'string' ? Buffer.from(buf) : buf
199
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-tty",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "Native TTY streams for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -31,7 +31,8 @@
31
31
  "homepage": "https://github.com/holepunchto/bare-tty#readme",
32
32
  "dependencies": {
33
33
  "bare-events": "^2.2.0",
34
- "streamx": "^2.13.2"
34
+ "bare-signals": "^2.2.0",
35
+ "bare-stream": "^2.0.0"
35
36
  },
36
37
  "devDependencies": {
37
38
  "brittle": "^3.2.1",