console.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.console
  4. ~~~~~~~~~~~~~~~~
  5. Format colored console output.
  6. :copyright: Copyright 2006-2017 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", "darkred", "darkgreen", "brown", "darkblue",
  20. "purple", "teal", "lightgray"]
  21. light_colors = ["darkgray", "red", "green", "yellow", "blue",
  22. "fuchsia", "turquoise", "white"]
  23. x = 30
  24. for d, l in zip(dark_colors, light_colors):
  25. codes[d] = esc + "%im" % x
  26. codes[l] = esc + "%i;01m" % x
  27. x += 1
  28. del d, l, x
  29. codes["darkteal"] = codes["turquoise"]
  30. codes["darkyellow"] = codes["brown"]
  31. codes["fuscia"] = codes["fuchsia"]
  32. codes["white"] = codes["bold"]
  33. def reset_color():
  34. return codes["reset"]
  35. def colorize(color_key, text):
  36. return codes[color_key] + text + codes["reset"]
  37. def ansiformat(attr, text):
  38. """
  39. Format ``text`` with a color and/or some attributes::
  40. color normal color
  41. *color* bold color
  42. _color_ underlined color
  43. +color+ blinking color
  44. """
  45. result = []
  46. if attr[:1] == attr[-1:] == '+':
  47. result.append(codes['blink'])
  48. attr = attr[1:-1]
  49. if attr[:1] == attr[-1:] == '*':
  50. result.append(codes['bold'])
  51. attr = attr[1:-1]
  52. if attr[:1] == attr[-1:] == '_':
  53. result.append(codes['underline'])
  54. attr = attr[1:-1]
  55. result.append(codes[attr])
  56. result.append(text)
  57. result.append(codes['reset'])
  58. return ''.join(result)