Created
December 15, 2019 21:41
-
-
Save jodersky/d9cc3379191a46a3df85a1a84214524c to your computer and use it in GitHub Desktop.
Loading animation for terminals
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 time, sys, random | |
def loading(count): | |
all_progress = [0] * count | |
sys.stdout.write("\n" * count) # Make sure we have space to draw the bars | |
while any(x < 100 for x in all_progress): | |
time.sleep(0.01) | |
# Randomly increment one of our progress values | |
unfinished = [(i, v) for (i, v) in enumerate(all_progress) if v < 100] | |
index, _ = random.choice(unfinished) | |
all_progress[index] += 1 | |
# Draw the progress bars | |
sys.stdout.write(u"\u001b[1000D") # Move left | |
sys.stdout.write(u"\u001b[" + str(count) + "A") # Move up | |
sys.stdout.write(u"\u001b[34;1m") | |
for progress in all_progress: | |
width = int(progress / 4) | |
print("[" + "⁂" * width + " " * (25 - width) + "][" + str(progress) + "%]") | |
sys.stdout.write(u"\u001b[0m") | |
loading(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment