Initial commit: com.jdbernard.util.ConsoleProgress
This commit is contained in:
parent
54c1246210
commit
b947a3cf35
14
build.gradle
Normal file
14
build.gradle
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
apply plugin: "groovy"
|
||||||
|
apply plugin: "maven"
|
||||||
|
|
||||||
|
group = "com.jdbernard"
|
||||||
|
version = "1.0"
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenLocal()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
compile localGroovy()
|
||||||
|
}
|
Binary file not shown.
69
src/main/groovy/com/jdbernard/util/ConsoleProgressBar.groovy
Normal file
69
src/main/groovy/com/jdbernard/util/ConsoleProgressBar.groovy
Normal file
@ -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 = ""
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user