Is there a way to remove the last line of output?

A very simple program that prints 3 lines of output:

console.log('a');
console.log('b');
console.log('c');

Is there any way from the program to delete the last line after printing it ?, i.e.

console.log('a');
console.log('b');
console.log('c');
eraseLastLine();
console.log('C');

which will produce:

a
b
C
+4
source share
3 answers

A simple solution is not to print a newline (i.e. not to use console.log).

  • Use process.stdout.writelines without the EOL character to print.
  • Use the carriage return character ( \r) to return to the beginning of the line.
  • Use \e[Kto clear all characters from the cursor position to the end of the line.

Example:

process.stdout.write("000");
process.stdout.write("\n111");
process.stdout.write("\n222");

The output in this line will be:

000
111
222

However, if you do:

process.stdout.write("000");
process.stdout.write("\n111");
process.stdout.write("\n222");
process.stdout.write("\r\x1b[K")
process.stdout.write("333");

Output:

000
111
222\r\x1b[K
333

:

000
111
333
+10

logUpdate NPM vorpal NPM. .

+1

The following works for me:

process.stdout.clearLine();
process.stdout.cursorTo(0); 
+1
source

Source: https://habr.com/ru/post/1610206/


All Articles