Compare commits

..

No commits in common. "main" and "1.0" have entirely different histories.
main ... 1.0

7 changed files with 37 additions and 78 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2016 Copyright (c) 2014
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
@ -24,4 +24,4 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,45 +0,0 @@
# Console Progress Bar
Simple progress bar for long-running operations.
## Java/Groovy
Build with gradle:
gradle assemble
Example usage:
```java
import com.jdbernard.util.ConsoleProgressBar
// ...
ConsoleProgressBar progressBar = new ConsoleProgressBar()
progressBar.setOut(System.out) // optional
progressBar.setMax(100)
for (int i = 0; i <= 100; i++) {
progressBar.update(i, "Message for " + i);
Thread.sleep(500);
}
```
## Nim
Install the library using nimble:
nimble install
Example usage:
```nim
import os, console_progress
var progress = newProgress(sout = stdout, maxValue = 100)
for i in 0..100:
progress.updateProgress(i, "Message for " & i)
sleep(500)
```

View File

@ -2,7 +2,7 @@ apply plugin: "groovy"
apply plugin: "maven" apply plugin: "maven"
group = "com.jdbernard" group = "com.jdbernard"
version = "1.2.2" version = "1.0"
repositories { repositories {
mavenLocal() mavenLocal()

View File

@ -1,6 +1,6 @@
# Package # Package
version = "1.2.2" version = "1.0"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Utility for writing dynamic progress bars to the console." description = "Utility for writing dynamic progress bars to the console."
license = "BSD" license = "BSD"
@ -9,3 +9,4 @@ srcDir = "src/main/nim"
# Dependencies # Dependencies
requires "nim >= 0.13.0" requires "nim >= 0.13.0"

View File

@ -8,20 +8,20 @@ package com.jdbernard.util
* counter is 1-based because the current step is complete for this counter. * counter is 1-based because the current step is complete for this counter.
* @author Jonathan Bernard (jdbernard@gmail.com) * @author Jonathan Bernard (jdbernard@gmail.com)
*/ */
public class ConsoleProgressBar { class ConsoleProgressBar {
int WIDTH = 79 int MAX_STEP = 30
int MAX_STEP = 35
long max = 10 long max = 10
def out = System.out def out = System.out
private int lastStepAmount = -1 private int lastStepAmount = -1
private String lastLinePrinted = ""
private String lastInfo = "" private String lastInfo = ""
private long startTime private long startTime
public void setMax(long max) { public void setMax(long max) {
this.max = Math.max(max, 1) } this.max = Math.max(max, 1) }
public void update(long value, String info) { void update(long value, String info) {
if (value == 0 || startTime == 0) if (value == 0 || startTime == 0)
startTime = System.currentTimeMillis() startTime = System.currentTimeMillis()
@ -50,17 +50,20 @@ public class ConsoleProgressBar {
if (info.length() > 16) info = info[0..<16] if (info.length() > 16) info = info[0..<16]
if (info.length() < 16) info = info.padRight(16) if (info.length() < 16) info = info.padRight(16)
out.print '\b' * WIDTH out.print '\b' * lastLinePrinted.length()
def line = = '=' * numEq + (curStep > 0 ? "0" : "") + '-' * (MAX_STEP - curStep) lastLinePrinted = '=' * numEq + (curStep > 0 ? "0" : "") + '-' * (MAX_STEP - curStep)
line += " ${info} -- (" + lastLinePrinted += " ${info} -- (" +
"${String.format('%5.2f', curPercent * 100)}%, ${remMin ? remMin + 'm ' : ''}${remSec}s) " "${String.format('%5.2f', curPercent * 100)}%, ${remMin ? remMin + 'm ' : ''}${remSec}s) "
out.print line.padRight(WIDTH) out.print lastLinePrinted
lastStepAmount = curStep; lastStepAmount = curStep;
} }
out.flush() out.flush()
} }
public void erase() { void erase() {
out.print ('\b' * WIDTH) + (' ' * WIDTH) + ('\b' * WIDTH) out.print '\b' * lastLinePrinted.length()
out.print ' ' * lastLinePrinted.length()
out.print '\b' * lastLinePrinted.length()
lastLinePrinted = ""
} }
} }

View File

@ -2,9 +2,9 @@ import strutils, times, math
type Progress* = ref object of RootObj type Progress* = ref object of RootObj
sout: File sout: File
lastStep, width, maxSteps: int lastStep, maxSteps: int
startTime: float startTime: float
lastInfo: string lastLinePrinted, lastInfo: string
maxValue: BiggestInt maxValue: BiggestInt
proc getMax*(pd: Progress): BiggestInt = proc getMax*(pd: Progress): BiggestInt =
@ -13,13 +13,13 @@ proc getMax*(pd: Progress): BiggestInt =
proc setMax*(pd: Progress, maxValue: BiggestInt) = proc setMax*(pd: Progress, maxValue: BiggestInt) =
pd.maxValue = max(maxValue, 1) pd.maxValue = max(maxValue, 1)
proc newProgress*(maxValue: BiggestInt, sout: File = stdout): Progress = proc newProgress*(sout: File, maxValue: BiggestInt): Progress =
return Progress(sout: sout, return Progress(sout: sout,
startTime: cpuTime(), startTime: cpuTime(),
lastStep: 0, lastStep: 0,
lastLinePrinted: "",
maxValue: maxValue, maxValue: maxValue,
width: 79, maxSteps: 30)
maxSteps: 35)
proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void = proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
@ -30,8 +30,8 @@ proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
if info == pd.lastInfo and curStep == pd.lastStep: return if info == pd.lastInfo and curStep == pd.lastStep: return
let curDuration = cpuTime() - pd.startTime let curTime = cpuTime()
let remTime = ((curDuration / curPercent) - curDuration) let remTime = ((curTime / curPercent) - curTime) * 1000
let displayedSteps = max(curStep - 1, 0) let displayedSteps = max(curStep - 1, 0)
pd.lastInfo = info pd.lastInfo = info
@ -40,29 +40,29 @@ proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
if displayedInfo.len < 16: if displayedInfo.len < 16:
displayedInfo = displayedInfo & ' '.repeat(16 - displayedInfo.len) displayedInfo = displayedInfo & ' '.repeat(16 - displayedInfo.len)
pd.sout.write('\b'.repeat(pd.width)) pd.sout.write('\b'.repeat(pd.lastLinePrinted.len))
var line = pd.lastLinePrinted =
'='.repeat(displayedSteps) & (if curStep > 0: "0" else: "") & '='.repeat(displayedSteps) & (if curStep > 0: "0" else: "") &
'-'.repeat(pd.maxSteps - curStep) & " " & '-'.repeat(pd.maxSteps - curStep) & " " &
displayedInfo & " -- (" & displayedInfo & " -- (" &
(curPercent * 100).formatFloat(ffDecimal, 2) & "%" (curPercent * 100).formatFloat(ffDecimal, 2) & "%"
if curPercent > 0.05: if curPercent > 0.05:
line &= ", " pd.lastLinePrinted &= ", "
if remTime > 60: if remTime > 60:
line &= $floor(remTime / 60).int & "m " pd.lastLinePrinted &= $floor(remTime / 60).int & "m "
line &= $ceil(remTime mod 60) & "s" pd.lastLinePrinted &= $ceil(remTime mod 60) & "s"
line &= ")" pd.lastLinePrinted &= ")"
line &= spaces(max(pd.width - line.len, 0))
pd.sout.write(line) pd.sout.write(pd.lastLinePrinted)
pd.lastStep = curStep pd.lastStep = curStep
pd.sout.flushFile pd.sout.flushFile
proc erase*(pd: Progress): void = proc erase*(pd: Progress): void =
pd.sout.write('\b'.repeat(pd.width)) pd.sout.write('\b'.repeat(pd.lastLinePrinted.len))
pd.sout.write(' '.repeat(pd.width)) pd.sout.write(' '.repeat(pd.lastLinePrinted.len))
pd.sout.write('\b'.repeat(pd.width)) pd.sout.write('\b'.repeat(pd.lastLinePrinted.len))
pd.lastLinePrinted = ""