Hi,
Is there a way in java to represent work in progress( not using a gui)
on command line?
Cheers,
//mikael
IchBin - 29 Jun 2006 09:05 GMT
> Hi,
>
[quoted text clipped - 4 lines]
>
> //mikael
You could create a thread that writes say output of an array of say 40
"." one at a time until 40 then start over. Or something like that..
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Simon - 29 Jun 2006 11:12 GMT
> Is there a way in java to represent work in progress( not using a gui)
> on command line?
I guess your problem is that you want to overwrite the old progress bar. You can
try to use \b. You can try the following. It needs some obvious improvements,
but it works, at least on my console (Linux). I would be interested to learn if
it also runs on Windows and other consoles. It definitely doesn't work when
System.out is a file, though :-)
public class ProgressBar {
private static final String DELETE =
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
private static void printProgress(int progress, boolean firstTime) {
if (!firstTime) {
System.out.print(DELETE);
}
System.out.print("[");
int hashes = 40 * progress / 100;
for (int i = 0; i < hashes; i++) {
System.out.print("#");
}
for (int i = 0; i < 40-hashes; i++) {
System.out.print(" ");
}
System.out.print("] ");
if (progress < 10) { System.out.print(" "); }
System.out.print(progress + "%");
}
public static void main(String[] argv) {
for (int i = 0; i <= 100; i++) {
printProgress(i, i==0);
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
}
System.out.println();
}
}
Simon - 29 Jun 2006 11:15 GMT
> but it works, at least on my console (Linux). I would be interested to learn if
> it also runs on Windows and other consoles. It definitely doesn't work when
Addendum: Before I wrote this, I was not very convinced that it would work, but
now I see it even works from within Emacs, so I assume it works almost
everywhere. :-)
Simon - 29 Jun 2006 12:59 GMT
> private static final String DELETE =
> "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
well, rather DELETE="\r";
Gordon Beaton - 29 Jun 2006 12:06 GMT
> Is there a way in java to represent work in progress( not using a
> gui) on command line?
Does this help?
http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/ed979
73597daef81
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Dale King - 05 Jul 2006 08:01 GMT
> Hi,
>
> Is there a way in java to represent work in progress( not using a gui)
> on command line?
A lot of techniques that are used in many systems are not portable.
One technique I used when I actually knew how much total work there was
to do and thus could express it as a percentage was to print it in
precents so that I did not have to back up.
So I would print something like this (indicating it is 75% done):
0%.10%.20%.30%.40%.50%.60%.70%.
So every 5% more that was done I would either print the percentage or a
dot to indicate the 5% mark in between.
This would give you a form of progress bar without requiring stdout to
be able to overwrite itself.
Note that you need to be sure to call flush when printing this since you
are not calling the println and it may get buffered.

Signature
Dale King