Browse Source

doc: general improvements to tty.md

PR-URL: https://github.com/nodejs/node/pull/6931
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
v6.x
James M Snell 9 years ago
committed by Rod Vagg
parent
commit
3909209e7a
  1. 82
      doc/api/tty.md

82
doc/api/tty.md

@ -2,13 +2,20 @@
Stability: 2 - Stable Stability: 2 - Stable
The `tty` module houses the `tty.ReadStream` and `tty.WriteStream` classes. In The `tty` module provides the `tty.ReadStream` and `tty.WriteStream` classes.
most cases, you will not need to use this module directly. In most cases, it will not be necessary or possible to use this module directly.
However, it can be accessed using:
When Node.js detects that it is being run inside a TTY context, then `process.stdin` ```js
will be a `tty.ReadStream` instance and `process.stdout` will be const tty = require('tty');
a `tty.WriteStream` instance. The preferred way to check if Node.js is being run ```
in a TTY context is to check `process.stdout.isTTY`:
When Node.js detects that it is being run inside a text terminal ("TTY")
context, the `process.stdin` will, by default, be initialized as an instance of
`tty.ReadStream` and both `process.stdout` and `process.stderr` will, by
default be instances of `tty.WriteStream`. The preferred method of determining
whether Node.js is being run within a TTY context is to check that the value of
the `process.stdout.isTTY` property is `true`:
``` ```
$ node -p -e "Boolean(process.stdout.isTTY)" $ node -p -e "Boolean(process.stdout.isTTY)"
@ -17,50 +24,55 @@ $ node -p -e "Boolean(process.stdout.isTTY)" | cat
false false
``` ```
## Class: ReadStream In most cases, there should be little to no reason for an application to
create instances of the `tty.ReadStream` and `tty.WriteStream` classes.
## Class: tty.ReadStream
<!-- YAML <!-- YAML
added: v0.5.8 added: v0.5.8
--> -->
A `net.Socket` subclass that represents the readable portion of a tty. In normal The `tty.ReadStream` class is a subclass of `net.Socket` that represents the
circumstances, `process.stdin` will be the only `tty.ReadStream` instance in any readable side of a TTY. In normal circumstances `process.stdin` will be the
Node.js program (only when `isatty(0)` is true). only `tty.ReadStream` instance in a Node.js process and there should be no
reason to create additional instances.
### rs.isRaw ### readStream.isRaw
<!-- YAML <!-- YAML
added: v0.7.7 added: v0.7.7
--> -->
A `Boolean` that is initialized to `false`. It represents the current "raw" state A `boolean` that is `true` if the TTY is currently configured to operate as a
of the `tty.ReadStream` instance. raw device. Defaults to `false`.
### rs.setRawMode(mode) ### readStream.setRawMode(mode)
<!-- YAML <!-- YAML
added: v0.7.7 added: v0.7.7
--> -->
`mode` should be `true` or `false`. This sets the properties of the * `mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
`tty.ReadStream` to act either as a raw device or default. `isRaw` will be set raw device. If `false`, configures the `tty.ReadStream` to operate in its
to the resulting mode. default mode. The `readStream.isRaw` property will be set to the resulting
mode.
## Class: WriteStream ## Class: tty.WriteStream
<!-- YAML <!-- YAML
added: v0.5.8 added: v0.5.8
--> -->
A `net.Socket` subclass that represents the writable portion of a tty. In normal The `tty.WriteStream` class is a subclass of `net.Socket` that represents the
circumstances, `process.stdout` will be the only `tty.WriteStream` instance writable side of a TTY. In normal circumstances, `process.stdout` and
ever created (and only when `isatty(1)` is true). `process.stderr` will be the only `tty.WriteStream` instances created for a
Node.js process and there should be no reason to create additional instances.
### Event: 'resize' ### Event: 'resize'
<!-- YAML <!-- YAML
added: v0.7.7 added: v0.7.7
--> -->
`function () {}` The `'resize'` event is emitted whenever either of the `writeStream.columns`
or `writeStream.rows` properties have changed. No arguments are passed to the
Emitted by `refreshSize()` when either of the `columns` or `rows` properties listener callback when called.
has changed.
```js ```js
process.stdout.on('resize', () => { process.stdout.on('resize', () => {
@ -69,28 +81,30 @@ process.stdout.on('resize', () => {
}); });
``` ```
### ws.columns ### writeStream.columns
<!-- YAML <!-- YAML
added: v0.7.7 added: v0.7.7
--> -->
A `Number` that gives the number of columns the TTY currently has. This property A `number` specifying the number of columns the TTY currently has. This property
gets updated on `'resize'` events. is updated whenever the `'resize'` event is emitted.
### ws.rows ### writeStream.rows
<!-- YAML <!-- YAML
added: v0.7.7 added: v0.7.7
--> -->
A `Number` that gives the number of rows the TTY currently has. This property A `number` specifying the number of rows the TTY currently has. This property
gets updated on `'resize'` events. is updated whenever the `'resize'` event is emitted.
## tty.isatty(fd) ## tty.isatty(fd)
<!-- YAML <!-- YAML
added: v0.5.8 added: v0.5.8
--> -->
Returns `true` or `false` depending on if the `fd` is associated with a * `fd` {number} A numeric file descriptor
terminal.
The `tty.isatty()` method returns `true` if the given `fd` is associated with
a TTY and `false` if is not.
[tty.ReadStream#setRawMode]: #tty_rs_setrawmode_mode [tty.ReadStream#setRawMode]: #tty_readstream_setrawmode_mode

Loading…
Cancel
Save