terminal.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """
  2. pygments.formatters.terminal
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Formatter for terminal output with ANSI sequences.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.formatter import Formatter
  9. from pygments.token import Keyword, Name, Comment, String, Error, \
  10. Number, Operator, Generic, Token, Whitespace
  11. from pygments.console import ansiformat
  12. from pygments.util import get_choice_opt
  13. __all__ = ['TerminalFormatter']
  14. #: Map token types to a tuple of color values for light and dark
  15. #: backgrounds.
  16. TERMINAL_COLORS = {
  17. Token: ('', ''),
  18. Whitespace: ('gray', 'brightblack'),
  19. Comment: ('gray', 'brightblack'),
  20. Comment.Preproc: ('cyan', 'brightcyan'),
  21. Keyword: ('blue', 'brightblue'),
  22. Keyword.Type: ('cyan', 'brightcyan'),
  23. Operator.Word: ('magenta', 'brightmagenta'),
  24. Name.Builtin: ('cyan', 'brightcyan'),
  25. Name.Function: ('green', 'brightgreen'),
  26. Name.Namespace: ('_cyan_', '_brightcyan_'),
  27. Name.Class: ('_green_', '_brightgreen_'),
  28. Name.Exception: ('cyan', 'brightcyan'),
  29. Name.Decorator: ('brightblack', 'gray'),
  30. Name.Variable: ('red', 'brightred'),
  31. Name.Constant: ('red', 'brightred'),
  32. Name.Attribute: ('cyan', 'brightcyan'),
  33. Name.Tag: ('brightblue', 'brightblue'),
  34. String: ('yellow', 'yellow'),
  35. Number: ('blue', 'brightblue'),
  36. Generic.Deleted: ('brightred', 'brightred'),
  37. Generic.Inserted: ('green', 'brightgreen'),
  38. Generic.Heading: ('**', '**'),
  39. Generic.Subheading: ('*magenta*', '*brightmagenta*'),
  40. Generic.Prompt: ('**', '**'),
  41. Generic.Error: ('brightred', 'brightred'),
  42. Error: ('_brightred_', '_brightred_'),
  43. }
  44. class TerminalFormatter(Formatter):
  45. r"""
  46. Format tokens with ANSI color sequences, for output in a text console.
  47. Color sequences are terminated at newlines, so that paging the output
  48. works correctly.
  49. The `get_style_defs()` method doesn't do anything special since there is
  50. no support for common styles.
  51. Options accepted:
  52. `bg`
  53. Set to ``"light"`` or ``"dark"`` depending on the terminal's background
  54. (default: ``"light"``).
  55. `colorscheme`
  56. A dictionary mapping token types to (lightbg, darkbg) color names or
  57. ``None`` (default: ``None`` = use builtin colorscheme).
  58. `linenos`
  59. Set to ``True`` to have line numbers on the terminal output as well
  60. (default: ``False`` = no line numbers).
  61. """
  62. name = 'Terminal'
  63. aliases = ['terminal', 'console']
  64. filenames = []
  65. def __init__(self, **options):
  66. Formatter.__init__(self, **options)
  67. self.darkbg = get_choice_opt(options, 'bg',
  68. ['light', 'dark'], 'light') == 'dark'
  69. self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS
  70. self.linenos = options.get('linenos', False)
  71. self._lineno = 0
  72. def format(self, tokensource, outfile):
  73. return Formatter.format(self, tokensource, outfile)
  74. def _write_lineno(self, outfile):
  75. self._lineno += 1
  76. outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno))
  77. def _get_color(self, ttype):
  78. # self.colorscheme is a dict containing usually generic types, so we
  79. # have to walk the tree of dots. The base Token type must be a key,
  80. # even if it's empty string, as in the default above.
  81. colors = self.colorscheme.get(ttype)
  82. while colors is None:
  83. ttype = ttype.parent
  84. colors = self.colorscheme.get(ttype)
  85. return colors[self.darkbg]
  86. def format_unencoded(self, tokensource, outfile):
  87. if self.linenos:
  88. self._write_lineno(outfile)
  89. for ttype, value in tokensource:
  90. color = self._get_color(ttype)
  91. for line in value.splitlines(True):
  92. if color:
  93. outfile.write(ansiformat(color, line.rstrip('\n')))
  94. else:
  95. outfile.write(line.rstrip('\n'))
  96. if line.endswith('\n'):
  97. if self.linenos:
  98. self._write_lineno(outfile)
  99. else:
  100. outfile.write('\n')
  101. if self.linenos:
  102. outfile.write("\n")