Skip to content

Instantly share code, notes, and snippets.

@crevier
Created June 14, 2018 14:05
Show Gist options
  • Save crevier/92291cf77d94c6e282e319d1402be68f to your computer and use it in GitHub Desktop.
Save crevier/92291cf77d94c6e282e319d1402be68f to your computer and use it in GitHub Desktop.
quick and dirty progress bar in groovy for cli
def progressColor(int percent) {
def red = "${(char) 27}[31;49m"
def yellow = "${(char) 27}[33;49m"
def green = "${(char) 27}[32;49m"
if (percent <= 25)
return red
if (percent <= 75)
return yellow
return green
}
def progessBar(String taskName, int progress, int size, String currentElement,long startTime) {
def defaultColor = "${(char) 27}[39;49m"
def percent = (int) ((progress / size) * 100)
def suffix = progress >= size ? "\n" : "\t: $currentElement"
def time = "${System.currentTimeMillis() - startTime} ms "
print "\r$taskName [${progressColor(percent)}${('=' * percent) + (' ' * (100 - percent))}$defaultColor] $percent% $time " + suffix
}
def progress = 0
def startTime = System.currentTimeMillis()
println("small progress")
[1, 2, 3, 4, 5, 6, 7, 8, 9].each {
sleep(400)
progessBar("scanning", ++progress, 9, it.toString(),startTime)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment