def _highlight(
        self, source: str, lexer: Literal["diff", "python"] = "python"
    ) -> str:
        """Highlight the given source if we have markup support."""
        if not source or not self.hasmarkup or not self.code_highlight:
            return source

        pygments_lexer = self._get_pygments_lexer(lexer)
        pygments_formatter = self._get_pygments_formatter()

        highlighted: str = pygments.highlight(
            source, pygments_lexer, pygments_formatter
        )
        # pygments terminal formatter may add a newline when there wasn't one.
        # We don't want this, remove.
        if highlighted[-1] == "\n" and source[-1] != "\n":
            highlighted = highlighted[:-1]

        # Some lexers will not set the initial color explicitly
        # which may lead to the previous color being propagated to the
        # start of the expression, so reset first.
        highlighted = "\x1b[0m" + highlighted

        return highlighted
Highlight the given source if we have markup support.
src/_pytest/_io/terminalwriter.py