• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# TTY
2
3<!--introduced_in=v0.10.0-->
4
5> Stability: 2 - Stable
6
7<!-- source_link=lib/tty.js -->
8
9The `tty` module provides the `tty.ReadStream` and `tty.WriteStream` classes.
10In most cases, it will not be necessary or possible to use this module directly.
11However, it can be accessed using:
12
13```js
14const tty = require('tty');
15```
16
17When Node.js detects that it is being run with a text terminal ("TTY")
18attached, [`process.stdin`][] will, by default, be initialized as an instance of
19`tty.ReadStream` and both [`process.stdout`][] and [`process.stderr`][] will, by
20default, be instances of `tty.WriteStream`. The preferred method of determining
21whether Node.js is being run within a TTY context is to check that the value of
22the `process.stdout.isTTY` property is `true`:
23
24```console
25$ node -p -e "Boolean(process.stdout.isTTY)"
26true
27$ node -p -e "Boolean(process.stdout.isTTY)" | cat
28false
29```
30
31In most cases, there should be little to no reason for an application to
32manually create instances of the `tty.ReadStream` and `tty.WriteStream`
33classes.
34
35## Class: `tty.ReadStream`
36<!-- YAML
37added: v0.5.8
38-->
39
40* Extends: {net.Socket}
41
42Represents the readable side of a TTY. In normal circumstances
43[`process.stdin`][] will be the only `tty.ReadStream` instance in a Node.js
44process and there should be no reason to create additional instances.
45
46### `readStream.isRaw`
47<!-- YAML
48added: v0.7.7
49-->
50
51A `boolean` that is `true` if the TTY is currently configured to operate as a
52raw device. Defaults to `false`.
53
54### `readStream.isTTY`
55<!-- YAML
56added: v0.5.8
57-->
58
59A `boolean` that is always `true` for `tty.ReadStream` instances.
60
61### `readStream.setRawMode(mode)`
62<!-- YAML
63added: v0.7.7
64-->
65
66* `mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
67  raw device. If `false`, configures the `tty.ReadStream` to operate in its
68  default mode. The `readStream.isRaw` property will be set to the resulting
69  mode.
70* Returns: {this} The read stream instance.
71
72Allows configuration of `tty.ReadStream` so that it operates as a raw device.
73
74When in raw mode, input is always available character-by-character, not
75including modifiers. Additionally, all special processing of characters by the
76terminal is disabled, including echoing input characters.
77<kbd>Ctrl</kbd>+<kbd>C</kbd> will no longer cause a `SIGINT` when in this mode.
78
79## Class: `tty.WriteStream`
80<!-- YAML
81added: v0.5.8
82-->
83
84* Extends: {net.Socket}
85
86Represents the writable side of a TTY. In normal circumstances,
87[`process.stdout`][] and [`process.stderr`][] will be the only
88`tty.WriteStream` instances created for a Node.js process and there
89should be no reason to create additional instances.
90
91### Event: `'resize'`
92<!-- YAML
93added: v0.7.7
94-->
95
96The `'resize'` event is emitted whenever either of the `writeStream.columns`
97or `writeStream.rows` properties have changed. No arguments are passed to the
98listener callback when called.
99
100```js
101process.stdout.on('resize', () => {
102  console.log('screen size has changed!');
103  console.log(`${process.stdout.columns}x${process.stdout.rows}`);
104});
105```
106
107### `writeStream.clearLine(dir[, callback])`
108<!-- YAML
109added: v0.7.7
110changes:
111  - version: v12.7.0
112    pr-url: https://github.com/nodejs/node/pull/28721
113    description: The stream's write() callback and return value are exposed.
114-->
115
116* `dir` {number}
117  * `-1`: to the left from cursor
118  * `1`: to the right from cursor
119  * `0`: the entire line
120* `callback` {Function} Invoked once the operation completes.
121* Returns: {boolean} `false` if the stream wishes for the calling code to wait
122  for the `'drain'` event to be emitted before continuing to write additional
123  data; otherwise `true`.
124
125`writeStream.clearLine()` clears the current line of this `WriteStream` in a
126direction identified by `dir`.
127
128### `writeStream.clearScreenDown([callback])`
129<!-- YAML
130added: v0.7.7
131changes:
132  - version: v12.7.0
133    pr-url: https://github.com/nodejs/node/pull/28721
134    description: The stream's write() callback and return value are exposed.
135-->
136
137* `callback` {Function} Invoked once the operation completes.
138* Returns: {boolean} `false` if the stream wishes for the calling code to wait
139  for the `'drain'` event to be emitted before continuing to write additional
140  data; otherwise `true`.
141
142`writeStream.clearScreenDown()` clears this `WriteStream` from the current
143cursor down.
144
145### `writeStream.columns`
146<!-- YAML
147added: v0.7.7
148-->
149
150A `number` specifying the number of columns the TTY currently has. This property
151is updated whenever the `'resize'` event is emitted.
152
153### `writeStream.cursorTo(x[, y][, callback])`
154<!-- YAML
155added: v0.7.7
156changes:
157  - version: v12.7.0
158    pr-url: https://github.com/nodejs/node/pull/28721
159    description: The stream's write() callback and return value are exposed.
160-->
161
162* `x` {number}
163* `y` {number}
164* `callback` {Function} Invoked once the operation completes.
165* Returns: {boolean} `false` if the stream wishes for the calling code to wait
166  for the `'drain'` event to be emitted before continuing to write additional
167  data; otherwise `true`.
168
169`writeStream.cursorTo()` moves this `WriteStream`'s cursor to the specified
170position.
171
172### `writeStream.getColorDepth([env])`
173<!-- YAML
174added: v9.9.0
175-->
176
177* `env` {Object} An object containing the environment variables to check. This
178  enables simulating the usage of a specific terminal. **Default:**
179  `process.env`.
180* Returns: {number}
181
182Returns:
183
184* `1` for 2,
185* `4` for 16,
186* `8` for 256,
187* `24` for 16,777,216
188colors supported.
189
190Use this to determine what colors the terminal supports. Due to the nature of
191colors in terminals it is possible to either have false positives or false
192negatives. It depends on process information and the environment variables that
193may lie about what terminal is used.
194It is possible to pass in an `env` object to simulate the usage of a specific
195terminal. This can be useful to check how specific environment settings behave.
196
197To enforce a specific color support, use one of the below environment settings.
198
199* 2 colors: `FORCE_COLOR = 0` (Disables colors)
200* 16 colors: `FORCE_COLOR = 1`
201* 256 colors: `FORCE_COLOR = 2`
202* 16,777,216 colors: `FORCE_COLOR = 3`
203
204Disabling color support is also possible by using the `NO_COLOR` and
205`NODE_DISABLE_COLORS` environment variables.
206
207### `writeStream.getWindowSize()`
208<!-- YAML
209added: v0.7.7
210-->
211
212* Returns: {number[]}
213
214`writeStream.getWindowSize()` returns the size of the TTY
215corresponding to this `WriteStream`. The array is of the type
216`[numColumns, numRows]` where `numColumns` and `numRows` represent the number
217of columns and rows in the corresponding TTY.
218
219### `writeStream.hasColors([count][, env])`
220<!-- YAML
221added:
222 - v11.13.0
223 - v10.16.0
224-->
225
226* `count` {integer} The number of colors that are requested (minimum 2).
227  **Default:** 16.
228* `env` {Object} An object containing the environment variables to check. This
229  enables simulating the usage of a specific terminal. **Default:**
230  `process.env`.
231* Returns: {boolean}
232
233Returns `true` if the `writeStream` supports at least as many colors as provided
234in `count`. Minimum support is 2 (black and white).
235
236This has the same false positives and negatives as described in
237[`writeStream.getColorDepth()`][].
238
239```js
240process.stdout.hasColors();
241// Returns true or false depending on if `stdout` supports at least 16 colors.
242process.stdout.hasColors(256);
243// Returns true or false depending on if `stdout` supports at least 256 colors.
244process.stdout.hasColors({ TMUX: '1' });
245// Returns true.
246process.stdout.hasColors(2 ** 24, { TMUX: '1' });
247// Returns false (the environment setting pretends to support 2 ** 8 colors).
248```
249
250### `writeStream.isTTY`
251<!-- YAML
252added: v0.5.8
253-->
254
255A `boolean` that is always `true`.
256
257### `writeStream.moveCursor(dx, dy[, callback])`
258<!-- YAML
259added: v0.7.7
260changes:
261  - version: v12.7.0
262    pr-url: https://github.com/nodejs/node/pull/28721
263    description: The stream's write() callback and return value are exposed.
264-->
265
266* `dx` {number}
267* `dy` {number}
268* `callback` {Function} Invoked once the operation completes.
269* Returns: {boolean} `false` if the stream wishes for the calling code to wait
270  for the `'drain'` event to be emitted before continuing to write additional
271  data; otherwise `true`.
272
273`writeStream.moveCursor()` moves this `WriteStream`'s cursor *relative* to its
274current position.
275
276### `writeStream.rows`
277<!-- YAML
278added: v0.7.7
279-->
280
281A `number` specifying the number of rows the TTY currently has. This property
282is updated whenever the `'resize'` event is emitted.
283
284## `tty.isatty(fd)`
285<!-- YAML
286added: v0.5.8
287-->
288
289* `fd` {number} A numeric file descriptor
290* Returns: {boolean}
291
292The `tty.isatty()` method returns `true` if the given `fd` is associated with
293a TTY and `false` if it is not, including whenever `fd` is not a non-negative
294integer.
295
296[`process.stderr`]: process.md#process_process_stderr
297[`process.stdin`]: process.md#process_process_stdin
298[`process.stdout`]: process.md#process_process_stdout
299[`writeStream.getColorDepth()`]: #tty_writestream_getcolordepth_env
300