views:

2731

answers:

5

I have a Java program running in command line mode. I would like to display a progress bar, showing the percentage of job done. The same kind of progress bar you would see using wget under unix. Is there a library to make this possible ?

A: 

Something like this?

http://clpbar.sourceforge.net/

Tom
Nice.. but is it a Java library? Seems like not: http://clpbar.sourceforge.net/doxygen/1.10.9/html/files.html
Jonik
Yep, just like that... but in Java. :)Maybe I'll do something of the kind on my own.Thanks for the link.
g andrieu
A: 

This would be possible with a Java Curses library. This is what I have found. I haven't used it myself and I don't know if it is cross-platform.

kgiannakakis
Curses may be a bit of overhead for the easy usage I need, but that sure is a track. Thanks.
g andrieu
+2  A: 

C# Example but I'm assuming this is the same for System.out.print in Java. Feel free to correct me if I'm wrong.

Basically, you want to write out the \r escape character to the start of your message which will cause the cursor to return to the start of the line (Line Feed) without moving to the next line.

    static string DisplayBar(int i)
    {
        StringBuilder sb = new StringBuilder();

        int x = i / 2;
        sb.Append("|");
        for (int k = 0; k < 50; k++)
            sb.AppendFormat("{0}", ((x <= k) ? " " : "="));
        sb.Append("|");

        return sb.ToString();
    }

    static void Main(string[] args)
    {
        for (int i = 0; i <= 100; i++)
        {
            System.Threading.Thread.Sleep(200);
            Console.Write("\r{0} {1}% Done", DisplayBar(i), i);
        }

        Console.ReadLine();

    }
Eoin Campbell
Nice example, will sure be helpful. Thanks.
g andrieu
+10  A: 

I have implemented this sort of thing before. Its not so much about java, but what characters to send to the console.

The key is the difference between '\n' and '\r'. '\n' goes to the start of a new line. But '\r' is just 'carriage return' - it goes back to the start of the same line.

So the thing to do is to print your progress bar, for example, by printing the string
"|========       |\r"

On the next tick of the progress bar, overwrite the same line with a longer bar. (because we are using \r, we stay on the same line) For example:
"|==========   |\r"

What you have to remember to do, is when done, if you then just print
"done!\n"

You may still have some garbage from the progress bar on the line. So after you are done with the progress bar, be sure to print enough whitespace to remove it from the line. Such as:
"done!               \n"

Hope that helps.

Matthias Wandel
Sure helps. Thanks a lot.
g andrieu
A: 

I found the following code to work correctly. It writes bytes to the output buffer. Perhaps that methods using a writer like the System.out.println() method replaces the occurrences of \r to \n to match the target's native line ending(if not configured properly).

public class Main{
    public static void main(String[] argv) throws Exception{
            String anim= "|/-\\";
            for (int x =0 ; x < 100 ; x++){
                    String data = "\r" + anim.charAt(x % anim.length())  + " " + x ;
                    System.out.write(data.getBytes());
                    Thread.sleep(100);
            }
    }
}
keesj