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
// Node.js program to demonstrate the // writeStream.hasColors() property // Importing dgram module var dgram = require( 'dgram' );
// Creating and initializing client // and server socket var client = dgram.createSocket( "udp4" );
var server = dgram.createSocket( "udp4" );
// Handling the message event server.on( "message" , function (msg) {
// Creating and initializing a WriteStream object
let WriteStream = process.stdout;
// Checking if the this object has the exactly
// same color or not requested by count
// by using hasColors() API
const col = WriteStream.hasColors(16, 777, 216);
// Displaying the result
process.stdout.write(msg + col);
// Exiting process
process.exit();
}) // Binding server with port .bind(1234, () => { }); // Client sending message to server 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
// Node.js program to demonstrate the // writeStream.hasColors() method // Creating and initializing a WriteStream object let WriteStream = process.stdout; // Checking if the this object has the exactly // same color or not requested by count // by using hasColors() method const col = WriteStream.hasColors(256); // Displaying the result 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