Node.js writeStream.hasColors() Method
The writeStream.hasColors() method is an inbuilt application programming interface of class WriteStream within tty module which is used to check if this write stream object support at least as many colors as provided in count.
Syntax:
const writeStream.hasColors([count][, env])
Parameters: This method accepts the following parameter:
- count: It is the number of different types of colors.
- env: An object containing the environment variables to check.
Return Value: This method returns Boolean value true if and only if the writeStream supports at least as many colors as provided in count.
Example 1: Filename: index.js
var dgram = require( 'dgram' );
var client = dgram.createSocket( "udp4" );
var server = dgram.createSocket( "udp4" );
server.on( "message" , function (msg) {
let WriteStream = process.stdout;
const col = WriteStream.hasColors(16, 777, 216);
process.stdout.write(msg + col);
process.exit();
})
.bind(1234, () => {
});
client.send( "This object supports at least as"
+ " many colors as provided in count: " ,
0, 98, 1234, "localhost" );
|
Output:
This object supports at least as many colors as provided in count: true
Example 2: Filename: index.js
Javascript
let WriteStream = process.stdout;
const col = WriteStream.hasColors(256);
console.log( "This object supports at least as "
+ "many colors as provided in count: " , col);
|
Run the index.js file using the following command:
node index.js
Output:
This object supports at least as many colors as provided in count: true
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_hascolors_count_env
Last Updated :
25 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...