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


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


writeStream.getWindowSize()方法是tty模块中类WriteStream的内置应用程序编程接口,用于获取与此WriteStream对象相对应的TTY的大小。

用法:

writeStream.getWindowSize()

参数:此方法不接受任何参数。

返回值:此方法返回[numColumns,numRows]类型的数组,其中包含numColumns和numRows。

范例1: 文件名:index.js



// Node.js program to demonstrate the 
// writeStream..getWindowSize()  API 
  
// 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; 
  
  // Getting window size of this write stream 
  // object by using .getWindowSize() API 
  const col = WriteStream.getWindowSize(); 
  
  // Displaying the result 
  process.stdout.write(msg + col[0]); 
  
  // Exiting process 
  process.exit(); 
})// Binding server with port 
.bind(1234, () => { 
}); 
  
// Client sending message to server 
client.send("number columns of writestream:-  ", 0, 
50, 1234, "localhost");

输出:

number columns of writestream:-  182

范例2: 文件名:index.js

// Node.js program to demonstrate the 
// writeStream..getWindowSize()  API 
  
// Creating and initializing a WriteStream object 
let WriteStream = process.stdout; 
  
// Getting window size of this write stream 
// object by using .getWindowSize() method 
const col = WriteStream.getWindowSize(); 
  
// Displaying the result 
console.log("number rows of writestream:-  " + col[1]);

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

node index.js

输出:

number rows of writestream:-  14

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




相关用法


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