writeStream.cursorTo()方法是tty模块中WriteStream类的内置应用程序编程接口,用于将写入流对象的光标移动到指定位置。
用法:
writeStream.cursorTo(x[, y][, callback])
参数:此方法采用以下参数:
- x:它保存光标位置的x轴坐标。
- y:它保存了光标位置的y轴坐标。
- callback:在操作后执行的回调函数。
返回值:如果将写流对象的光标移动到指定位置,则此方法返回布尔值true。
范例1: 文件名:index.js
// Node.js program to demonstrate the
// writeStream.cursorTo() method
// 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;
// Moving cursor to a specified position
// by using cursorTo() API
const col = WriteStream.cursorTo(10, 7, ()=>{
});
// 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("curser is moved:- ",
0, 26, 1234, "localhost");
输出:
curser is moved:- true
范例2: 文件名:index.js
// Node.js program to demonstrate the
// writeStream.cursorTo() method
// Creating and initializing a
// WriteStream object
let WriteStream = process.stdout;
// Moving cursor to a specified position
// by using cursorTo() API
const col = WriteStream.cursorTo(10, 7, ()=>{
});
// Displaying the result
console.log("curser is moved:- " + col);
使用以下命令运行index.js文件:
node index.js
输出:
curser is moved:- true
参考: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_cursorto_x_y_callback
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js GM charcoal()用法及代码示例
- Node.js GM blur()用法及代码示例
- Node.js GM sharpen()用法及代码示例
- Node.js GM drawLine()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js writeStream.cursorTo() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。