The writeStream.cursorTo() method is an inbuilt application programming interface of class WriteStream within tty module which is used to move the cursor of the write stream object to a specified position.
Syntax:
writeStream.cursorTo(x[, y][, callback])
Parameters: This method takes the following parameter:
- x: It holds the x-axis coordinate for the cursor position.
- y: It holds the y-axis coordinate for the cursor position.
- callback: The callback function executes after the operation.
Return Value: This method returns a boolean value true if the cursor of the write stream object is moved to a specified position.
Example 1: Filename: index.js
// Node.js program to demonstrate the // writeStream.cursorTo() method // Importing dgram module const dgram = require( 'dgram' );
// Creating and initializing client // and server socket const client = dgram.createSocket( "udp4" );
const 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( "cursor is moved :- " ,
0, 26, 1234, "localhost" );
|
Output:
cursor is moved :- true
Example 2: Filename: 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( "cursor is moved :- " + col);
|
Run the index.js file using the following command:
node index.js
Output:
cursor is moved :- true
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_cursorto_x_y_callback