Last active
January 25, 2024 15:42
-
-
Save bond-/c6d25a99ce345c75d8a4934e62f9a0f0 to your computer and use it in GitHub Desktop.
Command line progress bar in Groovy
This file contains 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
int spaces = 20 | |
int progress = 100 | |
(progress + 1).times { | |
// String * int is a shortcut to repeat string that many times | |
String hash = "#" * Math.ceil((it*spaces)/100.0); | |
// Carraige return (\r) returns the cursor to first character in line, overwriting other characters | |
print(String.format("[%-" + spaces + "s] %d%s\r", hash, it, '%')) | |
// Wait for 100 milli seconds to seem like an animation | |
Thread.sleep(100) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment