docstr: Highlight the given source code if we have markup support.
docstr_tok: ["Highlight","the","given","source","code","if","we","have","markup","support","."]
code: def _highlight(self, source: str) -> str:
      """Highlight the given source code if we have markup support."""
      from _pytest.config.exceptions import UsageError
  
      if not self.hasmarkup or not self.code_highlight:
          return source
      try:
          from pygments.formatters.terminal import TerminalFormatter
          from pygments.lexers.python import PythonLexer
          from pygments import highlight
          import pygments.util
      except ImportError:
          return source
      else:
          try:
              highlighted: str = highlight(
                  source,
                  PythonLexer(),
                  TerminalFormatter(
                      bg=os.getenv("PYTEST_THEME_MODE", "dark"),
                      style=os.getenv("PYTEST_THEME"),
                  ),
              )
              return highlighted
          except pygments.util.ClassNotFound:
              raise UsageError(
                  "PYTEST_THEME environment variable had an invalid value: '{}'. "
                  "Only valid pygment styles are allowed.".format(
                      os.getenv("PYTEST_THEME")
                  )
              )
          except pygments.util.OptionError:
              raise UsageError(
                  "PYTEST_THEME_MODE environment variable had an invalid value: '{}'. "
                  "The only allowed values are 'dark' and 'light'.".format(
                      os.getenv("PYTEST_THEME_MODE")
                  )
              )
  
imports: import os
  import shutil
  import sys
  from typing import Optional
  from typing import Sequence
  from typing import TextIO
  from .wcwidth import wcswidth
  from _pytest.compat import final
label_desc: Use image node_id 11 for calling the TerminalWriter obj's underlying member method code with example usage: obj._highlight(source) and returns: source, source, highlighted