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
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js fs.fsyncSync()用法及代碼示例
- Node.js process.nextTick()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM blur()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawBezier()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js writeStream.hasColors() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。