Skip to content

Instantly share code, notes, and snippets.

@jodersky
Created December 15, 2019 21:41
Show Gist options
  • Save jodersky/d9cc3379191a46a3df85a1a84214524c to your computer and use it in GitHub Desktop.
Save jodersky/d9cc3379191a46a3df85a1a84214524c to your computer and use it in GitHub Desktop.
Loading animation for terminals
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