2012-02-27 11:09:35 -08:00
|
|
|
# TTY
|
2010-11-25 19:09:28 -08:00
|
|
|
|
2015-02-24 16:15:26 -08:00
|
|
|
Stability: 2 - Stable
|
2012-03-02 15:14:03 -08:00
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
The `tty` module houses the `tty.ReadStream` and `tty.WriteStream` classes. In
|
|
|
|
most cases, you will not need to use this module directly.
|
|
|
|
|
2015-08-23 16:40:28 -04:00
|
|
|
When Node.js detects that it is being run inside a TTY context, then `process.stdin`
|
2012-03-26 15:21:25 -07:00
|
|
|
will be a `tty.ReadStream` instance and `process.stdout` will be
|
2015-08-23 16:40:28 -04:00
|
|
|
a `tty.WriteStream` instance. The preferred way to check if Node.js is being run
|
2015-02-07 11:25:13 +01:00
|
|
|
in a TTY context is to check `process.stdout.isTTY`:
|
2011-02-20 13:53:40 -08:00
|
|
|
|
2015-08-13 12:14:34 -04:00
|
|
|
$ node -p -e "Boolean(process.stdout.isTTY)"
|
2012-03-26 15:21:25 -07:00
|
|
|
true
|
2015-08-13 12:14:34 -04:00
|
|
|
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
|
2012-03-26 15:21:25 -07:00
|
|
|
false
|
2011-02-20 13:53:40 -08:00
|
|
|
|
2012-03-26 15:21:25 -07:00
|
|
|
## Class: ReadStream
|
|
|
|
|
|
|
|
A `net.Socket` subclass that represents the readable portion of a tty. In normal
|
|
|
|
circumstances, `process.stdin` will be the only `tty.ReadStream` instance in any
|
2015-08-23 16:40:28 -04:00
|
|
|
Node.js program (only when `isatty(0)` is true).
|
2012-03-26 15:21:25 -07:00
|
|
|
|
|
|
|
### rs.isRaw
|
|
|
|
|
|
|
|
A `Boolean` that is initialized to `false`. It represents the current "raw" state
|
|
|
|
of the `tty.ReadStream` instance.
|
|
|
|
|
|
|
|
### rs.setRawMode(mode)
|
|
|
|
|
|
|
|
`mode` should be `true` or `false`. This sets the properties of the
|
|
|
|
`tty.ReadStream` to act either as a raw device or default. `isRaw` will be set
|
|
|
|
to the resulting mode.
|
|
|
|
|
2013-03-26 16:09:48 +01:00
|
|
|
## Class: WriteStream
|
2012-03-26 15:21:25 -07:00
|
|
|
|
|
|
|
A `net.Socket` subclass that represents the writable portion of a tty. In normal
|
|
|
|
circumstances, `process.stdout` will be the only `tty.WriteStream` instance
|
|
|
|
ever created (and only when `isatty(1)` is true).
|
|
|
|
|
|
|
|
### Event: 'resize'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
|
|
|
Emitted by `refreshSize()` when either of the `columns` or `rows` properties
|
|
|
|
has changed.
|
|
|
|
|
2015-12-14 15:20:25 -08:00
|
|
|
process.stdout.on('resize', () => {
|
2012-03-26 15:21:25 -07:00
|
|
|
console.log('screen size has changed!');
|
2015-12-14 15:20:25 -08:00
|
|
|
console.log(`${process.stdout.columns}x${process.stdout.rows}`);
|
2012-03-26 15:21:25 -07:00
|
|
|
});
|
2015-08-20 04:07:52 +05:30
|
|
|
|
2015-11-04 12:33:35 -05:00
|
|
|
### ws.columns
|
2015-08-20 04:07:52 +05:30
|
|
|
|
2015-11-04 12:33:35 -05:00
|
|
|
A `Number` that gives the number of columns the TTY currently has. This property
|
2015-11-27 18:30:32 -05:00
|
|
|
gets updated on `'resize'` events.
|
2015-11-04 12:33:35 -05:00
|
|
|
|
|
|
|
### ws.rows
|
|
|
|
|
|
|
|
A `Number` that gives the number of rows the TTY currently has. This property
|
2015-11-27 18:30:32 -05:00
|
|
|
gets updated on `'resize'` events.
|
2015-11-04 12:33:35 -05:00
|
|
|
|
|
|
|
## tty.isatty(fd)
|
|
|
|
|
|
|
|
Returns `true` or `false` depending on if the `fd` is associated with a
|
|
|
|
terminal.
|
|
|
|
|
2015-11-13 19:21:49 -08:00
|
|
|
[tty.ReadStream#setRawMode]: #tty_rs_setrawmode_mode
|