2
2
3
3
Stability: 2 - Stable
4
4
5
- The ` tty ` module houses the ` tty.ReadStream ` and ` tty.WriteStream ` classes. In
6
- most cases, you will not need to use this module directly.
5
+ The ` tty ` module provides the ` tty.ReadStream ` and ` tty.WriteStream ` classes.
6
+ In most cases, it will not be necessary or possible to use this module directly.
7
+ However, it can be accessed using:
7
8
8
- When Node.js detects that it is being run inside a TTY context, then ` process.stdin `
9
- will be a ` tty.ReadStream ` instance and ` process.stdout ` will be
10
- a ` tty.WriteStream ` instance. The preferred way to check if Node.js is being run
11
- in a TTY context is to check ` process.stdout.isTTY ` :
9
+ ``` js
10
+ const tty = require (' tty' );
11
+ ```
12
+
13
+ When Node.js detects that it is being run inside a text terminal ("TTY")
14
+ context, the ` process.stdin ` will, by default, be initialized as an instance of
15
+ ` tty.ReadStream ` and both ` process.stdout ` and ` process.stderr ` will, by
16
+ default be instances of ` tty.WriteStream ` . The preferred method of determining
17
+ whether Node.js is being run within a TTY context is to check that the value of
18
+ the ` process.stdout.isTTY ` property is ` true ` :
12
19
13
20
```
14
21
$ node -p -e "Boolean(process.stdout.isTTY)"
@@ -17,50 +24,55 @@ $ node -p -e "Boolean(process.stdout.isTTY)" | cat
17
24
false
18
25
```
19
26
20
- ## Class: ReadStream
27
+ In most cases, there should be little to no reason for an application to
28
+ create instances of the ` tty.ReadStream ` and ` tty.WriteStream ` classes.
29
+
30
+ ## Class: tty.ReadStream
21
31
<!-- YAML
22
32
added: v0.5.8
23
33
-->
24
34
25
- A ` net.Socket ` subclass that represents the readable portion of a tty. In normal
26
- circumstances, ` process.stdin ` will be the only ` tty.ReadStream ` instance in any
27
- Node.js program (only when ` isatty(0) ` is true).
35
+ The ` tty.ReadStream ` class is a subclass of ` net.Socket ` that represents the
36
+ readable side of a TTY. In normal circumstances ` process.stdin ` will be the
37
+ only ` tty.ReadStream ` instance in a Node.js process and there should be no
38
+ reason to create additional instances.
28
39
29
- ### rs .isRaw
40
+ ### readStream .isRaw
30
41
<!-- YAML
31
42
added: v0.7.7
32
43
-->
33
44
34
- A ` Boolean ` that is initialized to ` false ` . It represents the current "raw" state
35
- of the ` tty.ReadStream ` instance .
45
+ A ` boolean ` that is ` true ` if the TTY is currently configured to operate as a
46
+ raw device. Defaults to ` false ` .
36
47
37
- ### rs .setRawMode(mode)
48
+ ### readStream .setRawMode(mode)
38
49
<!-- YAML
39
50
added: v0.7.7
40
51
-->
41
52
42
- ` mode ` should be ` true ` or ` false ` . This sets the properties of the
43
- ` tty.ReadStream ` to act either as a raw device or default. ` isRaw ` will be set
44
- to the resulting mode.
53
+ * ` mode ` {boolean} If ` true ` , configures the ` tty.ReadStream ` to operate as a
54
+ raw device. If ` false ` , configures the ` tty.ReadStream ` to operate in its
55
+ default mode. The ` readStream.isRaw ` property will be set to the resulting
56
+ mode.
45
57
46
- ## Class: WriteStream
58
+ ## Class: tty. WriteStream
47
59
<!-- YAML
48
60
added: v0.5.8
49
61
-->
50
62
51
- A ` net.Socket ` subclass that represents the writable portion of a tty. In normal
52
- circumstances, ` process.stdout ` will be the only ` tty.WriteStream ` instance
53
- ever created (and only when ` isatty(1) ` is true).
63
+ The ` tty.WriteStream ` class is a subclass of ` net.Socket ` that represents the
64
+ writable side of a TTY. In normal circumstances, ` process.stdout ` and
65
+ ` process.stderr ` will be the only ` tty.WriteStream ` instances created for a
66
+ Node.js process and there should be no reason to create additional instances.
54
67
55
68
### Event: 'resize'
56
69
<!-- YAML
57
70
added: v0.7.7
58
71
-->
59
72
60
- ` function () {} `
61
-
62
- Emitted by ` refreshSize() ` when either of the ` columns ` or ` rows ` properties
63
- has changed.
73
+ The ` 'resize' ` event is emitted whenever either of the ` writeStream.columns `
74
+ or ` writeStream.rows ` properties have changed. No arguments are passed to the
75
+ listener callback when called.
64
76
65
77
``` js
66
78
process .stdout .on (' resize' , () => {
@@ -69,28 +81,30 @@ process.stdout.on('resize', () => {
69
81
});
70
82
```
71
83
72
- ### ws .columns
84
+ ### writeStream .columns
73
85
<!-- YAML
74
86
added: v0.7.7
75
87
-->
76
88
77
- A ` Number ` that gives the number of columns the TTY currently has. This property
78
- gets updated on ` 'resize' ` events .
89
+ A ` number ` specifying the number of columns the TTY currently has. This property
90
+ is updated whenever the ` 'resize' ` event is emitted .
79
91
80
- ### ws .rows
92
+ ### writeStream .rows
81
93
<!-- YAML
82
94
added: v0.7.7
83
95
-->
84
96
85
- A ` Number ` that gives the number of rows the TTY currently has. This property
86
- gets updated on ` 'resize' ` events .
97
+ A ` number ` specifying the number of rows the TTY currently has. This property
98
+ is updated whenever the ` 'resize' ` event is emitted .
87
99
88
100
## tty.isatty(fd)
89
101
<!-- YAML
90
102
added: v0.5.8
91
103
-->
92
104
93
- Returns ` true ` or ` false ` depending on if the ` fd ` is associated with a
94
- terminal.
105
+ * ` fd ` {number} A numeric file descriptor
106
+
107
+ The ` tty.isatty() ` method returns ` true ` if the given ` fd ` is associated with
108
+ a TTY and ` false ` if is not.
95
109
96
- [ tty.ReadStream#setRawMode ] : #tty_rs_setrawmode_mode
110
+ [ tty.ReadStream#setRawMode ] : #tty_readstream_setrawmode_mode
0 commit comments