当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js writeStream.hasColors()用法及代码示例


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

用法:

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

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

  • count:它是不同类型颜色的数量。
  • 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");

输出:

This object supports at least as many colors as provided in count:true

范例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);

使用以下命令运行index.js文件:

node index.js

输出:

This object supports at least as many colors as provided in count:true

参考:https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_hascolors_count_env

相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js writeStream.hasColors() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。