Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom prompt text #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions http_prompt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions http_prompt/defaultconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
38 changes: 38 additions & 0 deletions http_prompt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)