📜  Node.js writeStream.isTTY 属性

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

Node.js writeStream.isTTY 属性

writeStream.isTTY属性是 'tty' 模块中 WriteStream 类的内置应用程序编程接口,用于检查写入 Stream 对象是否为 tty 实例。对于 tty、writeStream,它将始终返回 true。

句法:

const writeStream.isTTY

返回值:如果写入 Stream 对象是 tty 的实例,则此属性返回 true。

示例 1:文件名:index.js

Javascript
// Node.js program to demonstrate the
// writeStream.isTTY 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");
 
// Catching the message event
server.on("message", function (msg) {
 
    // Creating and initializing a
    // WriteStream object
    let WriteStream = process.stdout;
 
    // Checking if it is instance of TTY
    // or not by using isTTY() method
    const status = WriteStream.isTTY;
 
    // Displaying the result
    if (status)
        process.stdout.write(msg
            + "an instant of TTY" + "\n");
    else
        process.stdout.write(msg
            + "not an instant of TTY" + "\n");
 
    // Exiting process
    process.exit();
})
 
// Binding server with port
.bind(1234, () => {
});
 
// Client sending message to server
client.send("It is ", 0, 7, 1234, "localhost");


Javascript
// Node.js program to demonstrate the
// writeStream.isTTY() property
 
// Creating and initializing a
// WriteStream object
let WriteStream = process.stdout;
 
// Checking if it is instance of
// TTY or not by using isTTY() method
const status = WriteStream.isTTY;
 
// Displaying the result
if(status)
    console.log("It is an instant of TTY");
else
    console.log("It is not an instant of TTY");


输出:

It is an instant of TTY

示例 2:文件名:index.js

Javascript

// Node.js program to demonstrate the
// writeStream.isTTY() property
 
// Creating and initializing a
// WriteStream object
let WriteStream = process.stdout;
 
// Checking if it is instance of
// TTY or not by using isTTY() method
const status = WriteStream.isTTY;
 
// Displaying the result
if(status)
    console.log("It is an instant of TTY");
else
    console.log("It is not an instant of TTY");

输出:

It is an instant of TTY

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

node index.js

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