1 # TTY 2 3 <!--introduced_in=v0.10.0--> 4 5 > Stability: 2 - Stable 6 7 <!-- source_link=lib/tty.js --> 8 9 The `tty` module provides the `tty.ReadStream` and `tty.WriteStream` classes. 10 In most cases, it will not be necessary or possible to use this module directly. 11 However, it can be accessed using: 12 13 ```js 14 const tty = require('tty'); 15 ``` 16 17 When Node.js detects that it is being run with a text terminal ("TTY") 18 attached, [`process.stdin`][] will, by default, be initialized as an instance of 19 `tty.ReadStream` and both [`process.stdout`][] and [`process.stderr`][] will, by 20 default, be instances of `tty.WriteStream`. The preferred method of determining 21 whether Node.js is being run within a TTY context is to check that the value of 22 the `process.stdout.isTTY` property is `true`: 23 24 ```console 25 $ node -p -e "Boolean(process.stdout.isTTY)" 26 true 27 $ node -p -e "Boolean(process.stdout.isTTY)" | cat 28 false 29 ``` 30 31 In most cases, there should be little to no reason for an application to 32 manually create instances of the `tty.ReadStream` and `tty.WriteStream` 33 classes. 34 35 ## Class: `tty.ReadStream` 36 <!-- YAML 37 added: v0.5.8 38 --> 39 40 * Extends: {net.Socket} 41 42 Represents the readable side of a TTY. In normal circumstances 43 [`process.stdin`][] will be the only `tty.ReadStream` instance in a Node.js 44 process and there should be no reason to create additional instances. 45 46 ### `readStream.isRaw` 47 <!-- YAML 48 added: v0.7.7 49 --> 50 51 A `boolean` that is `true` if the TTY is currently configured to operate as a 52 raw device. Defaults to `false`. 53 54 ### `readStream.isTTY` 55 <!-- YAML 56 added: v0.5.8 57 --> 58 59 A `boolean` that is always `true` for `tty.ReadStream` instances. 60 61 ### `readStream.setRawMode(mode)` 62 <!-- YAML 63 added: 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 72 Allows configuration of `tty.ReadStream` so that it operates as a raw device. 73 74 When in raw mode, input is always available character-by-character, not 75 including modifiers. Additionally, all special processing of characters by the 76 terminal 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 81 added: v0.5.8 82 --> 83 84 * Extends: {net.Socket} 85 86 Represents 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 89 should be no reason to create additional instances. 90 91 ### Event: `'resize'` 92 <!-- YAML 93 added: v0.7.7 94 --> 95 96 The `'resize'` event is emitted whenever either of the `writeStream.columns` 97 or `writeStream.rows` properties have changed. No arguments are passed to the 98 listener callback when called. 99 100 ```js 101 process.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 109 added: v0.7.7 110 changes: 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 126 direction identified by `dir`. 127 128 ### `writeStream.clearScreenDown([callback])` 129 <!-- YAML 130 added: v0.7.7 131 changes: 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 143 cursor down. 144 145 ### `writeStream.columns` 146 <!-- YAML 147 added: v0.7.7 148 --> 149 150 A `number` specifying the number of columns the TTY currently has. This property 151 is updated whenever the `'resize'` event is emitted. 152 153 ### `writeStream.cursorTo(x[, y][, callback])` 154 <!-- YAML 155 added: v0.7.7 156 changes: 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 170 position. 171 172 ### `writeStream.getColorDepth([env])` 173 <!-- YAML 174 added: 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 182 Returns: 183 184 * `1` for 2, 185 * `4` for 16, 186 * `8` for 256, 187 * `24` for 16,777,216 188 colors supported. 189 190 Use this to determine what colors the terminal supports. Due to the nature of 191 colors in terminals it is possible to either have false positives or false 192 negatives. It depends on process information and the environment variables that 193 may lie about what terminal is used. 194 It is possible to pass in an `env` object to simulate the usage of a specific 195 terminal. This can be useful to check how specific environment settings behave. 196 197 To 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 204 Disabling color support is also possible by using the `NO_COLOR` and 205 `NODE_DISABLE_COLORS` environment variables. 206 207 ### `writeStream.getWindowSize()` 208 <!-- YAML 209 added: v0.7.7 210 --> 211 212 * Returns: {number[]} 213 214 `writeStream.getWindowSize()` returns the size of the TTY 215 corresponding to this `WriteStream`. The array is of the type 216 `[numColumns, numRows]` where `numColumns` and `numRows` represent the number 217 of columns and rows in the corresponding TTY. 218 219 ### `writeStream.hasColors([count][, env])` 220 <!-- YAML 221 added: 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 233 Returns `true` if the `writeStream` supports at least as many colors as provided 234 in `count`. Minimum support is 2 (black and white). 235 236 This has the same false positives and negatives as described in 237 [`writeStream.getColorDepth()`][]. 238 239 ```js 240 process.stdout.hasColors(); 241 // Returns true or false depending on if `stdout` supports at least 16 colors. 242 process.stdout.hasColors(256); 243 // Returns true or false depending on if `stdout` supports at least 256 colors. 244 process.stdout.hasColors({ TMUX: '1' }); 245 // Returns true. 246 process.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 252 added: v0.5.8 253 --> 254 255 A `boolean` that is always `true`. 256 257 ### `writeStream.moveCursor(dx, dy[, callback])` 258 <!-- YAML 259 added: v0.7.7 260 changes: 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 274 current position. 275 276 ### `writeStream.rows` 277 <!-- YAML 278 added: v0.7.7 279 --> 280 281 A `number` specifying the number of rows the TTY currently has. This property 282 is updated whenever the `'resize'` event is emitted. 283 284 ## `tty.isatty(fd)` 285 <!-- YAML 286 added: v0.5.8 287 --> 288 289 * `fd` {number} A numeric file descriptor 290 * Returns: {boolean} 291 292 The `tty.isatty()` method returns `true` if the given `fd` is associated with 293 a TTY and `false` if it is not, including whenever `fd` is not a non-negative 294 integer. 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