Skip to content

Instantly share code, notes, and snippets.

@wandernauta
Created May 29, 2010 14:41
Show Gist options
  • Save wandernauta/418309 to your computer and use it in GitHub Desktop.
Save wandernauta/418309 to your computer and use it in GitHub Desktop.
import os/Terminal
import math
ProgressBar: class {
value: UInt = 0
maxval: UInt = 100
barwidth: UInt = 80
text := "Please wait..."
thorpe := "#" /* I wanted this to be a Char, but Chars don't have times() */
period := "."
thorpecolor := Color blue
periodcolor := Color grey
init: func() {
println()
}
init: func ~withtext (=text) {
println()
}
update: func() {
"\e[A" print() /* Go up one line */
"\b" times(999) print() /* Erase that line and go to the start of the line */
this text println() /* Print the status text */
ratio := (barwidth as Float/maxval as Float)
Terminal setFgColor(this thorpecolor)
this thorpe times(floor(value * ratio)) print()
Terminal setFgColor(this periodcolor)
this period times(barwidth - floor(value * ratio)) print()
Terminal reset()
}
update: func ~withVal (val: Int) {
if (val > maxval) {
update(maxval)
} else if (val < 0) {
update(0)
} else {
this value = val
update()
}
}
finish: func() {
this update(maxval)
Terminal reset()
"\n" print()
}
}
import os/Time
import progress
import os/Terminal
pb := ProgressBar new("Reticulating splines...")
for (i in 0..10) {
pb update(i*10)
Time sleepMilli(200)
}
pb finish()
pb2 := ProgressBar new("Twiddling thumbs...")
pb2 thorpecolor = Color red
pb2 maxval = 200
for (i in 0..200) {
if ((i % 10) == 0) pb2 text = "Twiddling %d thumbs..." format(i)
pb2 update(i)
Time sleepMilli(20)
}
pb2 finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment