Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
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 Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
 


  

 
Node.js TTY
Question Posted on 21 Apr 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js TTY


Node.js TTY

Node.js TTY


When we say about the Node.js TTY, this module contains TTY.ReadStream and TTY.WriteStream classes. And when we say about the most cases, there is no need to use this module directly.

And 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:

  • Well process.stdin will be a tty.ReadStream instance

  • Well process.stdout will be a tty.WriteStream instance



And to check that if Node.js is running in a TTY context, we use the following command:

node -p -e "Boolean(process.stdout.isTTY)"



Node.js TTY

Class: ReadStream


This will contains a net.Socket subclass which represents the readable portion of a tty. And when we say about the normal circumstances, tt.ReadStream has the only instance named process.stdin in any Node.js program (only when isatty(0) is true).

rs.isRawThis is a Boolean which is initialized to false. This will specifies the current "raw" state of the tty.ReadStream instance.

rs.setRawMode(mode):This is true or false. And 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


This will contains a net.Socket subclass which will represents the writable portion of a tty. And in normal circumstances, tty.WriteStream has the only instance named process.stdout in any of 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:This is used to give the number of columns TTY currently have. ANd this property gets updated on 'resize' events.

ws.rows:This is used to give the number of rows the TTY currently have. And 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('Test for TTY:- How are you');
process.stdin.on('keypress', function(char, key) {
if (key && key.ctrl && key.name == 'c') {

process.exit()
}
});


Output:


Node.js TTY Sample





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |