001package org.dllearner.utilities; 002 003/** 004 * Ascii progress meter. On completion this will reset itself, 005 * so it can be reused 006 * <br /><br /> 007 * [================> ] 33% 008 * 009 * From: http://www.avanderw.co.za/command-line-progress-bar/ 010 */ 011public class ProgressBar { 012 int lastPercent; 013 014 /** 015 * called whenever the progress bar needs to be updated. 016 * that is whenever progress was made. 017 * 018 * @param done an int representing the work done so far 019 * @param total an int representing the total work 020 */ 021 public void update(int done, int total) { 022 int percent = (int) Math.round(done/(double)total * 100); 023 if (Math.abs(percent - lastPercent) >= 1) { 024 StringBuilder template = new StringBuilder("\r["); 025 for (int i = 0; i < 50; i++) { 026 if (i < percent * .5) { 027 template.append("="); 028 } else if (i == percent * .5) { 029 template.append(">"); 030 } else { 031 template.append(" "); 032 } 033 } 034 template.append("] %s "); 035 if (percent >= 100) { 036 template.append("%n"); 037 } 038 System.out.printf(template.toString(), percent + "%"); 039 lastPercent = percent; 040 } 041 if (done == total) { 042 lastPercent = 0; 043 System.out.flush(); 044 System.out.println(); 045 } 046 } 047 048}