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.

71 lines
1.7 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.console
  4. ~~~~~~~~~~~~~~~~
  5. Format colored console output.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. esc = "\x1b["
  10. codes = {}
  11. codes[""] = ""
  12. codes["reset"] = esc + "39;49;00m"
  13. codes["bold"] = esc + "01m"
  14. codes["faint"] = esc + "02m"
  15. codes["standout"] = esc + "03m"
  16. codes["underline"] = esc + "04m"
  17. codes["blink"] = esc + "05m"
  18. codes["overline"] = esc + "06m"
  19. dark_colors = ["black", "red", "green", "yellow", "blue",
  20. "magenta", "cyan", "gray"]
  21. light_colors = ["brightblack", "brightred", "brightgreen", "brightyellow", "brightblue",
  22. "brightmagenta", "brightcyan", "white"]
  23. x = 30
  24. for d, l in zip(dark_colors, light_colors):
  25. codes[d] = esc + "%im" % x
  26. codes[l] = esc + "%im" % (60 + x)
  27. x += 1
  28. del d, l, x
  29. codes["white"] = codes["bold"]
  30. def reset_color():
  31. return codes["reset"]
  32. def colorize(color_key, text):
  33. return codes[color_key] + text + codes["reset"]
  34. def ansiformat(attr, text):
  35. """
  36. Format ``text`` with a color and/or some attributes::
  37. color normal color
  38. *color* bold color
  39. _color_ underlined color
  40. +color+ blinking color
  41. """
  42. result = []
  43. if attr[:1] == attr[-1:] == '+':
  44. result.append(codes['blink'])
  45. attr = attr[1:-1]
  46. if attr[:1] == attr[-1:] == '*':
  47. result.append(codes['bold'])
  48. attr = attr[1:-1]
  49. if attr[:1] == attr[-1:] == '_':
  50. result.append(codes['underline'])
  51. attr = attr[1:-1]
  52. result.append(codes[attr])
  53. result.append(text)
  54. result.append(codes['reset'])
  55. return ''.join(result)