Skip to content

Instantly share code, notes, and snippets.

@Phosphenius
Last active February 19, 2016 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phosphenius/72cb7da322599deceff4 to your computer and use it in GitHub Desktop.
Save Phosphenius/72cb7da322599deceff4 to your computer and use it in GitHub Desktop.
Make ASCII progress bars
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import sys
PROGRESS_CHAR = '#'
EMPTY_CHAR = '.'
def main():
percentage = round(float(sys.argv[1]))
# Clip percentage
percentage = max(0, min(100, percentage))
bar_length = int(sys.argv[2])-(3+len(str(percentage)))
factor = percentage/100.
rest = int((1-factor)*bar_length) * EMPTY_CHAR
progress = (int(factor*bar_length) * PROGRESS_CHAR +
'{0}%'.format(percentage))
print('|', end='')
print(progress + rest, end='')
print('|')
if __name__ == '__main__':
main()
$ ./asciibar.py 42 100
|#######################################42%.......................................................|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment