From b947a3cf352890c6b1367a88ec18e7dd6cf6bf21 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 25 Jan 2016 14:17:10 -0600 Subject: [PATCH] Initial commit: com.jdbernard.util.ConsoleProgress --- build.gradle | 14 ++++ .../util/.ConsoleProgressBar.groovy.swp | Bin 0 -> 12288 bytes .../jdbernard/util/ConsoleProgressBar.groovy | 69 ++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 build.gradle create mode 100644 src/main/groovy/com/jdbernard/util/.ConsoleProgressBar.groovy.swp create mode 100644 src/main/groovy/com/jdbernard/util/ConsoleProgressBar.groovy 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 0000000000000000000000000000000000000000..1ff9039431596620f2df7fab41cb1e4f45677a22 GIT binary patch literal 12288 zcmeI2&yU+g6vwAXrBEnPxF9OfYuYN#Zek~0b&H_skA)TLilBvEN+qaZVmpbqwkP<< zwpF_VaqgW9KWiZ)_)NTDIbd7)#&Ao_X`;&F9V7TJ`nY z7tZ(Sq`f3?Jtf3v7r(l&cR`3Vuk8_ADwf$u#@w~rwdxIhSEhbztsKzmt*qOja%Fhe zFz12_^Tqr>T=Bt`o^1c7};k+t*+aIa^-e zPhy#PPM!s!0|z{2y&SS46T2|JYfkfE2p9q&kY|I|iwDHg@lKn09H||qXP$oBpfLmt z0YktLFa!(%L%zxwt;ydVD=u7A(bOQ>Z%g`Eh9(oqK^Oz8~ zp>Lp1p%0)%=&${V5&9ka5z3(#py#1|&@Yb)@dflb6hQ}}JC6vl16_i;&`GEc9f4}l zLFhNI_#XNM8bU6_eBOb~We6AohJYbp2<%2+M-Z{}Kl%pup-i*agBUeyL7e#>b*WZk z8(b>S>@hb}{k?vj7Upg3(2s}N$Z7~}K5I|^&isMyw^iVgpP-=6YS49o35)nS#6g&+ zRS>hQ+Q|Izxk%-Rze{eO@bk;}ggUqWvY)7S^!i4I#SiU)N+OwA^9pI8onuWpdOh)@UJ%n6DsL949s$EkRIl&o-VNW~NgD+!su<5rPmACWXF)tr zJJf2CRini+rg_sY)w_&2&ZE^2X_2gQjdYsY2&2}nVe(q7fv$Rs4Xxg}i|=o2uC7z7 z;&GqUJs=llFu{N<4sx;w=oEF98^mqEt-Hq3dn*`@G7BC5IPCQNuy@OJ!Sx3f-XT@)$lQV+GNb|kM(Qq!VNlQ5gmi{KE7pt`(~+H8|70 z@x<-r0LLLe%>wLFpE8xnknrGBAIHTb71Kyvp@B^3icd1B29++b&W&qn=0`Rf5^QSr z+Y7=lNO9C!lx8x~ZPU55^Hi}*-B|4fI^5N-2%WAQH>y2WHBZlDd4y^9C?O8Z@>Jdr z{RVBzFy~ZF8o;pNiy+w#tTo#~s8nKE+8)KZS2>&2E-xt;zN%aVabesf?NY~ICY={P zd7cH#{dg|-!mJ60Qst7SsCH#KK~@IV#c2hs31{k}g%_*edHL8wR`oV6s5Gf~E$JgR z0hx`0ls8(%1-BN|q)tP>DaYU%xP{Bq&wA+Qlc6CL?^C?!w|W2Q=1DLJqv|N9<+o6I z9`%RfKHc~EoK#l)F<}qWy?}^Y9ZbWJ@iw2L!vmVDDACSEqJ(8O;?GA0!bOZeYx%}Di6ADu5w2E`oM?HC- z;((2O-VJWiNn%Mw^?bpus-fqJY~d^{2r-uKWjSOd Mkv++R%~kZ`Z$Fat<8 literal 0 HcmV?d00001 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 = "" + } +}