Node.js Tutorial
- Node.js Tutorial
- Install Node.js on Windows
- Install Node.js on Linux/Ubuntu/CentOS
- Node.js First Example
- Node.js Console
- Node.js REPL
- Node.js Package Manager
- Node.js Command Line Options
- Node.js Global Objects
- Node.js OS
- Node.js Timer
- Node.js Errors
- Node.js DNS
- Node.js Net
- Node.js Crypto
- Node.js TLS/SSL
- Node.js Debugger
- Node.js Process
- Node.js Child Process
- Node.js Buffers
- Node.js Streams
- Node.js File System (FS)
- Node.js Path
- Node.js StringDecoder
- Node.js Query String
- Node.js ZLIB
- Node.js Assertion Testing
- Node.js V8
- Node.js Callbacks
- Node.js Events
- Node.js Punycode
- Node.js TTY
- Node.js Web Module
- NestJS
Node.js MySQL
Node.js MongoDB
Nodejs Difference
Node.js MCQ
Node.js Express
Nodejs Interview Questions
Node.js TTY
The Node.js TTY module contains tty.ReadStream and tty.WriteStream classes. In most cases, there is no need to use this module directly.
You have to use require ('tty') to access this module.
Syntax:
- var tty = require('tty');
When Node.js discovers that it is being run inside a TTY context, then:
- process.stdin will be a tty.ReadStream instance
- process.stdout will be a tty.WriteStream instance
To check that if Node.js is running in a TTY context, use the following command:
- node -p -e "Boolean(process.stdout.isTTY)"
Class: ReadStream
It contains a net.Socket subclass that represents the readable portion of a tty. In normal circumstances,the tty.ReadStream has the only instance named process.stdin in any Node.js program (only when isatty(0) is true).
rs.isRaw: It is a Boolean that is initialized to false. It specifies the current "raw" state of the tty.ReadStream instance.
rs.setRawMode(mode): It should be true or false. It is used to set the properties of the tty.ReadStream to act either as a raw device or default. isRaw will be set to the resulting mode.
Class: WriteStream
It contains a net.Socket subclass that represents the writable portion of a tty. In normal circumstances, the tty.WriteStream has the only instance named process.stdout in any Node.js program (only when isatty(1) is true).
Resize event: This event is used when either of the columns or rows properties has changed.
Syntax:
- process.stdout.on('resize', () => {
- console.log('screen size has changed!');
- console.log(`${process.stdout.columns}x${process.stdout.rows}`);
- });
ws.columns: It is used to give the number of columns the TTY currently has. This property gets updated on 'resize' events.
ws.rows: It is used to give the number of rows the TTY currently has. This property gets updated on 'resize' events.
Node.js TTY Example
File: tty.js
- var tty = require('tty');
- process.stdin.setRawMode(true);
- process.stdin.resume();
- console.log('I am leaving now');
- process.stdin.on('keypress', function(char, key) {
- if (key && key.ctrl && key.name == 'c') {
- process.exit()
- }
- });
Output: