You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.3 KiB

  1. import sys
  2. import os
  3. from contextlib import contextmanager
  4. class ColorPrint:
  5. def __init__(self, output=sys.stdout):
  6. self.system_output = output
  7. def get_console_width(self):
  8. system_output_fd = self.system_output.fileno()
  9. try:
  10. width, hight = os.get_terminal_size(system_output_fd)
  11. except OSError:
  12. return 80
  13. if width is None:
  14. return 80
  15. else:
  16. return width
  17. def print_right(self, left_offset, right_offset):
  18. console_width = self.get_console_width()
  19. number_of_spaces = console_width - left_offset - right_offset
  20. self.system_output.write(' ' * number_of_spaces)
  21. def print_colored(self, string, attribute='0', foreground='37',
  22. background='40'):
  23. print_parameters = []
  24. if attribute:
  25. print_parameters.append(attribute)
  26. if foreground:
  27. print_parameters.append(foreground)
  28. if background:
  29. print_parameters.append(background)
  30. self.system_output.write('\033[{0}m{1}\033[0m'.
  31. format(';'.join(print_parameters), string))
  32. def print_bright_red(self, string):
  33. self.print_colored(string,
  34. attribute='1',
  35. foreground='31')
  36. def print_bright_green(self, string):
  37. self.print_colored(string,
  38. attribute='1',
  39. foreground='32')
  40. def print_bright_yellow(self, string):
  41. self.print_colored(string,
  42. attribute='1',
  43. foreground='33')
  44. def print_bright_blue(self, string):
  45. self.print_colored(string,
  46. attribute='1',
  47. foreground='34')
  48. def print_default(self, string):
  49. try:
  50. self.system_output.write(string)
  51. except UnicodeError:
  52. self.system_output.write(string.encode('utf-8'))
  53. try:
  54. self.system_output.flush()
  55. except IOError:
  56. exit(1)
  57. def print_line(self, left_arg, right_arg, left_offset=0,
  58. printBR=True):
  59. COLORS = {'default': self.print_default,
  60. 'green': self.print_bright_green,
  61. 'red': self.print_bright_red,
  62. 'yellow': self.print_bright_yellow,
  63. 'blue': self.print_bright_blue}
  64. for color, left_string in left_arg:
  65. left_offset += len(left_string)
  66. COLORS.get(color, self.print_default)(left_string)
  67. right_offset = 0
  68. for color, right_string in right_arg:
  69. right_offset += len(right_string)
  70. if right_offset:
  71. self.print_right(left_offset, right_offset)
  72. for color, right_string in right_arg:
  73. COLORS.get(color, self.print_default)(right_string)
  74. if printBR:
  75. self.system_output.write('\n')
  76. try:
  77. self.system_output.flush()
  78. except IOError:
  79. exit(1)
  80. def print_ok(self, string, left_offset=0, printBR=True):
  81. self.system_output = sys.stdout
  82. self.print_line((('green', ' * '), ('', string)),
  83. (('blue', '['), ('green', ' ok '), ('blue', ']')),
  84. left_offset=left_offset, printBR=printBR)
  85. def print_only_ok(self, string, left_offset=0, printBR=True):
  86. self.system_output = sys.stdout
  87. self.print_line((('', string),),
  88. (('blue', '['), ('green', ' ok '), ('blue', ']')),
  89. left_offset=left_offset, printBR=printBR)
  90. def print_not_ok(self, string, left_offset=0, printBR=True):
  91. self.system_output = sys.stderr
  92. self.print_line((('green', ' * '), ('', string)),
  93. (('blue', '['), ('red', ' !! '), ('blue', ']')),
  94. left_offset=left_offset, printBR=printBR)
  95. def print_only_not_ok(self, string, left_offset=0, printBR=True):
  96. self.system_output = sys.stderr
  97. self.print_line((('', string),),
  98. (('blue', '['), ('red', ' !! '), ('blue', ']')),
  99. left_offset=left_offset, printBR=printBR)
  100. def print_warning(self, string, left_offset=0, printBR=True):
  101. self.system_output = sys.stdout
  102. self.print_line((('yellow', ' * '), ('', string)),
  103. (('', ''),),
  104. left_offset=left_offset, printBR=printBR)
  105. def print_error(self, string, left_offset=0, printBR=True):
  106. self.system_output = sys.stderr
  107. self.print_line((('red', ' * '), ('', string)),
  108. (('', ''),),
  109. left_offset=left_offset, printBR=printBR)
  110. def print_success(self, string, left_offset=0, printBR=True):
  111. self.system_output = sys.stdout
  112. self.print_line((('green', ' * '), ('', string)),
  113. (('', ''),),
  114. left_offset=left_offset, printBR=printBR)
  115. def print_info(self, string, left_offset=0, printBR=True):
  116. self.system_output = sys.stdout
  117. self.print_line((('blue', ' * '), ('', string)),
  118. (('', ''),),
  119. left_offset=left_offset, printBR=printBR)
  120. # Заглушка вместо модуля вывода.
  121. class IOModule:
  122. def __init__(self, save_messages=False):
  123. self.console_output = ColorPrint()
  124. self.save_messages = save_messages
  125. self.messages = []
  126. def set_error(self, message):
  127. if self.save_messages:
  128. self.messages.append(('error', message))
  129. self.console_output.print_error(message)
  130. def set_warning(self, message):
  131. if self.save_messages:
  132. self.messages.append(('warning', message))
  133. self.console_output.print_warning(message)
  134. def set_info(self, message):
  135. if self.save_messages:
  136. self.messages.append(('info', message))
  137. self.console_output.print_info(message)
  138. def set_success(self, message):
  139. if self.save_messages:
  140. self.messages.append(('success', message))
  141. self.console_output.print_ok(message)
  142. def set_failure(self, message):
  143. if self.save_messages:
  144. self.messages.append(('fail', message))
  145. self.console_output.print_not_ok(message)
  146. @contextmanager
  147. def set_task(self, message):
  148. pass