📜  Node.js writeStream.hasColors() 方法

📅  最后修改于: 2022-05-13 01:56:37.745000             🧑  作者: Mango

Node.js writeStream.hasColors() 方法

writeStream.hasColors()方法是 tty 模块中 WriteStream 类的内置应用程序编程接口,用于检查此写入流对象是否支持至少与 count 中提供的颜色一样多的颜色。

句法:

const writeStream.hasColors([count][, env])

参数:此方法接受以下参数:

  • 计数:它是不同类型颜色的数量。
  • env:包含要检查的环境变量的对象。

返回值:当且仅当 writeStream 至少支持与 count 中提供的一样多的颜色时,此方法才返回布尔值 true。

示例 1:文件名: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");

输出:

示例 2:文件名:index.js

Javascript
// 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);