Advertisement
cclloyd

Python CLI Progress Bar

Mar 12th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. '''
  2. ###########################
  3. This program demonstrates how to do a progress bar for your program in Python CLI.  It uses ASCII escape strings, with string formatted print statements to move the cursor to the correct position at the end of every 3-line print job.  Also works with any sized terminal, since it determines the width, then calculates how many of each character to print.
  4. ###########################
  5. '''
  6.  
  7.  
  8. from __future__ import print_function
  9. import sys
  10. import time
  11. import math
  12.  
  13. def get_terminal_size(fd=1):
  14.     """
  15.    Returns height and width of current terminal. First tries to get
  16.    size via termios.TIOCGWINSZ, then from environment. Defaults to 25
  17.    lines x 80 columns if both methods fail.
  18.  
  19.    :param fd: file descriptor (default: 1=stdout)
  20.    """
  21.     try:
  22.         import fcntl, termios, struct
  23.         hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
  24.     except:
  25.         try:
  26.             hw = (os.environ['LINES'], os.environ['COLUMNS'])
  27.         except:  
  28.             hw = (25, 80)
  29.  
  30.     return hw
  31.  
  32. def get_terminal_height(fd=1):
  33.     """
  34.    Returns height of terminal if it is a tty, 999 otherwise
  35.  
  36.    :param fd: file descriptor (default: 1=stdout)
  37.    """
  38.     if os.isatty(fd):
  39.         height = get_terminal_size(fd)[0]
  40.     else:
  41.         height = 999
  42.    
  43.     return height
  44.  
  45. def get_terminal_width(fd=1):
  46.     """
  47.    Returns width of terminal if it is a tty, 999 otherwise
  48.  
  49.    :param fd: file descriptor (default: 1=stdout)
  50.    """
  51.     if os.isatty(fd):
  52.         width = get_terminal_size(fd)[1]
  53.     else:
  54.         width = 999
  55.  
  56.     return width
  57.  
  58.  
  59. height, width = get_terminal_size()
  60. i = 0
  61. while i < 100:
  62.     i += .13
  63.     if i > 100: i = 100
  64.     spaces = int((((width-10)*i/100)))
  65.     print("%s%s%s" % ("|", "-" * (width-2), "|"))
  66.    
  67.     if i < 100:
  68.         print("|%6.2f %% " % (i), end="")
  69.         print("%s%s%s" % (u"\u2588" * min(width-11, (spaces)), " " * (width-spaces-11), "|"), end="\n")
  70.     if i == 100:
  71.         print("|%6.0f %% " % (i), end="")
  72.         print("%s%s" % (u"\u2588" * (width-11), "|"), end="\n")
  73.  
  74.     print("%s%s%s" % ("|", "-" * (width-2), "|"))
  75.    
  76.     # This is just to show you progress over a set time.  Normally this would not be included.
  77.     time.sleep(.0125)
  78.  
  79.     # You don't want this printed at the end so it doesn't move the cursor back up.  It moves the cursor X
  80.         # lines up for number N in the <>
  81.     if i < 100:
  82.         print("\033[<3>A", end="\r")
  83.         sys.stdout.flush()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement