Finish --style=auto for terminal ANSI colors and make it the default.

Previously (only in the development version), this was called 'preset'.
This commit is contained in:
Jakub Roztocil 2018-11-02 14:53:05 +01:00
parent 91961c6b51
commit ab5a50cee8
3 changed files with 53 additions and 36 deletions

View File

@ -9,8 +9,10 @@ This project adheres to `Semantic Versioning <http://semver.org/>`_.
`1.0.0-dev`_ (unreleased) `1.0.0-dev`_ (unreleased)
------------------------- -------------------------
* Added ``--style=auto`` which follows the terminal ANSI color styles.
* Added ``true``/``false`` as valid values for ``--verify`` * Added ``true``/``false`` as valid values for ``--verify``
(in addition to ``yes``/``no``) and the boolean value is case-insensitive. (in addition to ``yes``/``no``) and the boolean value is case-insensitive.
* Changed the default ``--style`` from ``solarized`` to ``auto`` (except for Windows).
* Fixed default headers being incorrectly case-sensitive. * Fixed default headers being incorrectly case-sensitive.
* Removed Python 2.6 support. * Removed Python 2.6 support.

View File

@ -20,7 +20,9 @@ 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, PRESET_STYLE from httpie.output.formatters.colors import (
AVAILABLE_STYLES, DEFAULT_STYLE, AUTO_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
@ -210,18 +212,21 @@ output_processing.add_argument(
help=""" help="""
Output coloring style (default is "{default}"). One of: Output coloring style (default is "{default}"). One of:
{available} {available_styles}
For this option to work properly, please make sure that the $TERM The "{auto_style}" style follows your terminals ANSI color styles.
environment variable is set to "xterm-256color" or similar
For non-{auto_style} styles to work properly, please make sure that the
$TERM environment variable is set to "xterm-256color" or similar
(e.g., via `export TERM=xterm-256color' in your ~/.bashrc). (e.g., via `export TERM=xterm-256color' in your ~/.bashrc).
""".format( """.format(
default=DEFAULT_STYLE, default=DEFAULT_STYLE,
available='\n'.join( available_styles='\n'.join(
'{0}{1}'.format(8 * ' ', line.strip()) '{0}{1}'.format(8 * ' ', line.strip())
for line in wrap(', '.join(sorted(AVAILABLE_STYLES)), 60) for line in wrap(', '.join(sorted(AVAILABLE_STYLES)), 60)
).rstrip(), ).rstrip(),
auto_style=AUTO_STYLE,
) )
) )

View File

@ -16,19 +16,18 @@ from httpie.compat import is_windows
from httpie.plugins import FormatterPlugin from httpie.plugins import FormatterPlugin
AVAILABLE_STYLES = set(pygments.styles.get_all_styles()) AUTO_STYLE = 'auto' # Follows terminal ANSI color styles
AVAILABLE_STYLES.add('solarized') DEFAULT_STYLE = AUTO_STYLE
SOLARIZED_STYLE = 'solarized' # Bundled here
# 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.
DEFAULT_STYLE = 'fruity' DEFAULT_STYLE = 'fruity'
else:
DEFAULT_STYLE = 'solarized'
AVAILABLE_STYLES = set(pygments.styles.get_all_styles())
AVAILABLE_STYLES.add(SOLARIZED_STYLE)
AVAILABLE_STYLES.add(AUTO_STYLE)
class ColorFormatter(FormatterPlugin): class ColorFormatter(FormatterPlugin):
@ -44,45 +43,56 @@ class ColorFormatter(FormatterPlugin):
def __init__(self, env, explicit_json=False, def __init__(self, env, explicit_json=False,
color_scheme=DEFAULT_STYLE, **kwargs): color_scheme=DEFAULT_STYLE, **kwargs):
super(ColorFormatter, self).__init__(**kwargs) super(ColorFormatter, self).__init__(**kwargs)
if not env.colors: if not env.colors:
self.enabled = False self.enabled = False
return return
# --json, -j use_auto_style = color_scheme == AUTO_STYLE
self.explicit_json = explicit_json has_256_colors = env.colors == 256
if use_auto_style or not has_256_colors:
try: formatter = TerminalFormatter()
style_class = pygments.styles.get_style_by_name(color_scheme) http_lexer = PygmentsHttpLexer()
except ClassNotFound:
style_class = Solarized256Style
if color_scheme != PRESET_STYLE and env.colors == 256:
fmt_class = Terminal256Formatter
else: else:
fmt_class = TerminalFormatter http_lexer = SimplifiedHTTPLexer()
self.formatter = fmt_class(style=style_class) formatter = Terminal256Formatter(
style=self.get_style_class(color_scheme)
)
if color_scheme == PRESET_STYLE: self.explicit_json = explicit_json # --json
self.http_lexer = PygmentsHttpLexer() self.formatter = formatter
else: self.http_lexer = http_lexer
self.http_lexer = HTTPLexer()
def format_headers(self, headers): def format_headers(self, headers):
return pygments.highlight(headers, self.http_lexer, self.formatter).strip() return pygments.highlight(
code=headers,
lexer=self.http_lexer,
formatter=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_for_body(mime, body)
if lexer: if lexer:
body = pygments.highlight(body, lexer, self.formatter) body = pygments.highlight(
code=body,
lexer=lexer,
formatter=self.formatter,
)
return body.strip() return body.strip()
def get_lexer(self, mime, body): def get_lexer_for_body(self, mime, body):
return get_lexer( return get_lexer(
mime=mime, mime=mime,
explicit_json=self.explicit_json, explicit_json=self.explicit_json,
body=body, body=body,
) )
def get_style_class(self, color_scheme):
try:
return pygments.styles.get_style_by_name(color_scheme)
except ClassNotFound:
return Solarized256Style
def get_lexer(mime, explicit_json=False, body=''): def get_lexer(mime, explicit_json=False, body=''):
@ -131,7 +141,7 @@ def get_lexer(mime, explicit_json=False, body=''):
return lexer return lexer
class HTTPLexer(pygments.lexer.RegexLexer): class SimplifiedHTTPLexer(pygments.lexer.RegexLexer):
"""Simplified HTTP lexer for Pygments. """Simplified HTTP lexer for Pygments.
It only operates on headers and provides a stronger contrast between It only operates on headers and provides a stronger contrast between