| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import sys
- import colorama as color
- DEBUG_LEVEL = 2
- OUTPUT = 0
- DBG_ERROR = 1
- DBG_INFO = 2
- LOCKED = False
- CACHE = []
- color.init(True)
- def output(text='', level=OUTPUT, end='\n'):
- if LOCKED:
- CACHE.append((text, level, end))
- return
- if level <= DEBUG_LEVEL:
- if level == 1:
- text = color.Fore.RED + '[ERROR] ' + text
- if level == 2:
- text = color.Fore.YELLOW + '[INFO] ' + text
- print(text, end=end)
- def ask_input(question, f=str, quit=False):
- output(color.Style.BRIGHT + color.Fore.CYAN + question, end=': ')
- _input = input().strip()
- if quit and _input == 'q':
- return None
- else:
- try:
- return f(_input)
- except:
- output('{0} cannot be casted to type {1}'.format(_input, type(f.__class__)), level=DBG_ERROR)
- def option_picker(question, options, objects=None, selected=None, quit=False, table=None):
- output(color.Style.BRIGHT + color.Fore.CYAN + question, end=':\n')
- width = None
- if type(table) is list:
- header = []
- for column in table:
- _space = '[{:' + str(column[1]) + '}]'
- header.append(_space.format(column[0]))
- _header = ''.join(header)
- width = len(_header)
- output(color.Style.BRIGHT + _header)
- output(color.Style.BRIGHT + ('=' * width))
- for i in range(0, len(options)):
- option = options[i]
- _row = ''
- if i == selected:
- _row += color.Style.BRIGHT + '>'
- if objects is not None and table is not None and len(objects) == len(table):
- for j in range(0, len(objects)):
- _row += '[{{{name}:{size}.{size}}}]'.format(name=str(objects[j]), size=str(table[j][1]))
- # _row += '[{' + str(objects[j]) + ':' + str(table[j][1]) + '.' + str(table[j][1]) + '}] '
- output(_row.format(__id__=str(i), x=option).strip())
- else:
- if i == selected:
- output('>', end='')
- output('[{0:>2}]'.format(i), end=' ')
- output(option)
- if quit:
- if width is not None:
- output(color.Style.BRIGHT + ('=' * width))
- else:
- output('==========')
- output(color.Fore.YELLOW + '[q] cancel')
- return ask_input('Pick option', int, quit=quit)
- def format_bytes(count):
- return '{:04.2f}MB'.format(count / 1048576)
- def lock():
- global LOCKED
- LOCKED = True
- def unlock():
- global LOCKED, CACHE
- for print in CACHE:
- output(print[0], print[1], print[2])
- LOCKED = False
- CACHE = []
- class ProgressBar:
- def __init__(self, total=None, size=100):
- self.size = size
- self.total = total
- self.current = 0
- lock()
- def destroy(self):
- print()
- unlock()
- def report_progress(self, increment=1, progress=None):
- if progress is not None:
- self.current = progress
- else:
- self.current += increment
- self.draw()
- def draw(self):
- if self.total is None: # Marque
- pass
- else:
- percent = int((self.current / self.total) * 100)
- _draw = ('\r[{0:' + str(self.size) + '}] {1}%').format('=' * percent, percent)
- sys.stdout.write(_draw)
- sys.stdout.flush()
|