Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-deterministic progress bar on java command line

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.

like image 531
rgimmy Avatar asked Feb 15 '12 21:02

rgimmy


3 Answers

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) {};
    }
  }
}
like image 77
RealHowTo Avatar answered Oct 26 '22 03:10

RealHowTo


Try using a carriage return, \r.

like image 26
ni-sh Avatar answered Oct 26 '22 02:10

ni-sh


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?

like image 42
Danny Thomas Avatar answered Oct 26 '22 03:10

Danny Thomas