Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- """pyrograss.py: Progress bar for vt100 terminals"""
- __author__ = "ed <irc.rizon.net>"
- __license__ = "BSD"
- __copyright__ = "2014"
- from time import sleep
- import math
- """Due to padding/chrome, a value of 78 makes the bar 79 characters wide"""
- barWidth = 78
- """Buffer keeping track of task changes, for adding colours"""
- numSteps = [ 0, 0 ]
- """List of available colours (default to 16-colour terminals)"""
- colors = [ 1, 2, 3, 4, 5, 6, 7 ]
- """The full-height block characters to use, starting with slimmest, ending with full-width"""
- blocks = [ u"\u258f", u"\u258e", u"\u258d", u"\u258c", u"\u258b", u"\u258a", u"\u2589", u"\u2588" ]
- """Print the progress bar. Expects 3 linesr of padding above cursor"""
- def progress(now, total, task, tasks, taskStep, taskSteps, slices=True):
- # TODO: have wessie refactor this
- global barWidth, numSteps, colors, blocks
- if len(numSteps) <> tasks:
- numSteps = [0] * tasks
- bar = ""
- currentWidth = (now * 1.0 / total) * barWidth
- ceiled = int(math.ceil(currentWidth));
- if (task < tasks - 1):
- numSteps[task+1] = ceiled
- for i in xrange (0, ceiled):
- # coloring
- if i in numSteps:
- ofs = numSteps.index(i)
- color = ofs%len(colors)
- bar = bar + '\x1B[1;3%dm' % colors[color]
- # black border before new color
- fullBlock = blocks[len(blocks)-1]
- if slices and i+1 in numSteps:
- fullBlock = blocks[len(blocks)/3]
- # drawing
- if i < currentWidth - 1:
- bar = bar + fullBlock
- else:
- block = int((currentWidth - i) * (len(blocks) - 1))
- bar = bar + blocks[block]
- if barWidth > ceiled:
- bar = bar + (' ' * (barWidth - ceiled))
- bar = '\x1B[1;30m' + u'\u2581' * (barWidth) + \
- '\n\x1B[1;37m' + bar + blocks[0] + '\n' + \
- '\x1B[1;30m' + u'\u2594' * (barWidth) + '\n\x1B[0m'
- print("\x1B[4A%s" \
- " Total progress: %.2f%% (task %d/%d at%3d%%, step %d/%d) " % ( \
- bar, \
- now * 100.0 / total, \
- task + 1, tasks, \
- taskStep * 100.0 / taskSteps, \
- taskStep + 1, taskSteps \
- ))
- """simple test case;
- 12 tasks with 35 steps each,
- blaze-it steps in total
- """
- now = 0
- total = 420
- tasks = 12
- steps = 35
- print "\n\n\n"
- for i in xrange (0, tasks):
- for j in xrange (0, steps):
- progress(now, total, i, tasks, j, steps)
- now = now + 1
- sleep(0.025)
- now = 0
- print "\n\n\n"
- for i in xrange (0, tasks):
- for j in xrange (0, steps):
- progress(now, total, i, tasks, j, steps, False)
- now = now + 1
- sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment