Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
91f1be4d76 | |||
a5dae03897 |
@ -2,7 +2,7 @@ apply plugin: "groovy"
|
|||||||
apply plugin: "maven"
|
apply plugin: "maven"
|
||||||
|
|
||||||
group = "com.jdbernard"
|
group = "com.jdbernard"
|
||||||
version = "1.2.1"
|
version = "1.2.2"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "1.2.1"
|
version = "1.2.3"
|
||||||
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,4 +9,3 @@ srcDir = "src/main/nim"
|
|||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
||||||
requires "nim >= 0.13.0"
|
requires "nim >= 0.13.0"
|
||||||
|
|
||||||
|
@ -1,11 +1,37 @@
|
|||||||
import strutils, times, math
|
import strutils, times, math
|
||||||
|
|
||||||
type Progress* = ref object of RootObj
|
type
|
||||||
sout: File
|
PBDisplayConfig* = object
|
||||||
lastStep, width, maxSteps: int
|
## Progress Bar Display Configuration - controls how the progress bar is
|
||||||
startTime: float
|
## displayed to the terminal
|
||||||
lastInfo: string
|
before*: string ## Written at the start, before the bar. This is
|
||||||
maxValue: BiggestInt
|
## intended for ANSI control characters and must take
|
||||||
|
## up zero characters when printed.
|
||||||
|
after*: string ## Written at the end, after the bar This is intended
|
||||||
|
## for ANSI control characters and must take up zero
|
||||||
|
## characters when printed.
|
||||||
|
completed*: string ## Written for each completed step of the bar. This
|
||||||
|
## should take up one character of space when printed.
|
||||||
|
todo*: string ## Written for each step remaining in the bar. This
|
||||||
|
## should take up one character of space when printed.
|
||||||
|
separator*: string ## Written for the current step, separating completed
|
||||||
|
## and done. This should take up one character of space
|
||||||
|
## when printed.
|
||||||
|
|
||||||
|
Progress* = ref object of RootObj
|
||||||
|
sout: File
|
||||||
|
lastStep, width, maxSteps: int
|
||||||
|
startTime: float
|
||||||
|
lastInfo: string
|
||||||
|
maxValue: BiggestInt
|
||||||
|
displayCfg: PBDisplayConfig
|
||||||
|
|
||||||
|
const DEFAULT_DISPLAY_CFG* = PBDisplayConfig(
|
||||||
|
before: "\x1b[38;5;2m",
|
||||||
|
after: "\x1b[0m",
|
||||||
|
completed: "━",
|
||||||
|
todo: "─",
|
||||||
|
separator: "\x1b[38;5;8m")
|
||||||
|
|
||||||
proc getMax*(pd: Progress): BiggestInt =
|
proc getMax*(pd: Progress): BiggestInt =
|
||||||
return pd.maxValue
|
return pd.maxValue
|
||||||
@ -13,13 +39,20 @@ 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*(sout: File, maxValue: BiggestInt): Progress =
|
proc newProgress*(
|
||||||
|
maxValue: BiggestInt,
|
||||||
|
sout: File = stdout,
|
||||||
|
width: int = 79,
|
||||||
|
maxSteps: int = 35,
|
||||||
|
displayCfg: PBDisplayConfig = DEFAULT_DISPLAY_CFG): Progress =
|
||||||
|
|
||||||
return Progress(sout: sout,
|
return Progress(sout: sout,
|
||||||
startTime: cpuTime(),
|
startTime: cpuTime(),
|
||||||
lastStep: 0,
|
lastStep: 0,
|
||||||
maxValue: maxValue,
|
maxValue: maxValue,
|
||||||
width: 79,
|
width: 79,
|
||||||
maxSteps: 35)
|
maxSteps: 35,
|
||||||
|
displayCfg: displayCfg)
|
||||||
|
|
||||||
proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
|
proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
|
||||||
|
|
||||||
@ -32,7 +65,9 @@ proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
|
|||||||
|
|
||||||
let curDuration = cpuTime() - pd.startTime
|
let curDuration = cpuTime() - pd.startTime
|
||||||
let remTime = ((curDuration / curPercent) - curDuration)
|
let remTime = ((curDuration / curPercent) - curDuration)
|
||||||
let displayedSteps = max(curStep - 1, 0)
|
let displayedSteps =
|
||||||
|
if curStep == pd.maxSteps: curStep
|
||||||
|
else: max(curStep - 1, 0)
|
||||||
|
|
||||||
pd.lastInfo = info
|
pd.lastInfo = info
|
||||||
var displayedInfo = info
|
var displayedInfo = info
|
||||||
@ -43,10 +78,13 @@ proc updateProgress*(pd: Progress, newValue: BiggestInt, info: string): void =
|
|||||||
pd.sout.write('\b'.repeat(pd.width))
|
pd.sout.write('\b'.repeat(pd.width))
|
||||||
|
|
||||||
var line =
|
var line =
|
||||||
'='.repeat(displayedSteps) & (if curStep > 0: "0" else: "") &
|
pd.displayCfg.before &
|
||||||
'-'.repeat(pd.maxSteps - curStep) & " " &
|
pd.displayCfg.completed.repeat(displayedSteps) &
|
||||||
|
(if curStep > 0 and curStep < pd.maxSteps: pd.displayCfg.separator else: "") &
|
||||||
|
pd.displayCfg.todo.repeat(pd.maxSteps - curStep) &
|
||||||
|
pd.displayCfg.after & " " &
|
||||||
displayedInfo & " -- (" &
|
displayedInfo & " -- (" &
|
||||||
(curPercent * 100).formatFloat(ffDecimal, 2) & "%"
|
(curPercent * 100).formatFloat(ffDecimal, 2) & "%"
|
||||||
|
|
||||||
if curPercent > 0.05:
|
if curPercent > 0.05:
|
||||||
line &= ", "
|
line &= ", "
|
||||||
|
8
src/test/nim/test.nim
Normal file
8
src/test/nim/test.nim
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import std/os
|
||||||
|
import ../../main/nim/console_progress
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
let pd = newProgress(100)
|
||||||
|
for i in 0 .. 100:
|
||||||
|
pd.updateProgress(i, "step " & $i)
|
||||||
|
sleep 25
|
Reference in New Issue
Block a user