Repository: httpie/httpie
File: httpie/output/formatters/colors.py (Python)

Code before:
        self.explicit_json = explicit_json  # --json
        self.formatter = formatter
        self.http_lexer = http_lexer

    def format_headers(self, headers: str) -> str:
        return pygments.highlight(
            code=headers,
            lexer=self.http_lexer,
            formatter=self.formatter,
        ).strip()

    def format_body(self, body: str, mime: str) -> str:
        lexer = self.get_lexer_for_body(mime, body)
        if not lexer:
            return body

        # We may want to use a custom JSON lexer for special JSON data.
        if type(lexer) is JsonLexer:
            from ..utils import parse_prefixed_json
            data_prefix = parse_prefixed_json(body)[0]
            if data_prefix:
                # `body` is prefixed with non-JSON data, likely a XSSI magic string.
                # Use our own JSON lexer in order to properly display the data prefix
                # and the actual JSON body.
                from ..lexers.json import EnhancedJsonLexer
                lexer = EnhancedJsonLexer()

        return pygments.highlight(
            code=body,
            lexer=lexer,
            formatter=self.formatter,
        )

    def get_lexer_for_body(
        self, mime: str,
        body: str
    ) -> Optional[Type[Lexer]]:
        return get_lexer(
            mime=mime,
            explicit_json=self.explicit_json,
            body=body,
        )

    @staticmethod
    def get_style_class(color_scheme: str) -> Type[pygments.style.Style]:
        try:
            return pygments.styles.get_style_by_name(color_scheme)
        except ClassNotFound:
            return Solarized256Style



Review comment (suggestion):
You no longer need to check for the prefix. Just swap it. The custom lexer is meant to handle both scenarios.

Code after:

        self.explicit_json = explicit_json  # --json
        self.formatter = formatter
        self.http_lexer = http_lexer

    def format_headers(self, headers: str) -> str:
        return pygments.highlight(
            code=headers,
            lexer=self.http_lexer,
            formatter=self.formatter,
        ).strip()

    def format_body(self, body: str, mime: str) -> str:
        lexer = self.get_lexer_for_body(mime, body)
        if lexer:
            body = pygments.highlight(
                code=body,
                lexer=lexer,
                formatter=self.formatter,
            )
        return body

    def get_lexer_for_body(
        self, mime: str,
        body: str
    ) -> Optional[Type[Lexer]]:
        return get_lexer(
            mime=mime,
            explicit_json=self.explicit_json,
            body=body,
        )

    @staticmethod
    def get_style_class(color_scheme: str) -> Type[pygments.style.Style]:
        try:
            return pygments.styles.get_style_by_name(color_scheme)
        except ClassNotFound:
            return Solarized256Style


def get_lexer(
    mime: str,
    explicit_json=False,
    body=''
) -> Optional[Type[Lexer]]:
    # Build candidate mime type and lexer names.
    mime_types, lexer_names = [mime], []
    type_, subtype = mime.split('/', 1)
    if '+' not in subtype:
        lexer_names.append(subtype)
    else: