Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js stdout clearline() and cursorTo() functions

Tags:

node.js

From a node.js tutorial, I see those two process.stdout functions :

process.stdout.clearLine(); process.stdout.cursorTo(0); 

But I'm using a more recent node.js version (4.2.4), and those functions do not exist. I get process.stdout.clearLine is not a function and process.stdout.cursorTo is not a function.

What is the equivalent of clearLine and cursorTo on node.js version 4.2.4 ?

EDIT :

Those are not working either :

process.readline.clearLine(); process.readline.cursorTo(0);  function writeWaitingPercent(p) {     process.readline.clearLine();     process.readline.cursorTo(0);     process.stdout.write(`waiting ... ${p}%`); } 

I get Cannot read property 'clearLine' of undefined

like image 492
trogne Avatar asked Jan 02 '16 20:01

trogne


1 Answers

This is the solution :

First, require readline :

var readline = require('readline'); 

Then, use cursorTo like this :

function writeWaitingPercent(p) {     //readline.clearLine(process.stdout);     readline.cursorTo(process.stdout, 0);     process.stdout.write(`waiting ... ${p}%`); } 

I've commented clearLine, since it's useless in my case (cursorTo move back the cursor to the start)

like image 83
trogne Avatar answered Sep 19 '22 19:09

trogne