2012-02-27 20:09:35 +01:00
|
|
|
# TTY
|
2010-11-26 04:09:28 +01:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2012-03-27 00:21:25 +02: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-13 18:14:34 +02:00
|
|
|
When node.js detects that it is being run inside a TTY context, then `process.stdin`
|
2012-03-27 00:21:25 +02:00
|
|
|
will be a `tty.ReadStream` instance and `process.stdout` will be
|
2015-08-13 18:14:34 +02: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 22:53:40 +01:00
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
$ node -p -e "Boolean(process.stdout.isTTY)"
|
2012-03-27 00:21:25 +02:00
|
|
|
true
|
2015-08-13 18:14:34 +02:00
|
|
|
$ node -p -e "Boolean(process.stdout.isTTY)" | cat
|
2012-03-27 00:21:25 +02:00
|
|
|
false
|
2011-02-20 22:53:40 +01:00
|
|
|
|
2010-11-26 04:09:28 +01:00
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
## tty.isatty(fd)
|
2010-11-26 04:09:28 +01:00
|
|
|
|
|
|
|
Returns `true` or `false` depending on if the `fd` is associated with a
|
|
|
|
terminal.
|
|
|
|
|
|
|
|
|
2012-02-27 20:09:35 +01:00
|
|
|
## tty.setRawMode(mode)
|
2010-11-26 04:09:28 +01:00
|
|
|
|
2012-03-29 22:15:24 +02:00
|
|
|
Deprecated. Use `tty.ReadStream#setRawMode()`
|
|
|
|
(i.e. `process.stdin.setRawMode()`) instead.
|
2012-03-27 00:21:25 +02: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-13 18:14:34 +02:00
|
|
|
node.js program (only when `isatty(0)` is true).
|
2012-03-27 00:21:25 +02: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-27 00:21:25 +02: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).
|
|
|
|
|
|
|
|
### ws.columns
|
|
|
|
|
|
|
|
A `Number` that gives the number of columns the TTY currently has. This property
|
|
|
|
gets updated on "resize" events.
|
|
|
|
|
|
|
|
### ws.rows
|
|
|
|
|
|
|
|
A `Number` that gives the number of rows the TTY currently has. This property
|
|
|
|
gets updated on "resize" events.
|
|
|
|
|
|
|
|
### Event: 'resize'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
|
|
|
Emitted by `refreshSize()` when either of the `columns` or `rows` properties
|
|
|
|
has changed.
|
|
|
|
|
|
|
|
process.stdout.on('resize', function() {
|
|
|
|
console.log('screen size has changed!');
|
|
|
|
console.log(process.stdout.columns + 'x' + process.stdout.rows);
|
|
|
|
});
|