Merge pull request #568 from dsego/dsego/ansi-colors

Follow terminal ANSI color styles

Close #524
This commit is contained in:
Jakub Roztocil 2017-03-12 22:44:05 +01:00 committed by GitHub
commit f1d4861fae
2 changed files with 13 additions and 3 deletions

View File

@ -20,7 +20,7 @@ from httpie.input import (
PRETTY_STDOUT_TTY_ONLY, SessionNameValidator, PRETTY_STDOUT_TTY_ONLY, SessionNameValidator,
readable_file_arg, SSL_VERSION_ARG_MAPPING readable_file_arg, SSL_VERSION_ARG_MAPPING
) )
from httpie.output.formatters.colors import AVAILABLE_STYLES, DEFAULT_STYLE from httpie.output.formatters.colors import AVAILABLE_STYLES, DEFAULT_STYLE, PRESET_STYLE
from httpie.plugins import plugin_manager from httpie.plugins import plugin_manager
from httpie.plugins.builtin import BuiltinAuthPlugin from httpie.plugins.builtin import BuiltinAuthPlugin
from httpie.sessions import DEFAULT_SESSIONS_DIR from httpie.sessions import DEFAULT_SESSIONS_DIR

View File

@ -9,6 +9,7 @@ import pygments.style
from pygments.formatters.terminal import TerminalFormatter from pygments.formatters.terminal import TerminalFormatter
from pygments.formatters.terminal256 import Terminal256Formatter from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.lexers.special import TextLexer from pygments.lexers.special import TextLexer
from pygments.lexers.text import HttpLexer as PygmentsHttpLexer
from pygments.util import ClassNotFound from pygments.util import ClassNotFound
from httpie.compat import is_windows from httpie.compat import is_windows
@ -18,6 +19,10 @@ from httpie.plugins import FormatterPlugin
AVAILABLE_STYLES = set(pygments.styles.STYLE_MAP.keys()) AVAILABLE_STYLES = set(pygments.styles.STYLE_MAP.keys())
AVAILABLE_STYLES.add('solarized') AVAILABLE_STYLES.add('solarized')
# This is the native style provided by the terminal emulator color scheme
PRESET_STYLE = 'preset'
AVAILABLE_STYLES.add(PRESET_STYLE)
if is_windows: if is_windows:
# Colors on Windows via colorama don't look that # Colors on Windows via colorama don't look that
# great and fruity seems to give the best result there # great and fruity seems to give the best result there
@ -51,14 +56,19 @@ class ColorFormatter(FormatterPlugin):
except ClassNotFound: except ClassNotFound:
style_class = Solarized256Style style_class = Solarized256Style
if env.colors == 256: if color_scheme != PRESET_STYLE and env.colors == 256:
fmt_class = Terminal256Formatter fmt_class = Terminal256Formatter
else: else:
fmt_class = TerminalFormatter fmt_class = TerminalFormatter
self.formatter = fmt_class(style=style_class) self.formatter = fmt_class(style=style_class)
if color_scheme == PRESET_STYLE:
self.http_lexer = PygmentsHttpLexer()
else:
self.http_lexer = HTTPLexer()
def format_headers(self, headers): def format_headers(self, headers):
return pygments.highlight(headers, HTTPLexer(), self.formatter).strip() return pygments.highlight(headers, self.http_lexer, self.formatter).strip()
def format_body(self, body, mime): def format_body(self, body, mime):
lexer = self.get_lexer(mime, body) lexer = self.get_lexer(mime, body)