Prateek Roushan
Prateek Roushan

Reputation: 43

What is the use of the cursorTo in the nodejs?

I read a file of nodejs and encountered a method cursorTo and I didn't understand it. Please someone explain.

function refreshConsole () {
    if (Settings.properties.preventMessageFlicker) {
        readline.cursorTo(process.stdout, 0, 0);
    } else {
        process.stdout.write('\033c');
    }
}

Upvotes: 2

Views: 3039

Answers (1)

kgangadhar
kgangadhar

Reputation: 5088

If you see the Nodejs documentation for cursorTo, you will find following explanation:

readline.cursorTo(stream, x, y)

  • stream < Writable >
  • x < number >
  • y < number >

The readline.cursorTo() method moves cursor to the specified position in a given TTY stream

If you want to understand how this really works, create a test.js file and copy paste below code in it. Execute in console using node test.js

process.stdin.resume();
process.stdin.setEncoding('utf8');

console.log('This is interactive console for cursorTo explanation');

process.stdin.on('data', function (data) {
   // This is when only x value is given as input 
    for(i = 0; i< 10; i++){
        console.log('here x = '+ i + ' and y = 0' );
        require('readline').cursorTo(process.stdout, i);
    }
    // This is when  x  and y values are given as input
    // for(i = 0; i< 10; i++){
    //     console.log('here x = '+ i + ' and y = '+ i );
    //     require('readline').cursorTo(process.stdout, i, i);
    // }
});

process.on('SIGINT', function(){
    process.stdout.write('\n end \n');
    process.exit();
});
  • For the first for loop you will get the response as below:

    This is interactive console for cursorTo explanation
    hello
    here x = 0 and y = 0
    here x = 1 and y = 0
     here x = 2 and y = 0
      here x = 3 and y = 0
       here x = 4 and y = 0
        here x = 5 and y = 0
         here x = 6 and y = 0
          here x = 7 and y = 0
           here x = 8 and y = 0
            here x = 9 and y = 0
    
  • This is because for each execution of require('readline').cursorTo(process.stdout, i), the cursor will point to next line with respective x-ordinate we are giving and the y-ordinate is zero.

  • For the second for loop(which is commented in the above code), we are passing both x and y ordinates. the output will be as follows:

    here x = 1 and y = 1console for cursorTo explanation
    hhere x = 2 and y = 2
    hehere x = 3 and y = 3
       here x = 4 and y = 4
        here x = 5 and y = 5
         here x = 6 and y = 6
          here x = 7 and y = 7
           here x = 8 and y = 8
            here x = 9 and y = 9
    
  • You may notice in the second output console for cursorTo explanation is overlapped with text here. This is because when both the ordinates(x and y) are given, For (0, 0) it moves back to the point where its started that's near "This is interactive console for cursorTo explanation" and prints location, similarly for each ordinates it moves and prints data.

Upvotes: 5

Related Questions