Re: Represent work in progress on std out
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();
}
}