Compare commits
No commits in common. "main" and "1.1" have entirely different histories.
@ -2,7 +2,7 @@ apply plugin: "groovy"
|
||||
apply plugin: "maven"
|
||||
|
||||
group = "com.jdbernard"
|
||||
version = "1.2.2"
|
||||
version = "1.1"
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
|
@ -1,6 +1,6 @@
|
||||
# Package
|
||||
|
||||
version = "1.2.2"
|
||||
version = "1.1"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Utility for writing dynamic progress bars to the console."
|
||||
license = "BSD"
|
||||
@ -9,3 +9,4 @@ srcDir = "src/main/nim"
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 0.13.0"
|
||||
|
||||
|
@ -9,12 +9,12 @@ package com.jdbernard.util
|
||||
* @author Jonathan Bernard (jdbernard@gmail.com)
|
||||
*/
|
||||
public class ConsoleProgressBar {
|
||||
int WIDTH = 79
|
||||
int MAX_STEP = 35
|
||||
int MAX_STEP = 30
|
||||
|
||||
long max = 10
|
||||
def out = System.out
|
||||
private int lastStepAmount = -1
|
||||
private String lastLinePrinted = ""
|
||||
private String lastInfo = ""
|
||||
private long startTime
|
||||
|
||||
@ -50,17 +50,20 @@ public class ConsoleProgressBar {
|
||||
if (info.length() > 16) info = info[0..<16]
|
||||
if (info.length() < 16) info = info.padRight(16)
|
||||
|
||||
out.print '\b' * WIDTH
|
||||
def line = = '=' * numEq + (curStep > 0 ? "0" : "") + '-' * (MAX_STEP - curStep)
|
||||
line += " ${info} -- (" +
|
||||
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 line.padRight(WIDTH)
|
||||
out.print lastLinePrinted
|
||||
lastStepAmount = curStep;
|
||||
}
|
||||
out.flush()
|
||||
}
|
||||
|
||||
public void erase() {
|
||||
out.print ('\b' * WIDTH) + (' ' * WIDTH) + ('\b' * WIDTH)
|
||||
out.print '\b' * lastLinePrinted.length()
|
||||
out.print ' ' * lastLinePrinted.length()
|
||||
out.print '\b' * lastLinePrinted.length()
|
||||
lastLinePrinted = ""
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ import strutils, times, math
|
||||
|
||||
type Progress* = ref object of RootObj
|
||||
sout: File
|
||||
lastStep, width, maxSteps: int
|
||||
lastStep, maxSteps: int
|
||||
startTime: float
|
||||
lastInfo: string
|
||||
lastLinePrinted, lastInfo: string
|
||||
maxValue: BiggestInt
|
||||
|
||||
proc getMax*(pd: Progress): BiggestInt =
|
||||
@ -13,13 +13,13 @@ proc getMax*(pd: Progress): BiggestInt =
|
||||
proc setMax*(pd: Progress, maxValue: BiggestInt) =
|
||||
pd.maxValue = max(maxValue, 1)
|
||||
|
||||
proc newProgress*(maxValue: BiggestInt, sout: File = stdout): Progress =
|
||||
proc newProgress*(sout: File, maxValue: BiggestInt): Progress =
|
||||
return Progress(sout: sout,
|
||||
startTime: cpuTime(),
|
||||
lastStep: 0,
|
||||
lastLinePrinted: "",
|
||||
maxValue: maxValue,
|
||||
width: 79,
|
||||
maxSteps: 35)
|
||||
maxSteps: 30)
|
||||
|
||||
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
|
||||
|
||||
let curDuration = cpuTime() - pd.startTime
|
||||
let remTime = ((curDuration / curPercent) - curDuration)
|
||||
let curTime = cpuTime()
|
||||
let remTime = ((curTime / curPercent) - curTime) * 1000
|
||||
let displayedSteps = max(curStep - 1, 0)
|
||||
|
||||
pd.lastInfo = info
|
||||
@ -40,29 +40,29 @@ proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
|
||||
if displayedInfo.len < 16:
|
||||
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(pd.maxSteps - curStep) & " " &
|
||||
displayedInfo & " -- (" &
|
||||
(curPercent * 100).formatFloat(ffDecimal, 2) & "%"
|
||||
(curPercent * 100).formatFloat(ffDecimal, 2) & "%"
|
||||
|
||||
if curPercent > 0.05:
|
||||
line &= ", "
|
||||
pd.lastLinePrinted &= ", "
|
||||
if remTime > 60:
|
||||
line &= $floor(remTime / 60).int & "m "
|
||||
line &= $ceil(remTime mod 60) & "s"
|
||||
pd.lastLinePrinted &= $floor(remTime / 60).int & "m "
|
||||
pd.lastLinePrinted &= $ceil(remTime mod 60) & "s"
|
||||
|
||||
line &= ")"
|
||||
line &= spaces(max(pd.width - line.len, 0))
|
||||
pd.lastLinePrinted &= ")"
|
||||
|
||||
pd.sout.write(line)
|
||||
pd.sout.write(pd.lastLinePrinted)
|
||||
pd.lastStep = curStep
|
||||
|
||||
pd.sout.flushFile
|
||||
|
||||
proc erase*(pd: Progress): void =
|
||||
pd.sout.write('\b'.repeat(pd.width))
|
||||
pd.sout.write(' '.repeat(pd.width))
|
||||
pd.sout.write('\b'.repeat(pd.width))
|
||||
pd.sout.write('\b'.repeat(pd.lastLinePrinted.len))
|
||||
pd.sout.write(' '.repeat(pd.lastLinePrinted.len))
|
||||
pd.sout.write('\b'.repeat(pd.lastLinePrinted.len))
|
||||
pd.lastLinePrinted = ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user