Changed print & erase logic to used a fixed max width.
This commit is contained in:
parent
5f7cbeae92
commit
f4dcfd2a24
@ -2,7 +2,7 @@ apply plugin: "groovy"
|
|||||||
apply plugin: "maven"
|
apply plugin: "maven"
|
||||||
|
|
||||||
group = "com.jdbernard"
|
group = "com.jdbernard"
|
||||||
version = "1.1"
|
version = "1.2"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "1.1"
|
version = "1.2"
|
||||||
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,12 +9,12 @@ package com.jdbernard.util
|
|||||||
* @author Jonathan Bernard (jdbernard@gmail.com)
|
* @author Jonathan Bernard (jdbernard@gmail.com)
|
||||||
*/
|
*/
|
||||||
public class ConsoleProgressBar {
|
public class ConsoleProgressBar {
|
||||||
int MAX_STEP = 30
|
int WIDTH = 79
|
||||||
|
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
|
||||||
|
|
||||||
@ -50,20 +50,17 @@ 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' * lastLinePrinted.length()
|
out.print '\b' * WIDTH
|
||||||
lastLinePrinted = '=' * numEq + (curStep > 0 ? "0" : "") + '-' * (MAX_STEP - curStep)
|
def line = = '=' * numEq + (curStep > 0 ? "0" : "") + '-' * (MAX_STEP - curStep)
|
||||||
lastLinePrinted += " ${info} -- (" +
|
line += " ${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 lastLinePrinted
|
out.print line.padRight(WIDTH)
|
||||||
lastStepAmount = curStep;
|
lastStepAmount = curStep;
|
||||||
}
|
}
|
||||||
out.flush()
|
out.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
public void erase() {
|
public void erase() {
|
||||||
out.print '\b' * lastLinePrinted.length()
|
out.print ('\b' * WIDTH) + (' ' * WIDTH) + ('\b' * WIDTH)
|
||||||
out.print ' ' * lastLinePrinted.length()
|
|
||||||
out.print '\b' * lastLinePrinted.length()
|
|
||||||
lastLinePrinted = ""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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, maxSteps: int
|
lastStep, width, maxSteps: int
|
||||||
startTime: float
|
startTime: float
|
||||||
lastLinePrinted, lastInfo: string
|
lastInfo: string
|
||||||
maxValue: BiggestInt
|
maxValue: BiggestInt
|
||||||
|
|
||||||
proc getMax*(pd: Progress): BiggestInt =
|
proc getMax*(pd: Progress): BiggestInt =
|
||||||
@ -17,9 +17,9 @@ 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,
|
||||||
maxSteps: 30)
|
width: 79,
|
||||||
|
maxSteps: 35)
|
||||||
|
|
||||||
proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
|
proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
|
||||||
|
|
||||||
@ -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.lastLinePrinted.len))
|
pd.sout.write('\b'.repeat(pd.width))
|
||||||
|
|
||||||
pd.lastLinePrinted =
|
var line =
|
||||||
'='.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:
|
||||||
pd.lastLinePrinted &= ", "
|
line &= ", "
|
||||||
if remTime > 60:
|
if remTime > 60:
|
||||||
pd.lastLinePrinted &= $floor(remTime / 60).int & "m "
|
line &= $floor(remTime / 60).int & "m "
|
||||||
pd.lastLinePrinted &= $ceil(remTime mod 60) & "s"
|
line &= $ceil(remTime mod 60) & "s"
|
||||||
|
|
||||||
pd.lastLinePrinted &= ")"
|
line &= ")"
|
||||||
|
line &= spaces(max(pd.width - line.len, 0))
|
||||||
|
|
||||||
pd.sout.write(pd.lastLinePrinted)
|
pd.sout.write(line)
|
||||||
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.lastLinePrinted.len))
|
pd.sout.write('\b'.repeat(pd.width))
|
||||||
pd.sout.write(' '.repeat(pd.lastLinePrinted.len))
|
pd.sout.write(' '.repeat(pd.width))
|
||||||
pd.sout.write('\b'.repeat(pd.lastLinePrinted.len))
|
pd.sout.write('\b'.repeat(pd.width))
|
||||||
pd.lastLinePrinted = ""
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user