How to delete the current line on console/command prompt? Varying number of backspaces?

U

Ulf Meinhardt

Assume I would like to output some dynamic textual information on the command prompt/console
about the state of (long lasting) computation.

I don't want to output for every while loop a new line. So I decide to delete the current line
until the start of the current line and output the new information line.

How do I do this?

Just outputting backspaces does not help.

At first this would simply move the cursor to the right WITHOUT ERASING the char under the cursor.

Furthermore the length of line varies on the length of the text output.

Assume the last, current line of the console (outputted with System.out.print()) look like:

"current state=3456 step=8901"<Cursorposition>

This should be erased and the following line should be shown instead:

"current state=7-2 step=8902"<Cursorposition>

How can I achieve this?

Ulf
 
M

Martin Gregorie

Assume I would like to output some dynamic textual information on the
command prompt/console about the state of (long lasting) computation.

I don't want to output for every while loop a new line. So I decide to
delete the current line until the start of the current line and output
the new information line.

How do I do this?

Just outputting backspaces does not help.
The trick is to output "\b \b", i.e. backspace over the end letter,
overwrite it with a space and than emit a second backspace to step back
past the space you just output. Here's an example:

public class Eraser
{
public static void main(String args[])
{
for (int n = 1; n <= 5; n++)
{
try
{
StringBuilder buff = new StringBuilder("Example line ");
for (int i = 0; i < n; i++)
buff.append("*");

String exLine = buff.toString();
System.out.print(exLine);
Thread.sleep(500);
for (int i = 0; i < exLine.length(); i++)
{
System.out.print("\b \b");
Thread.sleep(100);
}
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}

return;
}
}

Its not really an SSCE: I had to put Thread.sleep() statements in so you
can see it working. Without either sleep its so fast at you think it
hasn't done anything and with just the sleep after the line was output
you can't see the erasure happen, giving the impression that asterisks
are being appended to the original line.
 
K

Knute Johnson

Ulf said:
Assume I would like to output some dynamic textual information on the command prompt/console
about the state of (long lasting) computation.

I don't want to output for every while loop a new line. So I decide to delete the current line
until the start of the current line and output the new information line.

How do I do this?

Just outputting backspaces does not help.

At first this would simply move the cursor to the right WITHOUT ERASING the char under the cursor.

Furthermore the length of line varies on the length of the text output.

Assume the last, current line of the console (outputted with System.out.print()) look like:

"current state=3456 step=8901"<Cursorposition>

This should be erased and the following line should be shown instead:

"current state=7-2 step=8902"<Cursorposition>

How can I achieve this?

Ulf

public class test {
public static void main(String[] args) {
System.out.print("hello world");
System.out.print("\r");
System.out.print("good bye! ");
}
}

This works on a Windows XP dos box. I have no idea if it will work on
any other type of terminal. I am pretty sure that this is going to be
problematic given the use of Java and the state of terminals in the 21st
century.
 
J

John B. Matthews

Assume I would like to output some dynamic textual information on the
command prompt/console about the state of (long lasting) computation.

I don't want to output for every while loop a new line. So I decide
to delete the current line until the start of the current line and
output the new information line.

How do I do this?

I suspect this would depend on the features of the console. If the
console is ANSI X3.64 compliant [1], you can use escape sequences [2] to
control the cursor. Here is an example showing columnar output [3]. For
more elaborate control, you might look at the Java Curses Library [4].
Even better, consider a simple application using SwingWorker [5]; a
complete version of the API example is available [6].

[1]<http://en.wikipedia.org/wiki/ANSI_escape_code>
[2]<http://www.mit.edu/~vona/VonaUtils/vona/terminal/
VT100_Escape_Codes.html>
[3]<http://groups.google.com/group/comp.lang.java.programmer/
msg/5f02abeda114d88e>
[4]<http://sourceforge.net/projects/javacurses/>
[5]<http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
[6]<http://swingworker.dev.java.net>
 
J

John B. Matthews

Assume I would like to output some dynamic textual information on the
command prompt/console about the state of (long lasting) computation.

I don't want to output for every while loop a new line. So I decide
to delete the current line until the start of the current line and
output the new information line.

How do I do this?

I suspect this would depend on the features of the console. If the
console is ANSI X3.64 compliant [1], you can use escape sequences [2] to
control the cursor. Here is an example showing columnar output [3]. For
more elaborate control, you might look at the Java Curses Library [4].
Even better, consider a simple application using SwingWorker [5]; a
complete version of the API example is available [6].

[1]<http://en.wikipedia.org/wiki/ANSI_escape_code>
[2]<http://www.mit.edu/~vona/VonaUtils/vona/terminal/
VT100_Escape_Codes.html>
[3]<http://groups.google.com/group/comp.lang.java.programmer/
msg/5f02abeda114d88e>
[4]<http://sourceforge.net/projects/javacurses/>
[5]<http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html>
[6]<http://swingworker.dev.java.net>
 
S

slack_justyb

Assume I would like to output some dynamic textual information on the command prompt/console
about the state of (long lasting) computation.

I don't want to output for every while loop a new line. So I decide to delete the current line
until the start of the current line and output the new information line.

How do I do this?

Just outputting backspaces does not help.

At first this would simply move the cursor to the right WITHOUT ERASING the char under the cursor.

Furthermore the length of line varies on the length of the text output.

Assume the last, current line of the console (outputted with System.out.print()) look like:

"current state=3456 step=8901"<Cursorposition>

This should be erased and the following line should be shown instead:

"current state=7-2 step=8902"<Cursorposition>

How can I achieve this?

Ulf

char bc='\b';
System.out.print("Hello"+bc);

Remember you need to see this from a terminal, not from the output
window of an IDE.
 
M

Martin Gregorie

char bc='\b';
System.out.print("Hello"+bc);

That doesn't work in a Linux console.

All that happens is that "Hello" is output, and backspaced to leave the
cursor over the 'o'. Then the shell prompt overwrites the 'o', giving the
impression of success.

Modify my example code so it uses "\b" instead of "\b \b" as the erase
sequence and run it. You'll see that "\b" just backspaces along the line
without erasing it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top