Skip to content

Instantly share code, notes, and snippets.

@miguelsaddress
Created February 18, 2016 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelsaddress/c8939467c61579ead7fc to your computer and use it in GitHub Desktop.
Save miguelsaddress/c8939467c61579ead7fc to your computer and use it in GitHub Desktop.
Progress bar in scala. Simulates a line with progress feedback of an action in the console
import scala.util.Random
object ProgressBar extends App {
// list of times to sleep between each step of the progess, with different probabilities
val timesToSleep = List(500, 500, 500, 500, 600, 600, 600, 1000, 1000, 1000, 2000, 2000, 5000)
print("Progress [ ]")
Thread.sleep(500);
// 11 backspaces to override " ]"
(1 to 11).foreach(_ => print("\b"))
for (i <- 1 to 10) {
val time = Random.shuffle(timesToSleep).head
// every dot means a 10%
print(".")
// we fill the gap between the dot and the place
// the the closing bracket should go
(1 to (10 - i)).foreach(_ => print(" "))
// print the closing bracket and the percentage accomplished.
// remember wach step is a 10%
print("] " + (i * 10) + "%")
// no we have to go back to the last point printed, that is
// 15 caracters in total for the " ] xx%" minus the
// number of dots printed
(1 to (15 - i)).foreach(_ => print("\b"))
Thread.sleep(time)
}
println()
println("Done!")
}
ProgressBar.main(Array(""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment