Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

7,403 views
Skip to first unread message

Ulf Meinhardt

unread,
Aug 23, 2009, 7:57:44 AM8/23/09
to
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

Martin Gregorie

unread,
Aug 23, 2009, 9:18:15 AM8/23/09
to
On Sun, 23 Aug 2009 11:57:44 +0000, Ulf Meinhardt wrote:

> 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.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |

rossum

unread,
Aug 23, 2009, 11:39:10 AM8/23/09
to

Perhaps a simple way would be to pad all output lines with spaces so
they are all the same length. Before printing a line, backspace that
number of spaces.

rossum

Knute Johnson

unread,
Aug 23, 2009, 12:24:47 PM8/23/09
to

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.

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

John B. Matthews

unread,
Aug 23, 2009, 12:28:21 PM8/23/09
to
In article <4a912eb8$0$30232$9b4e...@newsspool1.arcor-online.net>,
ul...@email.com (Ulf Meinhardt) wrote:

> 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>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

John B. Matthews

unread,
Aug 23, 2009, 12:40:41 PM8/23/09
to
In article <4a912eb8$0$30232$9b4e...@newsspool1.arcor-online.net>,
ul...@email.com (Ulf Meinhardt) wrote:

> 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

slack_justyb

unread,
Aug 24, 2009, 10:29:04 PM8/24/09
to

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.

Martin Gregorie

unread,
Aug 25, 2009, 7:04:53 AM8/25/09
to
On Mon, 24 Aug 2009 19:29:04 -0700, slack_justyb wrote:

> 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.

0 new messages