5

I have a console application in which I would like to put a non-deterministic progress bar on the command line while some heavy computations are done. Currently I simply print out a '.' for each iteration in a while loop similar to the following:

while (continueWork){
    doLotsOfWork();
    System.out.print('.');
}

which works but I was wondering if anyone had a better/cleverer idea since this can get to be a little bit annoying if there are many iterations through the loop.

rgimmy
  • 293
  • 7
  • 15
  • 1
    There's always the classic exponential progress bar. Fills halfway at first iteration, half again(75%) on the second, and so on. Either that or the 'hourglass' approach where you just have one icon spinning the whole time. Unless you have users complaining, your current solution should work fine. The whole point of the progress bar is for the program to tell the user `I'm not dead yet.` anyway. – Thomas Feb 15 '12 at 22:04
  • 2
    There's one or two here... http://stackoverflow.com/questions/2685435/cooler-ascii-spinners/2685509#2685509 – Will Hartung Feb 15 '12 at 22:11

4 Answers4

6

Here an example to show a rotating progress bar and the traditional style :

import java.io.*;
public class ConsoleProgressBar {
    public static void main(String[] argv) throws Exception{
      System.out.println("Rotating progress bar");
      ProgressBarRotating pb1 = new ProgressBarRotating();
      pb1.start();
      int j = 0;
      for (int x =0 ; x < 2000 ; x++){
        // do some activities
        FileWriter fw = new FileWriter("c:/temp/x.out", true);
        fw.write(j++);
        fw.close();
      }
      pb1.showProgress = false;
      System.out.println("\nDone " + j);

      System.out.println("Traditional progress bar");
      ProgressBarTraditional pb2 = new ProgressBarTraditional();
      pb2.start();
      j = 0;
      for (int x =0 ; x < 2000 ; x++){
        // do some activities
        FileWriter fw = new FileWriter("c:/temp/x.out", true);
        fw.write(j++);
        fw.close();
      }
      pb2.showProgress = false;
      System.out.println("\nDone " + j);
    }
}

class ProgressBarRotating extends Thread {
  boolean showProgress = true;
  public void run() {
    String anim= "|/-\\";
    int x = 0;
    while (showProgress) {
      System.out.print("\r Processing " + anim.charAt(x++ % anim.length()));
      try { Thread.sleep(100); }
      catch (Exception e) {};
    }
  }
}

class ProgressBarTraditional extends Thread {
  boolean showProgress = true;
  public void run() {
    String anim  = "=====================";
    int x = 0;
    while (showProgress) {
      System.out.print("\r Processing " 
           + anim.substring(0, x++ % anim.length())
           + " "); 
      try { Thread.sleep(100); }
      catch (Exception e) {};
    }
  }
}
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
3

Try using a carriage return, \r.

ni-sh
  • 31
  • 2
2

In GUI applications the approach is generally a spinning circle or bouncing/cycling progress bar. I remember many console applications using slashes, pipe and hyphen to create a spinning animation:

\ | / - 

You could also use a bouncing character in brackets:

[-----*-----]

Of course, as the other answered mentioned, you want to use return to return to the start of the line, then print the progress, overwriting the existing output.

Edit: Many cooler options mentioned by Will in the comments:

Cooler ASCII Spinners?

Community
  • 1
  • 1
Danny Thomas
  • 1,879
  • 1
  • 18
  • 32
0

If you know how much work you have to do and how much is left (or done), you might consider printing out a percent complete bar graph sort of progress bar. Depending on the scope of this project, this could simply be in ascii or you could also consider using graphics.

Aayush Kumar
  • 1,618
  • 1
  • 11
  • 31