diff --git a/http_prompt/cli.py b/http_prompt/cli.py index 36dc0bf..5476e26 100644 --- a/http_prompt/cli.py +++ b/http_prompt/cli.py @@ -27,7 +27,7 @@ from .contextio import load_context, save_context from .execution import execute from .lexer import HttpPromptLexer -from .utils import smart_quote +from .utils import smart_quote, get_prompt from .xdg import get_data_dir @@ -160,8 +160,9 @@ def cli(spec, env, url, http_options): while True: try: - text = prompt('%s> ' % context.url, completer=completer, - lexer=lexer, style=style, history=history, + p = get_prompt(context.url, cfg['prompt']) + text = prompt('%s> ' % p, completer=completer, lexer=lexer, + style=style, history=history, auto_suggest=AutoSuggestFromHistory(), on_abort=AbortAction.RETRY, vi_mode=cfg['vi']) except EOFError: diff --git a/http_prompt/defaultconfig.py b/http_prompt/defaultconfig.py index 32f3a39..a7fcc12 100644 --- a/http_prompt/defaultconfig.py +++ b/http_prompt/defaultconfig.py @@ -24,3 +24,16 @@ # When Vi mode is enabled, you use Vi-like keybindings to edit your commands. # When it is disabled, you use Emacs keybindings. vi = False + +# Prompt format using the urlparse module terms. +# Available values: +# 'scheme': URL scheme specifier +# 'netloc': Network location part ([] compliant on dots) +# 'path': Hierarchical path ([] compliant on slashes) +# 'params': Parameters for last path element +# 'query': Query component +# 'fragment': Fragment identifier +# You can add a pythonic [] operator (e.g. path[-2:] returns the last two +# elements of the variable path). +# See https://docs.python.org/2/library/urlparse.html#module-urlparse +prompt = '{scheme}{netloc}{path}{params}{query}{fragment}' diff --git a/http_prompt/utils.py b/http_prompt/utils.py index 5a5d8ba..a5c2755 100644 --- a/http_prompt/utils.py +++ b/http_prompt/utils.py @@ -5,6 +5,7 @@ from prompt_toolkit.shortcuts import create_output from six.moves import range +from six.moves.urllib.parse import urlparse RE_ANSI_ESCAPE = re.compile(r'\x1b[^m]*m') @@ -87,3 +88,40 @@ def colformat(strings, num_sep_spaces=1, terminal_width=None): sep = ' ' * num_sep_spaces for line in lines: yield sep.join(line) + +def get_prompt(url, fmt): + """Generate a prompt following the user config prompt format.""" + prompt = [] + whitelist = [ 'scheme', 'netloc', 'path', 'params', 'query', 'fragment' ] + parse_result = urlparse(url) + + for w in filter(bool, fmt.replace('{', '').split('}')): + try: + attribute = re.search('^[a-z_]+', w).group(0) + except AttributeError: + return url + if attribute not in whitelist: + return url + try: + index = re.search('\[([-?0-9:?])+\]', w).group(0) + if attribute == 'path': + path_slice = eval('parse_result.path.split(\'/\')' + index) + join = '/'.join(path_slice) + new_path = join[1:] if join.startswith('/') else join + prompt.append('/' + new_path) + elif attribute == 'netloc': + netloc_slice = eval('parse_result.netloc.split(\'.\')' + index) + new_netloc = '.'.join(netloc_slice) + prompt.append(new_netloc) + else: + raise AttributeError + except AttributeError: + if attribute == 'scheme': + prompt.append(parse_result.scheme + '://') + elif attribute == 'query' and parse_result.query: + prompt.append('?' + parse_result.query) + elif attribute == 'fragment' and parse_result.fragment: + prompt.append('#' + parse_result.fragment) + else: + prompt.append(getattr(parse_result, attribute)) + return ''.join(prompt)