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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。