Node.js TTY模块

来源:互联网 发布:aes加解密 java 编辑:程序博客网 时间:2024/06/12 21:32

TTY

  • Class: tty.ReadStream
    • readStream.isRaw
    • readStream.setRawMode(mode)
  • Class: tty.WriteStream
    • Event: ‘resize’
    • writeStream.columns
    • writeStream.rows
  • tty.isatty(fd)

The tty module provides the tty.ReadStream and tty.WriteStream classes. In most cases, it will not be necessary or possible to use this module directly. However, it can be accessed using:

tty模块提供了tty.ReadStream和tty.WriteStream类。在大多数情况下,这将是不必要的或不可能直接去使用这个模块。然而它可以使用如下方式访问

const tty = require('tty');

When Node.js detects that it is being run inside a text terminal (“TTY”) context, the process.stdin will, by default, be initialized as an instance of tty.ReadStream and both process.stdout and process.stderr will, by default be instances of tty.WriteStream. The preferred method of determining whether Node.js is being run within a TTY context is to check that the value of the process.stdout.isTTY property is true:

当Node.js检测到它运行在文本终端(TTY)上下文环境中,process.stdin默认将会初始化为tty.ReadStream并且process.stdout和process.stderr默认会实例化为tty.WriteStream。推荐的判断Node.js是否运行在TTY上下文的方式就是去检查process.stdout.isTTY属性是否为真。

这里写图片描述

$ node -p -e "process.stdout.isTTY"true$ node -p -e "Boolean(process.stdout.isTTY)"true$ node -p -e "Boolean(process.stdout.isTTY)" | catfalse


node -p -e “Boolean(process.stdout.isTTY)” | cat
该指令在windows上无法执行,将cat换成type还是会报错

In most cases, there should be little to no reason for an application to create instances of the tty.ReadStream and tty.WriteStream classes.

在大多数情况下,应该没有理由为应用程序创建tty.ReadStream和tty.WriteStream类的实例。

Class: tty.ReadStream

Added in: v0.5.8
The tty.ReadStream class is a subclass of net.Socket that represents the readable side of a TTY. In normal circumstances process.stdin will be the only tty.ReadStream instance in a Node.js process and there should be no reason to create additional instances.

tty.ReadStream类是net.Socket的子类,它代表了一个可读类型的TTY。在正常的情况下,在Node.js进程中,process.stdin将会是tty.ReadStream唯一的实例并且应该没理由去创建另外的实例。

readStream.isRaw

Added in: v0.7.7
A boolean that is true if the TTY is currently configured to operate as a raw device. Defaults to false.

一个布尔值,如果是真表示TTY当前被配置为操作一个原始的设备。默认是假。

原始设备(raw device)

原始设备(raw device)的定义:在一个块设备安装到一个节点之前,对该设备的访问通常是作为一个线性的无结构的字节流来访问的,称为“原始设备(raw device)”。设备上的文件系统是不可访问的。经过安装以后,设备上的文件系统就以访问了。

readStream.setRawMode(mode)

Added in: v0.7.7

  • mode <boolean> If true, configures the tty.ReadStream to operate as a raw device. If false, configures the tty.ReadStream to operate in its default mode. The readStream.isRaw property will be set to the resulting mode.
  • mode <boolean>如果是真,会配置tty.ReadStream来操作一个原始的设备。如果为假,会配置tty.ReadStream使用它自己默认的方式来操作。readStream.isRaw属性将会设置为该结果的模式。

Class: tty.WriteStream

>Added in: v0.5.8
>The tty.WriteStream class is a subclass of net.Socket that represents the writable side of a TTY. In normal circumstances, process.stdout and process.stderr will be the only tty.WriteStream instances created for a Node.js process and there should be no reason to create additional instances.

tty.WriteStream类是net.Socket的子类,它代表了一个可写类型的TTY。在正常情况下,在Node.js进程中,process.stdout和process.stderr将会是tty.WriteStream的唯一实例并且应该没理由去创建另外的实例。

Event: ‘resize’

>Added in: v0.7.7
>The ‘resize’ event is emitted whenever either of the writeStream.columns or writeStream.rows properties have changed. No arguments are passed to the listener callback when called.

无论何时当writeStream.columns或writeStream.rows属性发发生改变时,’resize’事件都将会被触发。当监听器回调函数被调用时,不传递任何参数。

process.stdout.on('resize', () => {  console.log('screen size has changed!');  console.log(`${process.stdout.columns}x${process.stdout.rows}`);});

writeStream.columns

Added in: v0.7.7
A number specifying the number of columns the TTY currently has. This property is updated whenever the ‘resize’ event is emitted.

一个指定当前TTY有多少列的数字。无论何时当’resize’事件被触发时,该属性都会被更新。

writeStream.rows

Added in: v0.7.7
A number specifying the number of rows the TTY currently has. This property is updated whenever the ‘resize’ event is emitted.

一个指定当前TTY有多少行的数字。无论何时当’resize’事件被触发时,该属性都会被更新。

tty.isatty(fd)

Added in: v0.5.8

  • fd <number> A numeric file descriptor
  • fd <number> 一个数值化的文件描述符

The tty.isatty() method returns true if the given fd is associated with a TTY and false if is not.

如果给定的fd与某个TTY有关,则tty.isatty()函数返回真,否则返回false。

1 0
原创粉丝点击