Peter Peter - 2 months ago 13
Javascript Question

node.js fixed line text

i would like to make a really simple cli based ui in node.js.

example:

process1: on hold
process2: 1m 23sec
process3: 5m 13sec
process4: on hold


I want these lines are fixed, console log is ugly.
Is there any solution to do this without heavy 3rd party libraries? My current script is 30 lines long, with logs. I don`t want to make it much more heavier.

something like:

console.write('text', [line number]);

Answer

There are lots of modules if you search on npms.io for things like ansi.

Here is one way to do it.

      import clc from 'cli-color';

      process.stdout.write(clc.erase.screen);
      process.stdout.write(clc.erase.line);
      process.stdout.write(clc.move.to(0,0));
      process.stdout.write('status: ok');
      // .. other stuff happens for awhile
      process.stdout.write(clc.erase.line);
      process.stdout.write(clc.move.to(0,0));
      process.stdout.write('status: issue');