Node.js writeStream.cursorTo() Method

The writeStream.cursorTo() method is an inbuilt application programming interface of class WriteStream within tty module which is used to move the cursor of 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 cursor position.
  • y: It holds the y axis coordinate for cursor position.
  • callback: The callback function which executes after the operation.

Return Value: This method returns boolean value true if the cursor of write stream object is moved to a specified position.

Example 1: Filename: index.js



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");

chevron_right


Output:

          curser is moved :-  true

Example 2: Filename: index.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


Run the index.js file using the following command:

node index.js

Output:

          curser is moved :-  true

Reference:  https://nodejs.org/dist/latest-v12.x/docs/api/tty.html#tty_writestream_cursorto_x_y_callback




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.