Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala or Java, how to print a line to console replacing its previous content instead of appending?

A Scala application does some data processing. It would be nice to show processing progress in percents overwriting previous value on change rather than appending a new value to what's already displayed. How to acheive this effect? I use Scala 2.8 on Linux.

like image 376
Ivan Avatar asked Sep 05 '10 04:09

Ivan


2 Answers

Your easiest options are to use the carriage return without linefeed "\r" or the backspace character "\u0008". If you backspace, you need to keep track of how many characters you've emitted so you can back up enough (and blank out any excess). If you carriage return, you need to rewrite the entire line (and write spaces to blank out any excess).

like image 199
Rex Kerr Avatar answered Nov 15 '22 08:11

Rex Kerr


If you were programming in C/C++, the answer would unequivocably be "ncurses".

There is a curses implemention in Java you might be interested in:

Java Curses

like image 35
paulsm4 Avatar answered Nov 15 '22 09:11

paulsm4