1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-28 08:38:44 +02:00

Added --style

Closes #6. Thanks, @iromli.
This commit is contained in:
Jakub Roztočil 2012-03-02 01:39:22 +01:00
commit 94c605fac1
3 changed files with 45 additions and 37 deletions

View File

@ -66,6 +66,8 @@ group_only.add_argument('--headers', '-t', dest='print_body',
group_only.add_argument('--body', '-b', dest='print_headers',
action='store_false', default=True,
help='Print only the response body.')
parser.add_argument('--style', '-s', dest='style', default='solarized',
help='Output coloring style')
# ``requests.request`` keyword arguments.
@ -167,11 +169,11 @@ def main():
if args.prettify and sys.stdout.isatty():
if args.print_headers:
status_line = pretty.prettify_http(status_line).strip()
headers = pretty.prettify_http(headers)
status_line = pretty.prettify_http(status_line, args.style).strip()
headers = pretty.prettify_http(headers, args.style)
if args.print_body:
body = pretty.prettify_body(body,
response.headers['content-type'])
response.headers['content-type'], args.style)
if args.print_headers:
print status_line

View File

@ -1,16 +1,23 @@
import json
from functools import partial
import pygments
import re
import os
import json
import pygments
from pygments import token
from pygments.util import ClassNotFound
from pygments.lexers import get_lexer_for_mimetype
from pygments.formatters.terminal256 import Terminal256Formatter
from pygments.formatters.terminal import TerminalFormatter
from pygments.lexer import RegexLexer, bygroups
from pygments import token
from pygments.styles import get_style_by_name, STYLE_MAP
from . import solarized
DEFAULT_STYLE = 'solarized'
AVAILABLE_STYLES = [DEFAULT_STYLE] + STYLE_MAP.keys()
TYPE_JS = 'application/javascript'
FORMATTER = (Terminal256Formatter
if os.environ.get('TERM', None) == 'xterm-256color'
else TerminalFormatter)
class HTTPLexer(RegexLexer):
@ -25,23 +32,20 @@ class HTTPLexer(RegexLexer):
(r'(.*?:)(.+)', bygroups(token.Name, token.String))
]}
if os.environ['TERM'] == 'xterm-256color':
formatter = Terminal256Formatter
class PrettyHttp(object):
def __init__(self, style_name):
if style_name == 'solarized':
style = solarized.SolarizedStyle
else:
formatter = TerminalFormatter
style = get_style_by_name(style_name)
self.formatter = FORMATTER(style=style)
def headers(self, content):
return pygments.highlight(content, HTTPLexer(), self.formatter).strip()
highlight = partial(pygments.highlight,
formatter=formatter(
style=solarized.SolarizedStyle))
highlight_http = partial(highlight, lexer=HTTPLexer())
def prettify_http(headers):
return highlight_http(headers)
def prettify_body(content, content_type):
def body(self, content, content_type):
content_type = content_type.split(';')[0]
if 'json' in content_type:
content_type = TYPE_JS
@ -53,10 +57,9 @@ def prettify_body(content, content_type):
pass
try:
lexer = get_lexer_for_mimetype(content_type)
content = highlight(code=content, lexer=lexer)
if content:
content = content[:-1]
except Exception:
pass
except ClassNotFound:
return content
content = pygments.highlight(content, lexer, self.formatter)
# TODO: Make sure the leading/trailing whitespaces remain.
# Some of the Pygments styles add superfluous line breaks.
return content.strip()

3
tests.py Normal file
View File

@ -0,0 +1,3 @@
import unittest