Skip to content

Instantly share code, notes, and snippets.

@ThatDevopsGuy
Created November 9, 2013 00:04
Show Gist options
  • Save ThatDevopsGuy/7379622 to your computer and use it in GitHub Desktop.
Save ThatDevopsGuy/7379622 to your computer and use it in GitHub Desktop.
A simple progress bar, showing a spinner, and featuring deferred polling which can easily be replaced. To use this as-is, create a file called 'progress' next to the script, and `echo 0 > progress`. Then, echo your new progress into it, and watch the bar update.
#!/usr/bin/env python
from os import popen
from sys import stdout
from time import sleep
largest_max = 0
while True:
with open('progress') as f:
progress = int(f.read()) or 0
# Set the time to wait before polling the progress in the xrange, in seconds:
for i in xrange(10):
w = int(popen('stty size', 'r').read().split()[1])
largest_max = max(largest_max, w)
for char in ['/', '-', '\\', '|']:
line = '['
bar_length = int((w - 8) * (progress / 100.0))
line += '=' * bar_length
if progress == 100:
line += '='
else:
line += char
line = line.ljust(w - 6)
line += '] ' + str(progress).rjust(3) + '%'
stdout.write(' ' * largest_max) # Clear the line forwards
stdout.write('\b' * (largest_max * 2)) # Clear the line backwards
stdout.write(line) # Write the line
stdout.flush() # Flush the line
sleep(0.25) # Sleep 1/4 of a second, for 4 chars
if progress == 100:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment