console.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import sys
  2. import colorama as color
  3. from helper.settings import Settings
  4. OUTPUT = 0
  5. DBG_ERROR = 1
  6. DBG_INFO = 2
  7. LOCKED = True
  8. CACHE = []
  9. color.init(True)
  10. def output(text='', level=OUTPUT, end='\n'):
  11. if LOCKED:
  12. #CACHE.append((text, level, end))
  13. return
  14. if level <= Settings.Debuglvl.get():
  15. if level == 1:
  16. text = color.Fore.RED + '[ERROR] ' + text
  17. if level == 2:
  18. text = color.Fore.YELLOW + '[INFO] ' + text
  19. print(text, end=end)
  20. def ask_input(question, f=str, quit=False):
  21. output(color.Style.BRIGHT + color.Fore.CYAN + question, end=': ')
  22. _input = input().strip()
  23. if quit and _input == 'q':
  24. return None
  25. else:
  26. try:
  27. return f(_input)
  28. except:
  29. output('{0} cannot be casted to type {1}'.format(_input, type(f.__class__)), level=DBG_ERROR)
  30. def option_picker(question, options, objects=None, selected=None, quit=False, table=None):
  31. output(color.Style.BRIGHT + color.Fore.CYAN + question, end=':\n')
  32. width = None
  33. if type(table) is list:
  34. header = []
  35. for column in table:
  36. _space = '[{:' + str(column[1]) + '}]'
  37. header.append(_space.format(column[0]))
  38. _header = ''.join(header)
  39. width = len(_header)
  40. output(color.Style.BRIGHT + _header)
  41. output(color.Style.BRIGHT + ('=' * width))
  42. for i in range(0, len(options)):
  43. option = options[i]
  44. _row = ''
  45. if i == selected:
  46. _row += color.Style.BRIGHT + '>'
  47. if objects is not None and table is not None and len(objects) == len(table):
  48. for j in range(0, len(objects)):
  49. _row += '[{{{name}:{size}.{size}}}]'.format(name=str(objects[j]), size=str(table[j][1]))
  50. # _row += '[{' + str(objects[j]) + ':' + str(table[j][1]) + '.' + str(table[j][1]) + '}] '
  51. output(_row.format(__id__=str(i), x=option))
  52. # output(_row.format(__id__=str(i), x=dict(map(lambda s: s if s is not None else '', option.__dict__.items()))).strip())
  53. else:
  54. if i == selected:
  55. output('>', end='')
  56. output('[{0:>2}]'.format(i), end=' ')
  57. output(option)
  58. if quit:
  59. if width is not None:
  60. output(color.Style.BRIGHT + ('=' * width))
  61. else:
  62. output('==========')
  63. output(color.Fore.YELLOW + '[q] cancel')
  64. return ask_input('Pick option', int, quit=quit)
  65. def format_bytes(count):
  66. return '{:04.2f}MB'.format(count / 1048576)
  67. def lock():
  68. global LOCKED
  69. LOCKED = True
  70. def unlock():
  71. global LOCKED, CACHE
  72. for print in CACHE:
  73. output(print[0], print[1], print[2])
  74. LOCKED = False
  75. CACHE = []
  76. class ProgressBar:
  77. def __init__(self, total=None, size=100):
  78. self.size = size
  79. self.total = total
  80. self.current = 0
  81. lock()
  82. def destroy(self):
  83. print()
  84. unlock()
  85. def report_progress(self, increment=1, progress=None):
  86. if progress is not None:
  87. self.current = progress
  88. else:
  89. self.current += increment
  90. self.draw()
  91. def draw(self):
  92. if self.total is None: # Marque
  93. pass
  94. else:
  95. percent = int((self.current / self.total) * 100)
  96. _draw = ('\r[{0:' + str(self.size) + '}] {1}%').format('=' * percent, percent)
  97. sys.stdout.write(_draw)
  98. sys.stdout.flush()