diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..78c4cb8 --- /dev/null +++ b/build.gradle @@ -0,0 +1,14 @@ +apply plugin: "groovy" +apply plugin: "maven" + +group = "com.jdbernard" +version = "1.0" + +repositories { + mavenLocal() + mavenCentral() +} + +dependencies { + compile localGroovy() +} diff --git a/src/main/groovy/com/jdbernard/util/.ConsoleProgressBar.groovy.swp b/src/main/groovy/com/jdbernard/util/.ConsoleProgressBar.groovy.swp new file mode 100644 index 0000000..1ff9039 Binary files /dev/null and b/src/main/groovy/com/jdbernard/util/.ConsoleProgressBar.groovy.swp differ diff --git a/src/main/groovy/com/jdbernard/util/ConsoleProgressBar.groovy b/src/main/groovy/com/jdbernard/util/ConsoleProgressBar.groovy new file mode 100644 index 0000000..9736216 --- /dev/null +++ b/src/main/groovy/com/jdbernard/util/ConsoleProgressBar.groovy @@ -0,0 +1,69 @@ +package com.jdbernard.util + +/** + * Controls a console-based progress bar. + * This bar has two totals, an overall process total and an individual file + * total. The overall total is 0-based because the current value is incomplete + * (the file counter is the partial completion of the current step). The file + * counter is 1-based because the current step is complete for this counter. + * @author Jonathan Bernard (jdbernard@gmail.com) + */ +class ConsoleProgressBar { + int MAX_STEP = 30 + + long max = 10 + def out = System.out + private int lastStepAmount = -1 + private String lastLinePrinted = "" + private String lastInfo = "" + private long startTime + + public void setMax(long max) { + this.max = Math.max(max, 1) } + + void update(long value, String info) { + if (value == 0 || startTime == 0) + startTime = System.currentTimeMillis() + + def curStep + def curPercent + def curTime + def remTime + + value = Math.min(value, max) + + curStep = Math.floor((value/max) * MAX_STEP) + curPercent = ((double) value / (double) max) + + if (info != lastInfo || curStep != lastStepAmount) { + // time so far + curTime = System.currentTimeMillis() - startTime + // estimate total time based on how far we are + remTime = (curTime / curPercent) - curTime + remTime /= 1000 + + def numEq = Math.max(curStep - 1, 0) + def remMin = curPercent < 0.05 ? '?' : (long) (remTime / 60) + def remSec = curPercent < 0.05 ? '?' : (long) (((remTime / 60.0) - remMin) * 60) + + lastInfo = info + if (info.length() > 16) info = info[0..<16] + if (info.length() < 16) info = info.padRight(16) + + out.print '\b' * lastLinePrinted.length() + lastLinePrinted = '=' * numEq + (curStep > 0 ? "0" : "") + '-' * (MAX_STEP - curStep) + lastLinePrinted += " ${info} -- (" + + "${String.format('%5.2f', curPercent * 100)}%, ${remMin ? remMin + 'm ' : ''}${remSec}s) " + out.print lastLinePrinted + lastStepAmount = curStep; + } + out.flush() + } + + void erase() { + out.print '\b' * lastLinePrinted.length() + out.print ' ' * lastLinePrinted.length() + out.print '\b' * lastLinePrinted.length() + lastLinePrinted = "" + } +}