11cb0ef41Sopenharmony_ci# TTY
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci<!--introduced_in=v0.10.0-->
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci> Stability: 2 - Stable
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<!-- source_link=lib/tty.js -->
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciThe `node:tty` module provides the `tty.ReadStream` and `tty.WriteStream`
101cb0ef41Sopenharmony_ciclasses. In most cases, it will not be necessary or possible to use this module
111cb0ef41Sopenharmony_cidirectly. However, it can be accessed using:
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci```js
141cb0ef41Sopenharmony_ciconst tty = require('node:tty');
151cb0ef41Sopenharmony_ci```
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciWhen Node.js detects that it is being run with a text terminal ("TTY")
181cb0ef41Sopenharmony_ciattached, [`process.stdin`][] will, by default, be initialized as an instance of
191cb0ef41Sopenharmony_ci`tty.ReadStream` and both [`process.stdout`][] and [`process.stderr`][] will, by
201cb0ef41Sopenharmony_cidefault, be instances of `tty.WriteStream`. The preferred method of determining
211cb0ef41Sopenharmony_ciwhether Node.js is being run within a TTY context is to check that the value of
221cb0ef41Sopenharmony_cithe `process.stdout.isTTY` property is `true`:
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci```console
251cb0ef41Sopenharmony_ci$ node -p -e "Boolean(process.stdout.isTTY)"
261cb0ef41Sopenharmony_citrue
271cb0ef41Sopenharmony_ci$ node -p -e "Boolean(process.stdout.isTTY)" | cat
281cb0ef41Sopenharmony_cifalse
291cb0ef41Sopenharmony_ci```
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciIn most cases, there should be little to no reason for an application to
321cb0ef41Sopenharmony_cimanually create instances of the `tty.ReadStream` and `tty.WriteStream`
331cb0ef41Sopenharmony_ciclasses.
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci## Class: `tty.ReadStream`
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci<!-- YAML
381cb0ef41Sopenharmony_ciadded: v0.5.8
391cb0ef41Sopenharmony_ci-->
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci* Extends: {net.Socket}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciRepresents the readable side of a TTY. In normal circumstances
441cb0ef41Sopenharmony_ci[`process.stdin`][] will be the only `tty.ReadStream` instance in a Node.js
451cb0ef41Sopenharmony_ciprocess and there should be no reason to create additional instances.
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci### `readStream.isRaw`
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci<!-- YAML
501cb0ef41Sopenharmony_ciadded: v0.7.7
511cb0ef41Sopenharmony_ci-->
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ciA `boolean` that is `true` if the TTY is currently configured to operate as a
541cb0ef41Sopenharmony_ciraw device.
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciThis flag is always `false` when a process starts, even if the terminal is
571cb0ef41Sopenharmony_cioperating in raw mode. Its value will change with subsequent calls to
581cb0ef41Sopenharmony_ci`setRawMode`.
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci### `readStream.isTTY`
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci<!-- YAML
631cb0ef41Sopenharmony_ciadded: v0.5.8
641cb0ef41Sopenharmony_ci-->
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ciA `boolean` that is always `true` for `tty.ReadStream` instances.
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci### `readStream.setRawMode(mode)`
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci<!-- YAML
711cb0ef41Sopenharmony_ciadded: v0.7.7
721cb0ef41Sopenharmony_ci-->
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci* `mode` {boolean} If `true`, configures the `tty.ReadStream` to operate as a
751cb0ef41Sopenharmony_ci  raw device. If `false`, configures the `tty.ReadStream` to operate in its
761cb0ef41Sopenharmony_ci  default mode. The `readStream.isRaw` property will be set to the resulting
771cb0ef41Sopenharmony_ci  mode.
781cb0ef41Sopenharmony_ci* Returns: {this} The read stream instance.
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciAllows configuration of `tty.ReadStream` so that it operates as a raw device.
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciWhen in raw mode, input is always available character-by-character, not
831cb0ef41Sopenharmony_ciincluding modifiers. Additionally, all special processing of characters by the
841cb0ef41Sopenharmony_citerminal is disabled, including echoing input
851cb0ef41Sopenharmony_cicharacters. <kbd>Ctrl</kbd>+<kbd>C</kbd> will no longer cause a `SIGINT` when
861cb0ef41Sopenharmony_ciin this mode.
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci## Class: `tty.WriteStream`
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci<!-- YAML
911cb0ef41Sopenharmony_ciadded: v0.5.8
921cb0ef41Sopenharmony_ci-->
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci* Extends: {net.Socket}
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ciRepresents the writable side of a TTY. In normal circumstances,
971cb0ef41Sopenharmony_ci[`process.stdout`][] and [`process.stderr`][] will be the only
981cb0ef41Sopenharmony_ci`tty.WriteStream` instances created for a Node.js process and there
991cb0ef41Sopenharmony_cishould be no reason to create additional instances.
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci### Event: `'resize'`
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci<!-- YAML
1041cb0ef41Sopenharmony_ciadded: v0.7.7
1051cb0ef41Sopenharmony_ci-->
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciThe `'resize'` event is emitted whenever either of the `writeStream.columns`
1081cb0ef41Sopenharmony_cior `writeStream.rows` properties have changed. No arguments are passed to the
1091cb0ef41Sopenharmony_cilistener callback when called.
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci```js
1121cb0ef41Sopenharmony_ciprocess.stdout.on('resize', () => {
1131cb0ef41Sopenharmony_ci  console.log('screen size has changed!');
1141cb0ef41Sopenharmony_ci  console.log(`${process.stdout.columns}x${process.stdout.rows}`);
1151cb0ef41Sopenharmony_ci});
1161cb0ef41Sopenharmony_ci```
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci### `writeStream.clearLine(dir[, callback])`
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci<!-- YAML
1211cb0ef41Sopenharmony_ciadded: v0.7.7
1221cb0ef41Sopenharmony_cichanges:
1231cb0ef41Sopenharmony_ci  - version: v12.7.0
1241cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/28721
1251cb0ef41Sopenharmony_ci    description: The stream's write() callback and return value are exposed.
1261cb0ef41Sopenharmony_ci-->
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci* `dir` {number}
1291cb0ef41Sopenharmony_ci  * `-1`: to the left from cursor
1301cb0ef41Sopenharmony_ci  * `1`: to the right from cursor
1311cb0ef41Sopenharmony_ci  * `0`: the entire line
1321cb0ef41Sopenharmony_ci* `callback` {Function} Invoked once the operation completes.
1331cb0ef41Sopenharmony_ci* Returns: {boolean} `false` if the stream wishes for the calling code to wait
1341cb0ef41Sopenharmony_ci  for the `'drain'` event to be emitted before continuing to write additional
1351cb0ef41Sopenharmony_ci  data; otherwise `true`.
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci`writeStream.clearLine()` clears the current line of this `WriteStream` in a
1381cb0ef41Sopenharmony_cidirection identified by `dir`.
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci### `writeStream.clearScreenDown([callback])`
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci<!-- YAML
1431cb0ef41Sopenharmony_ciadded: v0.7.7
1441cb0ef41Sopenharmony_cichanges:
1451cb0ef41Sopenharmony_ci  - version: v12.7.0
1461cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/28721
1471cb0ef41Sopenharmony_ci    description: The stream's write() callback and return value are exposed.
1481cb0ef41Sopenharmony_ci-->
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci* `callback` {Function} Invoked once the operation completes.
1511cb0ef41Sopenharmony_ci* Returns: {boolean} `false` if the stream wishes for the calling code to wait
1521cb0ef41Sopenharmony_ci  for the `'drain'` event to be emitted before continuing to write additional
1531cb0ef41Sopenharmony_ci  data; otherwise `true`.
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci`writeStream.clearScreenDown()` clears this `WriteStream` from the current
1561cb0ef41Sopenharmony_cicursor down.
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci### `writeStream.columns`
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ci<!-- YAML
1611cb0ef41Sopenharmony_ciadded: v0.7.7
1621cb0ef41Sopenharmony_ci-->
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ciA `number` specifying the number of columns the TTY currently has. This property
1651cb0ef41Sopenharmony_ciis updated whenever the `'resize'` event is emitted.
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci### `writeStream.cursorTo(x[, y][, callback])`
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci<!-- YAML
1701cb0ef41Sopenharmony_ciadded: v0.7.7
1711cb0ef41Sopenharmony_cichanges:
1721cb0ef41Sopenharmony_ci  - version: v12.7.0
1731cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/28721
1741cb0ef41Sopenharmony_ci    description: The stream's write() callback and return value are exposed.
1751cb0ef41Sopenharmony_ci-->
1761cb0ef41Sopenharmony_ci
1771cb0ef41Sopenharmony_ci* `x` {number}
1781cb0ef41Sopenharmony_ci* `y` {number}
1791cb0ef41Sopenharmony_ci* `callback` {Function} Invoked once the operation completes.
1801cb0ef41Sopenharmony_ci* Returns: {boolean} `false` if the stream wishes for the calling code to wait
1811cb0ef41Sopenharmony_ci  for the `'drain'` event to be emitted before continuing to write additional
1821cb0ef41Sopenharmony_ci  data; otherwise `true`.
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci`writeStream.cursorTo()` moves this `WriteStream`'s cursor to the specified
1851cb0ef41Sopenharmony_ciposition.
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci### `writeStream.getColorDepth([env])`
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci<!-- YAML
1901cb0ef41Sopenharmony_ciadded: v9.9.0
1911cb0ef41Sopenharmony_ci-->
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci* `env` {Object} An object containing the environment variables to check. This
1941cb0ef41Sopenharmony_ci  enables simulating the usage of a specific terminal. **Default:**
1951cb0ef41Sopenharmony_ci  `process.env`.
1961cb0ef41Sopenharmony_ci* Returns: {number}
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ciReturns:
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci* `1` for 2,
2011cb0ef41Sopenharmony_ci* `4` for 16,
2021cb0ef41Sopenharmony_ci* `8` for 256,
2031cb0ef41Sopenharmony_ci* `24` for 16,777,216 colors supported.
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ciUse this to determine what colors the terminal supports. Due to the nature of
2061cb0ef41Sopenharmony_cicolors in terminals it is possible to either have false positives or false
2071cb0ef41Sopenharmony_cinegatives. It depends on process information and the environment variables that
2081cb0ef41Sopenharmony_cimay lie about what terminal is used.
2091cb0ef41Sopenharmony_ciIt is possible to pass in an `env` object to simulate the usage of a specific
2101cb0ef41Sopenharmony_citerminal. This can be useful to check how specific environment settings behave.
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ciTo enforce a specific color support, use one of the below environment settings.
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci* 2 colors: `FORCE_COLOR = 0` (Disables colors)
2151cb0ef41Sopenharmony_ci* 16 colors: `FORCE_COLOR = 1`
2161cb0ef41Sopenharmony_ci* 256 colors: `FORCE_COLOR = 2`
2171cb0ef41Sopenharmony_ci* 16,777,216 colors: `FORCE_COLOR = 3`
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ciDisabling color support is also possible by using the `NO_COLOR` and
2201cb0ef41Sopenharmony_ci`NODE_DISABLE_COLORS` environment variables.
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci### `writeStream.getWindowSize()`
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci<!-- YAML
2251cb0ef41Sopenharmony_ciadded: v0.7.7
2261cb0ef41Sopenharmony_ci-->
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci* Returns: {number\[]}
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci`writeStream.getWindowSize()` returns the size of the TTY
2311cb0ef41Sopenharmony_cicorresponding to this `WriteStream`. The array is of the type
2321cb0ef41Sopenharmony_ci`[numColumns, numRows]` where `numColumns` and `numRows` represent the number
2331cb0ef41Sopenharmony_ciof columns and rows in the corresponding TTY.
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci### `writeStream.hasColors([count][, env])`
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci<!-- YAML
2381cb0ef41Sopenharmony_ciadded:
2391cb0ef41Sopenharmony_ci - v11.13.0
2401cb0ef41Sopenharmony_ci - v10.16.0
2411cb0ef41Sopenharmony_ci-->
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci* `count` {integer} The number of colors that are requested (minimum 2).
2441cb0ef41Sopenharmony_ci  **Default:** 16.
2451cb0ef41Sopenharmony_ci* `env` {Object} An object containing the environment variables to check. This
2461cb0ef41Sopenharmony_ci  enables simulating the usage of a specific terminal. **Default:**
2471cb0ef41Sopenharmony_ci  `process.env`.
2481cb0ef41Sopenharmony_ci* Returns: {boolean}
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ciReturns `true` if the `writeStream` supports at least as many colors as provided
2511cb0ef41Sopenharmony_ciin `count`. Minimum support is 2 (black and white).
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ciThis has the same false positives and negatives as described in
2541cb0ef41Sopenharmony_ci[`writeStream.getColorDepth()`][].
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci```js
2571cb0ef41Sopenharmony_ciprocess.stdout.hasColors();
2581cb0ef41Sopenharmony_ci// Returns true or false depending on if `stdout` supports at least 16 colors.
2591cb0ef41Sopenharmony_ciprocess.stdout.hasColors(256);
2601cb0ef41Sopenharmony_ci// Returns true or false depending on if `stdout` supports at least 256 colors.
2611cb0ef41Sopenharmony_ciprocess.stdout.hasColors({ TMUX: '1' });
2621cb0ef41Sopenharmony_ci// Returns true.
2631cb0ef41Sopenharmony_ciprocess.stdout.hasColors(2 ** 24, { TMUX: '1' });
2641cb0ef41Sopenharmony_ci// Returns false (the environment setting pretends to support 2 ** 8 colors).
2651cb0ef41Sopenharmony_ci```
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci### `writeStream.isTTY`
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ci<!-- YAML
2701cb0ef41Sopenharmony_ciadded: v0.5.8
2711cb0ef41Sopenharmony_ci-->
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_ciA `boolean` that is always `true`.
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci### `writeStream.moveCursor(dx, dy[, callback])`
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_ci<!-- YAML
2781cb0ef41Sopenharmony_ciadded: v0.7.7
2791cb0ef41Sopenharmony_cichanges:
2801cb0ef41Sopenharmony_ci  - version: v12.7.0
2811cb0ef41Sopenharmony_ci    pr-url: https://github.com/nodejs/node/pull/28721
2821cb0ef41Sopenharmony_ci    description: The stream's write() callback and return value are exposed.
2831cb0ef41Sopenharmony_ci-->
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ci* `dx` {number}
2861cb0ef41Sopenharmony_ci* `dy` {number}
2871cb0ef41Sopenharmony_ci* `callback` {Function} Invoked once the operation completes.
2881cb0ef41Sopenharmony_ci* Returns: {boolean} `false` if the stream wishes for the calling code to wait
2891cb0ef41Sopenharmony_ci  for the `'drain'` event to be emitted before continuing to write additional
2901cb0ef41Sopenharmony_ci  data; otherwise `true`.
2911cb0ef41Sopenharmony_ci
2921cb0ef41Sopenharmony_ci`writeStream.moveCursor()` moves this `WriteStream`'s cursor _relative_ to its
2931cb0ef41Sopenharmony_cicurrent position.
2941cb0ef41Sopenharmony_ci
2951cb0ef41Sopenharmony_ci### `writeStream.rows`
2961cb0ef41Sopenharmony_ci
2971cb0ef41Sopenharmony_ci<!-- YAML
2981cb0ef41Sopenharmony_ciadded: v0.7.7
2991cb0ef41Sopenharmony_ci-->
3001cb0ef41Sopenharmony_ci
3011cb0ef41Sopenharmony_ciA `number` specifying the number of rows the TTY currently has. This property
3021cb0ef41Sopenharmony_ciis updated whenever the `'resize'` event is emitted.
3031cb0ef41Sopenharmony_ci
3041cb0ef41Sopenharmony_ci## `tty.isatty(fd)`
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ci<!-- YAML
3071cb0ef41Sopenharmony_ciadded: v0.5.8
3081cb0ef41Sopenharmony_ci-->
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_ci* `fd` {number} A numeric file descriptor
3111cb0ef41Sopenharmony_ci* Returns: {boolean}
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_ciThe `tty.isatty()` method returns `true` if the given `fd` is associated with
3141cb0ef41Sopenharmony_cia TTY and `false` if it is not, including whenever `fd` is not a non-negative
3151cb0ef41Sopenharmony_ciinteger.
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci[`process.stderr`]: process.md#processstderr
3181cb0ef41Sopenharmony_ci[`process.stdin`]: process.md#processstdin
3191cb0ef41Sopenharmony_ci[`process.stdout`]: process.md#processstdout
3201cb0ef41Sopenharmony_ci[`writeStream.getColorDepth()`]: #writestreamgetcolordepthenv
321