Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[Java snippet] CLI animated progress bar filter_list
Author
Message
[Java snippet] CLI animated progress bar #1
This little code snippet might come in handy for command line tools that need to show a progress.
It uses carriage return character (/r) to move the cursor back to the beginning of the current line thus overwriting the content afterwards.
This way it can show a progress animation. Just try it out.

Example how it looks like:

Code:
[===============>                                   ]30%

And after a few seconds:

Code:
[===================================>               ]70%

I tried to make the code flexible so you can customize it easily (like changing the characters for drawing the progress bar, changing the progress bar width).

Code and sample usage is shown below.

Code:
public class ProgressBar {

    private final int width;
    private String barStart = "[";
    private String barEnd = "]";
    private String arrowBody = "=";
    private String arrowEnd = ">";

    public ProgressBar(int width) {
        this.width = width;
    }

    public ProgressBar(int width, String barStart, String barEnd, String arrowBody,
            String arrowEnd) {
        this.barStart = barStart;
        this.barEnd = barEnd;
        this.arrowBody = arrowBody;
        this.arrowEnd = arrowEnd;
        this.width = width;
    }

    public void printProcessBar(int percent) {
        int processWidth = percent * width / 100;
        System.out.print("\r" + barStart);
        for (int i = 0; i < processWidth; i++) {
            System.out.print(arrowBody);
        }
        System.out.print(arrowEnd);
        for (int i = processWidth; i < width; i++) {
            System.out.print(" ");
        }
        System.out.print(barEnd + percent + "%");
    }

    public static void main(String[] args) throws InterruptedException {
        ProgressBar bar = new ProgressBar(50);
        for (int i = 0; i <= 10; i++) {
            Thread.sleep(600);
            bar.printProcessBar(i * 10);
        }
        System.out.println();
    }

}
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: [Java snippet] CLI animated progress bar #2
This is a neat little piece of code that could prove handy for me , thank you.
Was wondering what gave you the idea for this?

Reply

RE: [Java snippet] CLI animated progress bar #3
I am working on the CLI version of my archivecracker and this will have progress bar.
The current CLI version can be found here: http://www.hackcommunity.com/Thread-Java...iveCracker
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: [Java snippet] CLI animated progress bar #4
Thanx Deque ... It's so nice and clean and working absolutely fine.
I'll try to implement it in my IP logger Blackhat app. Wink
Going to pm you the source code very soon currently working on design.
[Image: cooltext980231940.gif]

Reply







Users browsing this thread: 1 Guest(s)