Command-line progress bar

[================>                                 ]   33%

I frequently need a progress bar for applications, in order to visualize what is going on in the application. Below is a Java implementation of one.

    private static int lastPercent;

    public static void updatePercentageBar(float progress) {
        int percent = (int) Math.round(progress * 100);
        if (Math.abs(percent - lastPercent) >= 1) {
            StringBuilder template = new StringBuilder("\r[");
            for (int i = 0; i < 50; i++) {
                if (i < percent * .5) {
                    template.append("=");
                } else if (i == percent * .5) {
                    template.append(">");
                } else {
                    template.append(" ");
                }
            }
            template.append("] %s   ");
            if (percent >= 100) {
                template.append("%n");
            }
            System.out.printf(template.toString(), percent + "%");
            lastPercent = percent;
        }
    }

References

http://nakkaya.com/2009/11/08/command-line-progress-bar/

  • MrL0g1c

    Hi, offtopic, could you check your steam notification, trade offer pending, helps us both complete set, thanks – mrL0G1C