Created
February 18, 2016 11:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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