mkv

pyrograss.py

mkv
Jan 20th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. """pyrograss.py: Progress bar for vt100 terminals"""
  3. __author__    = "ed <irc.rizon.net>"
  4. __license__   = "BSD"
  5. __copyright__ = "2014"
  6.  
  7. from time import sleep
  8. import math
  9.  
  10. """Due to padding/chrome, a value of 78 makes the bar 79 characters wide"""
  11. barWidth = 78
  12.  
  13. """Buffer keeping track of task changes, for adding colours"""
  14. numSteps = [ 0, 0 ]
  15.  
  16. """List of available colours (default to 16-colour terminals)"""
  17. colors = [ 1, 2, 3, 4, 5, 6, 7 ]
  18.  
  19. """The full-height block characters to use, starting with slimmest, ending with full-width"""
  20. blocks = [ u"\u258f", u"\u258e", u"\u258d", u"\u258c", u"\u258b", u"\u258a", u"\u2589", u"\u2588" ]
  21.  
  22. """Print the progress bar. Expects 3 linesr of padding above cursor"""
  23. def progress(now, total, task, tasks, taskStep, taskSteps, slices=True):
  24.  
  25.     # TODO: have wessie refactor this
  26.     global barWidth, numSteps, colors, blocks
  27.     if len(numSteps) <> tasks:
  28.         numSteps = [0] * tasks
  29.  
  30.     bar = ""
  31.     currentWidth = (now * 1.0 / total) * barWidth
  32.     ceiled = int(math.ceil(currentWidth));
  33.     if (task < tasks - 1):
  34.         numSteps[task+1] = ceiled
  35.     for i in xrange (0, ceiled):
  36.  
  37.         # coloring
  38.         if i in numSteps:
  39.             ofs = numSteps.index(i)
  40.             color = ofs%len(colors)
  41.             bar = bar + '\x1B[1;3%dm' % colors[color]
  42.  
  43.         # black border before new color
  44.         fullBlock = blocks[len(blocks)-1]
  45.         if slices and i+1 in numSteps:
  46.             fullBlock = blocks[len(blocks)/3]
  47.  
  48.         # drawing
  49.         if i < currentWidth - 1:
  50.             bar = bar + fullBlock
  51.         else:
  52.             block = int((currentWidth - i) * (len(blocks) - 1))
  53.             bar = bar + blocks[block]
  54.    
  55.     if barWidth > ceiled:
  56.         bar = bar + (' ' * (barWidth - ceiled))
  57.  
  58.     bar = '\x1B[1;30m' + u'\u2581' * (barWidth) + \
  59.         '\n\x1B[1;37m' + bar + blocks[0] + '\n' + \
  60.         '\x1B[1;30m' + u'\u2594' * (barWidth) + '\n\x1B[0m'
  61.  
  62.     print("\x1B[4A%s" \
  63.         "  Total progress: %.2f%%    (task %d/%d at%3d%%, step %d/%d)       " % ( \
  64.         bar, \
  65.         now * 100.0 / total, \
  66.         task + 1, tasks, \
  67.         taskStep * 100.0 / taskSteps, \
  68.         taskStep + 1, taskSteps \
  69.         ))
  70.  
  71.  
  72.  
  73. """simple test case;
  74.       12 tasks with 35 steps each,
  75.       blaze-it steps in total
  76. """
  77. now = 0
  78. total = 420
  79. tasks = 12
  80. steps = 35
  81.  
  82. print "\n\n\n"
  83. for i in xrange (0, tasks):
  84.     for j in xrange (0, steps):
  85.         progress(now, total, i, tasks, j, steps)
  86.         now = now + 1
  87.         sleep(0.025)
  88.  
  89. now = 0
  90. print "\n\n\n"
  91. for i in xrange (0, tasks):
  92.     for j in xrange (0, steps):
  93.         progress(now, total, i, tasks, j, steps, False)
  94.         now = now + 1
  95.         sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment