Compare commits

...

7 Commits

Author SHA1 Message Date
Batuhan Taskaya
ee5fc59c51 Implement support for bash & completion flow generation 2022-05-20 10:42:38 +03:00
Batuhan Taskaya
b8e0be241c Embed the structurally defined flows into ZSH 2022-05-19 16:06:37 +03:00
Batuhan Taskaya
0dce332b16 more refinements 2022-05-19 14:56:25 +03:00
Batuhan Taskaya
5af4acc798 fish generation etc. 2022-05-18 18:30:46 +03:00
Batuhan Taskaya
310f712010 x 2022-05-16 18:09:59 +03:00
Batuhan Taskaya
d9792084fa Refine zsh to suggest URLs after methods. 2022-05-16 16:10:23 +03:00
Batuhan Taskaya
bd0b18489b Automate ZSH completion. 2022-05-10 21:36:29 +03:00
16 changed files with 908 additions and 140 deletions

View File

@ -0,0 +1,53 @@
METHODS=("GET" "POST" "PUT" "DELETE" "HEAD" "OPTIONS" "PATCH" "TRACE" "CONNECT" )
NORMARG=1 # TO-DO: dynamically calculate this?
_http_complete() {
local cur_word=${COMP_WORDS[COMP_CWORD]}
local prev_word=${COMP_WORDS[COMP_CWORD - 1]}
if [[ "$cur_word" == -* ]]; then
_http_complete_options "$cur_word"
else
if (( COMP_CWORD == NORMARG + 0 )); then
_http_complete_methods "$cur_word"
fi
if (( COMP_CWORD == NORMARG + 0 )); then
_http_complete_url "$cur_word"
fi
if (( COMP_CWORD == NORMARG + 1 )) && [[ " ${METHODS[*]} " =~ " ${prev_word} " ]]; then
_http_complete_url "$cur_word"
fi
if (( COMP_CWORD >= NORMARG + 2 )); then
_httpie_complete_request_item "$cur_word"
fi
if (( COMP_CWORD >= NORMARG + 1 )) && ! [[ " ${METHODS[*]} " =~ " ${prev_word} " ]]; then
_httpie_complete_request_item "$cur_word"
fi
fi
}
complete -o default -F _http_complete http httpie.http httpie.https https
_http_complete_methods() {
local cur_word=$1
local options="GET POST PUT DELETE HEAD OPTIONS PATCH TRACE CONNECT"
COMPREPLY+=( $( compgen -W "$options" -- "$cur_word" ) )
}
_http_complete_url() {
local cur_word=$1
local options="http:// https://"
COMPREPLY+=( $( compgen -W "$options" -- "$cur_word" ) )
}
_httpie_complete_request_item() {
local cur_word=$1
COMPREPLY+=("==" "=" ":=" ":=@")
}
_http_complete_options() {
local cur_word=$1
local options="--json -j --form -f --multipart --boundary --raw --compress -x --pretty --style -s --unsorted --sorted --response-charset --response-mime --format-options --print -p --headers -h --meta -m --body -b --verbose -v --all --stream -S --output -o --download -d --continue -c --quiet -q --session --session-read-only --auth -a --auth-type -A --ignore-netrc --offline --proxy --follow -F --max-redirects --max-headers --timeout --check-status --path-as-is --chunked --verify --ssl --ciphers --cert --cert-key --cert-key-pass --ignore-stdin -I --help --manual --version --traceback --default-scheme --debug "
COMPREPLY=( $( compgen -W "$options" -- "$cur_word" ) )
}

View File

@ -0,0 +1,51 @@
complete -c http -s --json -l -j -d '(default) Serialize data items from the command line as a JSON object.'
complete -c http -s --form -l -f -d 'Serialize data items from the command line as form field data.'
complete -c http -l --multipart -d 'Similar to --form, but always sends a multipart/form-data request (i.e., even without files).'
complete -c http -l --boundary -d 'Specify a custom boundary string for multipart/form-data requests. Only has effect only together with --form.'
complete -c http -l --raw -d 'Pass raw request data without extra processing.'
complete -c http -s --compress -l -x -d 'Compress the content with Deflate algorithm.'
complete -c http -l --pretty -xa "all colors format none" -d 'Control the processing of console outputs.'
complete -c http -s --style -l -s -d 'Output coloring style (default is "auto").'
complete -c http -l --unsorted -d 'Disables all sorting while formatting output.'
complete -c http -l --sorted -d 'Re-enables all sorting options while formatting output.'
complete -c http -l --response-charset -d 'Override the response encoding for terminal display purposes.'
complete -c http -l --response-mime -d 'Override the response mime type for coloring and formatting for the terminal.'
complete -c http -l --format-options -d 'Controls output formatting.'
complete -c http -s --print -l -p -d 'Options to specify what the console output should contain.'
complete -c http -s --headers -l -h -d 'Print only the response headers.'
complete -c http -s --meta -l -m -d 'Print only the response metadata.'
complete -c http -s --body -l -b -d 'Print only the response body.'
complete -c http -s --verbose -l -v -d 'Make output more verbose.'
complete -c http -l --all -d 'Show any intermediary requests/responses.'
complete -c http -s --stream -l -S -d 'Always stream the response body by line, i.e., behave like `tail -f`.'
complete -c http -s --output -l -o -d 'Save output to FILE instead of stdout.'
complete -c http -s --download -l -d -d 'Download the body to a file instead of printing it to stdout.'
complete -c http -s --continue -l -c -d 'Resume an interrupted download (--output needs to be specified).'
complete -c http -s --quiet -l -q -d 'Do not print to stdout or stderr, except for errors and warnings when provided once.'
complete -c http -l --session -d 'Create, or reuse and update a session.'
complete -c http -l --session-read-only -d 'Create or read a session without updating it'
complete -c http -s --auth -l -a -d 'Credentials for the selected (-A) authentication method.'
complete -c http -s --auth-type -l -A -d 'The authentication mechanism to be used.'
complete -c http -l --ignore-netrc -d 'Ignore credentials from .netrc.'
complete -c http -l --offline -d 'Build the request and print it but dont actually send it.'
complete -c http -l --proxy -d 'String mapping of protocol to the URL of the proxy.'
complete -c http -s --follow -l -F -d 'Follow 30x Location redirects.'
complete -c http -l --max-redirects -d 'The maximum number of redirects that should be followed (with --follow).'
complete -c http -l --max-headers -d 'The maximum number of response headers to be read before giving up (default 0, i.e., no limit).'
complete -c http -l --timeout -d 'The connection timeout of the request in seconds.'
complete -c http -l --check-status -d 'Exit with an error status code if the server replies with an error.'
complete -c http -l --path-as-is -d 'Bypass dot segment (/../ or /./) URL squashing.'
complete -c http -l --chunked -d 'Enable streaming via chunked transfer encoding. The Transfer-Encoding header is set to chunked.'
complete -c http -l --verify -d 'If "no", skip SSL verification. If a file path, use it as a CA bundle.'
complete -c http -l --ssl -xa "ssl2.3 tls1 tls1.1 tls1.2" -d 'The desired protocol version to used.'
complete -c http -l --ciphers -d 'A string in the OpenSSL cipher list format.'
complete -c http -l --cert -d 'Specifys a local cert to use as client side SSL certificate.'
complete -c http -l --cert-key -d 'The private key to use with SSL. Only needed if --cert is given.'
complete -c http -l --cert-key-pass -d 'The passphrase to be used to with the given private key.'
complete -c http -s --ignore-stdin -l -I -d 'Do not attempt to read stdin'
complete -c http -l --help -d 'Show this help message and exit.'
complete -c http -l --manual -d 'Show the full manual.'
complete -c http -l --version -d 'Show version and exit.'
complete -c http -l --traceback -d 'Prints the exception traceback should one occur.'
complete -c http -l --default-scheme -d 'The default scheme to use if not specified in the URL.'
complete -c http -l --debug -d 'Print useful diagnostic information for bug reports.'

View File

@ -0,0 +1,146 @@
#compdef http
# Copyright (c) 2015 Github zsh-users
# Based on the initial work of http://github.com/zsh-users
METHODS=("GET" "POST" "PUT" "DELETE" "HEAD" "OPTIONS" "PATCH" "TRACE" "CONNECT" )
_httpie_params () {
local ret=1 expl
local current=$words[$CURRENT]
if (( CURRENT > NORMARG )); then
local predecessor=$words[(( $CURRENT - 1 ))]
fi
if ! [[ $current == -* ]]; then
if (( CURRENT == NORMARG + 0 )); then
_httpie_method && ret=0
fi
if (( CURRENT == NORMARG + 0 )); then
_httpie_url && ret=0
fi
if (( CURRENT == NORMARG + 1 )) && [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]]; then
_httpie_url && ret=0
fi
if (( CURRENT >= NORMARG + 2 )); then
_httpie_request_item && ret=0
fi
if (( CURRENT >= NORMARG + 1 )) && ! [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]]; then
_httpie_request_item && ret=0
fi
fi
return $ret
}
_httpie_request_item() {
compset -P '(#b)([^:@=]#)'
local name=$match[1]
if false; then
false;
elif compset -P ':'; then
_message "$name HTTP Headers"
elif compset -P '=='; then
_message "$name URL Parameters"
elif compset -P '='; then
_message "$name Data Fields"
elif compset -P ':='; then
_message "$name Raw JSON Fields"
elif compset -P '@'; then
_files
else
typeset -a ops
ops=(
"\::Arbitrary HTTP header, e.g X-API-Token:123"
"==:Querystring parameter to the URL, e.g limit==50"
"=:Data fields to be serialized as JSON (default) or Form Data (with --form)"
"\:=:Data field for real JSON types."
"@:Path field for uploading a file."
)
_describe -t httpparams 'parameter types' ops -Q -S ''
fi
return 1;
}
_httpie_method() {
_wanted http_method expl 'Request Method' \
compadd GET POST PUT DELETE HEAD OPTIONS PATCH TRACE CONNECT && ret=0
return 1;
}
_httpie_url() {
local ret=1
if ! [[ -prefix [-+.a-z0-9]#:// ]]; then
local expl
compset -S '[^:/]*' && compstate[to_end]=''
_wanted url-schemas expl 'URL schema' compadd -S '' http:// https:// && ret=0
else
_urls && ret=0
fi
return $ret
}
integer NORMARG
_arguments -n -C -s \
{--json,-j}'[(default) Serialize data items from the command line as a JSON object.]' \
{--form,-f}'[Serialize data items from the command line as form field data.]' \
'--multipart[Similar to --form, but always sends a multipart/form-data request (i.e., even without files).]' \
'--boundary=[Specify a custom boundary string for multipart/form-data requests. Only has effect only together with --form.]' \
'--raw=[Pass raw request data without extra processing.]' \
{--compress,-x}'[Compress the content with Deflate algorithm.]' \
'--pretty=[Control the processing of console outputs.]:PRETTY:(all colors format none)' \
{--style,-s}'=[Output coloring style (default is "auto").]:STYLE:' \
'--unsorted[Disables all sorting while formatting output.]' \
'--sorted[Re-enables all sorting options while formatting output.]' \
'--response-charset=[Override the response encoding for terminal display purposes.]:ENCODING:' \
'--response-mime=[Override the response mime type for coloring and formatting for the terminal.]:MIME_TYPE:' \
'--format-options=[Controls output formatting.]' \
{--print,-p}'=[Options to specify what the console output should contain.]:WHAT:' \
{--headers,-h}'[Print only the response headers.]' \
{--meta,-m}'[Print only the response metadata.]' \
{--body,-b}'[Print only the response body.]' \
{--verbose,-v}'[Make output more verbose.]' \
'--all[Show any intermediary requests/responses.]' \
{--stream,-S}'[Always stream the response body by line, i.e., behave like `tail -f`.]' \
{--output,-o}'=[Save output to FILE instead of stdout.]:FILE:' \
{--download,-d}'[Download the body to a file instead of printing it to stdout.]' \
{--continue,-c}'[Resume an interrupted download (--output needs to be specified).]' \
{--quiet,-q}'[Do not print to stdout or stderr, except for errors and warnings when provided once.]' \
'--session=[Create, or reuse and update a session.]:SESSION_NAME_OR_PATH:' \
'--session-read-only=[Create or read a session without updating it]:SESSION_NAME_OR_PATH:' \
{--auth,-a}'=[Credentials for the selected (-A) authentication method.]:USER[\:PASS] | TOKEN:' \
{--auth-type,-A}'=[The authentication mechanism to be used.]' \
'--ignore-netrc[Ignore credentials from .netrc.]' \
'--offline[Build the request and print it but dont actually send it.]' \
'--proxy=[String mapping of protocol to the URL of the proxy.]:PROTOCOL\:PROXY_URL:' \
{--follow,-F}'[Follow 30x Location redirects.]' \
'--max-redirects=[The maximum number of redirects that should be followed (with --follow).]' \
'--max-headers=[The maximum number of response headers to be read before giving up (default 0, i.e., no limit).]' \
'--timeout=[The connection timeout of the request in seconds.]:SECONDS:' \
'--check-status[Exit with an error status code if the server replies with an error.]' \
'--path-as-is[Bypass dot segment (/../ or /./) URL squashing.]' \
'--chunked[Enable streaming via chunked transfer encoding. The Transfer-Encoding header is set to chunked.]' \
'--verify=[If "no", skip SSL verification. If a file path, use it as a CA bundle.]' \
'--ssl=[The desired protocol version to used.]:SSL:(ssl2.3 tls1 tls1.1 tls1.2)' \
'--ciphers=[A string in the OpenSSL cipher list format.]' \
'--cert=[Specifys a local cert to use as client side SSL certificate.]' \
'--cert-key=[The private key to use with SSL. Only needed if --cert is given.]' \
'--cert-key-pass=[The passphrase to be used to with the given private key.]' \
{--ignore-stdin,-I}'[Do not attempt to read stdin]' \
'--help[Show this help message and exit.]' \
'--manual[Show the full manual.]' \
'--version[Show version and exit.]' \
'--traceback[Prints the exception traceback should one occur.]' \
'--default-scheme=[The default scheme to use if not specified in the URL.]' \
'--debug[Print useful diagnostic information for bug reports.]' \
'*:args:_httpie_params' && return 0

View File

@ -0,0 +1,40 @@
METHODS=({% for method in methods -%} "{{ method }}" {% endfor -%})
NORMARG=1 # TO-DO: dynamically calculate this?
_http_complete() {
local cur_word=${COMP_WORDS[COMP_CWORD]}
local prev_word=${COMP_WORDS[COMP_CWORD - 1]}
if [[ "$cur_word" == -* ]]; then
_http_complete_options "$cur_word"
else
{% for flow_item in generate_flow() -%}
{{ compile_bash(flow_item) | indent(width=8) }}
{% endfor %}
fi
}
complete -o default -F _http_complete http httpie.http httpie.https https
_http_complete_methods() {
local cur_word=$1
local options="{{' '.join(methods)}}"
COMPREPLY+=( $( compgen -W "$options" -- "$cur_word" ) )
}
_http_complete_url() {
local cur_word=$1
local options="http:// https://"
COMPREPLY+=( $( compgen -W "$options" -- "$cur_word" ) )
}
_httpie_complete_request_item() {
local cur_word=$1
COMPREPLY+=("==" "=" ":=" ":=@")
}
_http_complete_options() {
local cur_word=$1
local options="{% for argument in arguments -%} {{ ' '.join(argument.aliases) }} {% endfor -%}"
COMPREPLY=( $( compgen -W "$options" -- "$cur_word" ) )
}

View File

@ -0,0 +1,3 @@
{% for argument in arguments -%}
{{ serialize_argument_to_fish(argument) }}
{% endfor -%}

View File

@ -0,0 +1,76 @@
#compdef http
# Copyright (c) 2015 Github zsh-users
# Based on the initial work of http://github.com/zsh-users
METHODS=({% for method in methods -%} "{{ method }}" {% endfor -%})
_httpie_params () {
local ret=1 expl
local current=$words[$CURRENT]
if (( CURRENT > NORMARG )); then
local predecessor=$words[(( $CURRENT - 1 ))]
fi
if ! [[ $current == -* ]]; then
{% for flow_item in generate_flow() -%}
{{ compile_zsh(flow_item) | indent(width=8) }}
{% endfor %}
fi
return $ret
}
_httpie_request_item() {
compset -P '(#b)([^:@=]#)'
local name=$match[1]
if false; then
false;
{% for option_name, _, operator, desc in request_items.nested_options -%}
elif compset -P '{{ operator }}'; then
{% if is_file_based_operator(operator) -%}
_files
{% else -%}
_message "$name {{ option_name }}"
{% endif %}
{% endfor -%}
else
typeset -a ops
ops=(
{% for option_name, _, operator, desc in request_items.nested_options -%}
"{{ escape_zsh(operator) }}:{{ desc }}"
{% endfor -%}
)
_describe -t httpparams 'parameter types' ops -Q -S ''
fi
return 1;
}
_httpie_method() {
_wanted http_method expl 'Request Method' \
compadd {% for method in methods -%} {{ method }} {% endfor -%} && ret=0
return 1;
}
_httpie_url() {
local ret=1
if ! [[ -prefix [-+.a-z0-9]#:// ]]; then
local expl
compset -S '[^:/]*' && compstate[to_end]=''
_wanted url-schemas expl 'URL schema' compadd -S '' http:// https:// && ret=0
else
_urls && ret=0
fi
return $ret
}
integer NORMARG
_arguments -n -C -s \
{% for argument in arguments -%}
{{ serialize_argument_to_zsh(argument) }} \
{% endfor -%}
'*:args:_httpie_params' && return 0

View File

@ -1,20 +0,0 @@
_http_complete() {
local cur_word=${COMP_WORDS[COMP_CWORD]}
local prev_word=${COMP_WORDS[COMP_CWORD - 1]}
if [[ "$cur_word" == -* ]]; then
_http_complete_options "$cur_word"
fi
}
complete -o default -F _http_complete http httpie.http httpie.https https
_http_complete_options() {
local cur_word=$1
local options="-j --json -f --form --pretty -s --style -p --print
-v --verbose -h --headers -b --body -S --stream -o --output -d --download
-c --continue --session --session-read-only -a --auth --auth-type --proxy
--follow --verify --cert --cert-key --timeout --check-status --ignore-stdin
--help --version --traceback --debug --raw"
COMPREPLY=( $( compgen -W "$options" -- "$cur_word" ) )
}

View File

@ -1,114 +0,0 @@
function __fish_httpie_styles
printf '%s\n' abap algol algol_nu arduino auto autumn borland bw colorful default emacs friendly fruity gruvbox-dark gruvbox-light igor inkpot lovelace manni material monokai murphy native paraiso-dark paraiso-light pastie perldoc pie pie-dark pie-light rainbow_dash rrt sas solarized solarized-dark solarized-light stata stata-dark stata-light tango trac vim vs xcode zenburn
end
function __fish_httpie_mime_types
test -r /usr/share/mime/types && cat /usr/share/mime/types
end
function __fish_httpie_print_args
set -l arg (commandline -t)
string match -qe H "$arg" || echo -e $arg"H\trequest headers"
string match -qe B "$arg" || echo -e $arg"B\trequest body"
string match -qe h "$arg" || echo -e $arg"h\tresponse headers"
string match -qe b "$arg" || echo -e $arg"b\tresponse body"
string match -qe m "$arg" || echo -e $arg"m\tresponse metadata"
end
function __fish_httpie_auth_types
echo -e "basic\tBasic HTTP auth"
echo -e "digest\tDigest HTTP auth"
echo -e "bearer\tBearer HTTP Auth"
end
function __fish_http_verify_options
echo -e "yes\tEnable cert verification"
echo -e "no\tDisable cert verification"
end
# Predefined Content Types
complete -c http -s j -l json -d 'Data items are serialized as a JSON object'
complete -c http -s f -l form -d 'Data items are serialized as form fields'
complete -c http -l multipart -d 'Always sends a multipart/form-data request'
complete -c http -l boundary -x -d 'Custom boundary string for multipart/form-data requests'
complete -c http -l raw -x -d 'Pass raw request data without extra processing'
# Content Processing Options
complete -c http -s x -l compress -d 'Content compressed with Deflate algorithm'
# Output Processing
complete -c http -l pretty -xa "all colors format none" -d 'Controls output processing'
complete -c http -s s -l style -xa "(__fish_httpie_styles)" -d 'Output coloring style'
complete -c http -l unsorted -d 'Disables all sorting while formatting output'
complete -c http -l sorted -d 'Re-enables all sorting options while formatting output'
complete -c http -l response-charset -x -d 'Override the response encoding'
complete -c http -l response-mime -xa "(__fish_httpie_mime_types)" -d 'Override the response mime type for coloring and formatting'
complete -c http -l format-options -x -d 'Controls output formatting'
# Output Options
complete -c http -s p -l print -xa "(__fish_httpie_print_args)" -d 'String specifying what the output should contain'
complete -c http -s h -l headers -d 'Print only the response headers'
complete -c http -s m -l meta -d 'Print only the response metadata'
complete -c http -s b -l body -d 'Print only the response body'
complete -c http -s v -l verbose -d 'Print the whole request as well as the response'
complete -c http -l all -d 'Show any intermediary requests/responses'
complete -c http -s S -l stream -d 'Always stream the response body by line'
complete -c http -s o -l output -F -d 'Save output to FILE'
complete -c http -s d -l download -d 'Download a file'
complete -c http -s c -l continue -d 'Resume an interrupted download'
complete -c http -s q -l quiet -d 'Do not print to stdout or stderr'
# Sessions
complete -c http -l session -F -d 'Create, or reuse and update a session'
complete -c http -l session-read-only -F -d 'Create or read a session without updating it'
# Authentication
complete -c http -s a -l auth -x -d 'Username and password for authentication'
complete -c http -s A -l auth-type -xa "(__fish_httpie_auth_types)" -d 'The authentication mechanism to be used'
complete -c http -l ignore-netrc -d 'Ignore credentials from .netrc'
# Network
complete -c http -l offline -d 'Build the request and print it but don\'t actually send it'
complete -c http -l proxy -x -d 'String mapping protocol to the URL of the proxy'
complete -c http -s F -l follow -d 'Follow 30x Location redirects'
complete -c http -l max-redirects -x -d 'Set maximum number of redirects'
complete -c http -l max-headers -x -d 'Maximum number of response headers to be read before giving up'
complete -c http -l timeout -x -d 'Connection timeout in seconds'
complete -c http -l check-status -d 'Error with non-200 HTTP status code'
complete -c http -l path-as-is -d 'Bypass dot segment URL squashing'
complete -c http -l chunked -d 'Enable streaming via chunked transfer encoding'
# SSL
complete -c http -l verify -xa "(__fish_http_verify_options)" -d 'Enable/disable cert verification'
complete -c http -l ssl -x -d 'Desired protocol version to use'
complete -c http -l ciphers -x -d 'String in the OpenSSL cipher list format'
complete -c http -l cert -F -d 'Client side SSL certificate'
complete -c http -l cert-key -F -d 'Private key to use with SSL'
complete -c http -l cert-key-pass -x -d 'Passphrase for the given private key'
# Troubleshooting
complete -c http -s I -l ignore-stdin -d 'Do not attempt to read stdin'
complete -c http -l help -d 'Show help'
complete -c http -l manual -d 'Show the full manual'
complete -c http -l version -d 'Show version'
complete -c http -l traceback -d 'Prints exception traceback should one occur'
complete -c http -l default-scheme -x -d 'The default scheme to use'
complete -c http -l debug -d 'Show debugging output'

View File

View File

@ -0,0 +1,83 @@
from enum import Enum
from functools import singledispatch
from completion_flow import (
And,
Check,
Condition,
If,
Node,
Not,
Suggest,
Suggestion,
Variable,
generate_flow,
)
class BashVariable(str, Enum):
CURRENT = 'COMP_CWORD'
NORMARG = 'NORMARG'
CURRENT_WORD = 'cur_word'
PREDECESSOR = 'prev_word'
METHODS = 'METHODS'
SUGGESTION_TO_FUNCTION = {
Suggestion.METHOD: '_http_complete_methods',
Suggestion.URL: '_http_complete_url',
Suggestion.REQUEST_ITEM: '_httpie_complete_request_item',
}
@singledispatch
def compile_bash(node: Node) -> ...:
raise NotImplementedError(f'{type(node)} is not supported')
@compile_bash.register(If)
def compile_if(node: If) -> str:
check = compile_bash(node.check)
action = compile_bash(node.action)
return f'if {check}; then\n {action}\nfi'
@compile_bash.register(Check)
def compile_check(node: Check) -> str:
args = [
BashVariable(arg.name) if isinstance(arg, Variable) else arg
for arg in node.args
]
if node.condition is Condition.POSITION_EQ:
return f'(( {BashVariable.CURRENT} == {BashVariable.NORMARG} + {args[0]} ))'
elif node.condition is Condition.POSITION_GE:
return f'(( {BashVariable.CURRENT} >= {BashVariable.NORMARG} + {args[0]} ))'
elif node.condition is Condition.CONTAINS_PREDECESSOR:
parts = [
'[[ ',
'" ${',
BashVariable.METHODS,
'[*]} " =~ " ${',
BashVariable.PREDECESSOR,
'} " ]]',
]
return ''.join(parts)
@compile_bash.register(And)
def compile_and(node: And) -> str:
return ' && '.join(compile_bash(check) for check in node.checks)
@compile_bash.register(Not)
def compile_not(node: Not) -> str:
return f'! {compile_bash(node.check)}'
@compile_bash.register(Suggest)
def compile_suggest(node: Suggest) -> str:
return (
SUGGESTION_TO_FUNCTION[node.suggestion]
+ f' "${BashVariable.CURRENT_WORD}"'
)

View File

@ -0,0 +1,116 @@
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import Iterator, List
class Condition(Enum):
# $N = check.arguments[N]
# $words = a list of splitted arguments on the completion
# current = index in the $words
# Check whether the $words[current][0] matches the $1
STARTSWITH = auto()
# Check whether the $1 contains the $words[current-1]
CONTAINS_PREDECESSOR = auto()
# Check whether current == $1
POSITION_EQ = auto()
# Check whether current >= $1
POSITION_GE = auto()
class Suggestion(Enum):
OPTION = auto()
METHOD = auto()
URL = auto()
REQUEST_ITEM = auto()
class Variable(Enum):
METHODS = auto()
class Node:
...
@dataclass
class Check(Node):
condition: Condition
args: List[str] = field(default_factory=list)
@dataclass
class Suggest(Node):
suggestion: Suggestion
@dataclass
class If(Node):
check: Node
action: Node
@dataclass
class And(Node):
checks: List[Node]
def __init__(self, *checks) -> None:
self.checks = checks
@dataclass
class Not(Node):
check: Node
def generate_flow() -> Iterator[Node]:
# yield from suggest_option()
yield from suggest_method()
yield from suggest_url()
yield from suggest_request_items()
def suggest_option():
yield If(
Check(Condition.STARTSWITH, args=['-']),
action=Suggest(Suggestion.OPTION),
)
def suggest_method():
yield If(
Check(Condition.POSITION_EQ, args=[0]),
action=Suggest(Suggestion.METHOD),
)
def suggest_url():
yield If(
Check(Condition.POSITION_EQ, args=[0]), action=Suggest(Suggestion.URL)
)
yield If(
And(
Check(Condition.POSITION_EQ, args=[1]),
Check(Condition.CONTAINS_PREDECESSOR, args=[Variable.METHODS]),
),
action=Suggest(Suggestion.URL),
)
def suggest_request_items():
yield If(
Check(Condition.POSITION_GE, args=[2]),
action=Suggest(Suggestion.REQUEST_ITEM),
)
yield If(
And(
Check(Condition.POSITION_GE, args=[1]),
Not(
Check(Condition.CONTAINS_PREDECESSOR, args=[Variable.METHODS])
),
),
action=Suggest(Suggestion.REQUEST_ITEM),
)

View File

@ -0,0 +1,231 @@
import functools
import string
import textwrap
from atexit import register
from pathlib import Path
from typing import Any, Callable, Dict, TypeVar
from completion_flow import generate_flow
from jinja2 import Template
from httpie.cli.constants import SEPARATOR_FILE_UPLOAD
from httpie.cli.definition import options
from httpie.cli.options import Argument, ParserSpec
T = TypeVar('T')
EXTRAS_DIR = Path(__file__).parent.parent.parent
COMPLETION_DIR = EXTRAS_DIR / 'completion'
TEMPLATES_DIR = COMPLETION_DIR / 'templates'
COMPLETION_TEMPLATE_BASE = TEMPLATES_DIR / 'completion'
COMPLETION_SCRIPT_BASE = COMPLETION_DIR / 'completion'
COMMON_HTTP_METHODS = [
'GET',
'POST',
'PUT',
'DELETE',
'HEAD',
'OPTIONS',
'PATCH',
'TRACE',
'CONNECT',
]
COMPLETERS = {}
def use_template(shell_type):
def decorator(func):
@functools.wraps(func)
def wrapper(spec):
template_file = COMPLETION_TEMPLATE_BASE.with_suffix(
f'.{shell_type}.j2'
)
compiletion_script_file = COMPLETION_SCRIPT_BASE.with_suffix(
f'.{shell_type}'
)
jinja_template = Template(template_file.read_text())
jinja_template.globals.update(prepare_objects(spec))
extra_variables = func(spec)
compiletion_script_file.write_text(
jinja_template.render(**extra_variables)
)
COMPLETERS[shell_type] = wrapper
return wrapper
return decorator
BASE_FUNCTIONS = {}
def prepare_objects(spec: ParserSpec) -> Dict[str, Any]:
global_objects = {
**BASE_FUNCTIONS,
}
global_objects['request_items'] = find_argument_by_target_name(
spec, 'REQUEST_ITEM'
)
global_objects['arguments'] = [
argument
for group in spec.groups
for argument in group.arguments
if not argument.is_hidden
if not argument.is_positional
]
global_objects['methods'] = COMMON_HTTP_METHODS
global_objects['generate_flow'] = generate_flow
return global_objects
def register_function(func: T) -> T:
BASE_FUNCTIONS[func.__name__] = func
return func
@register_function
def is_file_based_operator(operator: str) -> bool:
return operator in {SEPARATOR_FILE_UPLOAD}
def escape_zsh(text: str) -> str:
return text.replace(':', '\\:')
def serialize_argument_to_zsh(argument):
# The argument format is the following:
# $prefix'$alias$has_value[$short_desc]:$metavar$:($choice_1 $choice_2)'
prefix = ''
declaration = []
has_choices = 'choices' in argument.configuration
# The format for the argument declaration canges depending on the
# the number of aliases. For a single $alias, we'll embed it directly
# in the declaration string, but for multiple of them, we'll use a
# $prefix.
if len(argument.aliases) > 1:
prefix = '{' + ','.join(argument.aliases) + '}'
else:
declaration.append(argument.aliases[0])
if not argument.is_flag:
declaration.append('=')
declaration.append('[' + argument.short_help + ']')
if 'metavar' in argument.configuration:
metavar = argument.metavar
elif has_choices:
# Choices always require a metavar, so even if we don't have one
# we can generate it from the argument aliases.
metavar = (
max(argument.aliases, key=len)
.lstrip('-')
.replace('-', '_')
.upper()
)
else:
metavar = None
if metavar:
# Strip out any whitespace, and escape any characters that would
# conflict with the shell.
metavar = escape_zsh(metavar.strip(' '))
declaration.append(f':{metavar}:')
if has_choices:
declaration.append('(' + ' '.join(argument.choices) + ')')
return prefix + f"'{''.join(declaration)}'"
def serialize_argument_to_fish(argument):
# The argument format is defined here
# <https://fishshell.com/docs/current/completions.html>
declaration = [
'complete',
'-c',
'http',
]
try:
short_form, long_form = argument.aliases
except ValueError:
short_form = None
(long_form,) = argument.aliases
if short_form:
declaration.append('-s')
declaration.append(short_form)
declaration.append('-l')
declaration.append(long_form)
if 'choices' in argument.configuration:
declaration.append('-xa')
declaration.append('"' + ' '.join(argument.choices) + '"')
elif 'lazy_choices' in argument.configuration:
declaration.append('-x')
declaration.append('-d')
declaration.append("'" + argument.short_help.replace("'", r"\'") + "'")
return '\t'.join(declaration)
def find_argument_by_target_name(spec: ParserSpec, name: str) -> Argument:
for group in spec.groups:
for argument in group.arguments:
if argument.aliases:
targets = argument.aliases
else:
targets = [argument.metavar]
if name in targets:
return argument
raise ValueError(f'Could not find argument with name {name}')
@use_template('zsh')
def zsh_completer(spec: ParserSpec) -> Dict[str, Any]:
from zsh import compile_zsh
return {
'escape_zsh': escape_zsh,
'serialize_argument_to_zsh': serialize_argument_to_zsh,
'compile_zsh': compile_zsh,
}
@use_template('fish')
def fish_completer(spec: ParserSpec) -> Dict[str, Any]:
return {
'serialize_argument_to_fish': serialize_argument_to_fish,
}
@use_template('bash')
def fish_completer(spec: ParserSpec) -> Dict[str, Any]:
from bash import compile_bash
return {
'compile_bash': compile_bash,
}
def main():
for shell_type, completer in COMPLETERS.items():
print(f'Generating {shell_type} completer.')
completer(options)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,84 @@
from enum import Enum
from functools import singledispatch
from completion_flow import (
And,
Check,
Condition,
If,
Node,
Not,
Suggest,
Suggestion,
Variable,
)
class ZSHVariable(str, Enum):
CURRENT = 'CURRENT'
NORMARG = 'NORMARG'
PREDECESSOR = 'predecessor'
METHODS = 'METHODS'
SUGGESTION_TO_FUNCTION = {
Suggestion.METHOD: '_httpie_method',
Suggestion.URL: '_httpie_url',
Suggestion.REQUEST_ITEM: '_httpie_request_item',
}
@singledispatch
def compile_zsh(node: Node) -> ...:
raise NotImplementedError(f'{type(node)} is not supported')
@compile_zsh.register(If)
def compile_if(node: If) -> str:
check = compile_zsh(node.check)
action = compile_zsh(node.action)
return f'if {check}; then\n {action} && ret=0\nfi'
@compile_zsh.register(Check)
def compile_check(node: Check) -> str:
args = [
ZSHVariable(arg.name) if isinstance(arg, Variable) else arg
for arg in node.args
]
if node.condition is Condition.POSITION_EQ:
return (
f'(( {ZSHVariable.CURRENT} == {ZSHVariable.NORMARG} + {args[0]} ))'
)
elif node.condition is Condition.POSITION_GE:
return (
f'(( {ZSHVariable.CURRENT} >= {ZSHVariable.NORMARG} + {args[0]} ))'
)
elif node.condition is Condition.CONTAINS_PREDECESSOR:
parts = [
'[[ ${',
args[0],
'[(ie)$',
ZSHVariable.PREDECESSOR,
']}',
' -le ${#',
args[0],
'} ]]',
]
return ''.join(parts)
@compile_zsh.register(And)
def compile_and(node: And) -> str:
return ' && '.join(compile_zsh(check) for check in node.checks)
@compile_zsh.register(Not)
def compile_not(node: Not) -> str:
return f'! {compile_zsh(node.check)}'
@compile_zsh.register(Suggest)
def compile_suggest(node: Suggest) -> str:
return SUGGESTION_TO_FUNCTION[node.suggestion]

View File

@ -13,6 +13,9 @@ from httpie.cli.constants import (BASE_OUTPUT_OPTIONS, DEFAULT_FORMAT_OPTIONS,
OUTPUT_OPTIONS_DEFAULT, PRETTY_MAP,
PRETTY_STDOUT_TTY_ONLY,
SEPARATOR_GROUP_ALL_ITEMS, SEPARATOR_PROXY,
SEPARATOR_HEADER, SEPARATOR_QUERY_PARAM,
SEPARATOR_DATA_STRING, SEPARATOR_DATA_RAW_JSON,
SEPARATOR_FILE_UPLOAD,
SORTED_FORMAT_OPTIONS_STRING,
UNSORTED_FORMAT_OPTIONS_STRING, RequestType)
from httpie.cli.options import ParserSpec, Qualifiers, to_argparse
@ -91,11 +94,11 @@ positional_arguments.add_argument(
'data, files, and URL parameters.'
),
nested_options=[
('HTTP Headers', 'Name:Value', 'Arbitrary HTTP header, e.g X-API-Token:123'),
('URL Parameters', 'name==value', 'Querystring parameter to the URL, e.g limit==50'),
('Data Fields', 'field=value', 'Data fields to be serialized as JSON (default) or Form Data (with --form)'),
('Raw JSON Fields', 'field:=json', 'Data field for real JSON types.'),
('File upload Fields', 'field@/dir/file', 'Path field for uploading a file.'),
('HTTP Headers', 'Name:Value', SEPARATOR_HEADER, 'Arbitrary HTTP header, e.g X-API-Token:123'),
('URL Parameters', 'name==value', SEPARATOR_QUERY_PARAM, 'Querystring parameter to the URL, e.g limit==50'),
('Data Fields', 'field=value', SEPARATOR_DATA_STRING, 'Data fields to be serialized as JSON (default) or Form Data (with --form)'),
('Raw JSON Fields', 'field:=json', SEPARATOR_DATA_RAW_JSON, 'Data field for real JSON types.'),
('File upload Fields', 'field@/dir/file', SEPARATOR_FILE_UPLOAD, 'Path field for uploading a file.'),
],
help=r"""
Optional key-value pairs to be included in the request. The separator used

View File

@ -172,6 +172,11 @@ class Argument(typing.NamedTuple):
def is_hidden(self):
return self.configuration.get('help') is Qualifiers.SUPPRESS
@property
def is_flag(self):
action = getattr(self, 'action', None)
return action in ARGPARSE_FLAG_ACTIONS
def __getattr__(self, attribute_name):
if attribute_name in self.configuration:
return self.configuration[attribute_name]
@ -188,6 +193,17 @@ ARGPARSE_QUALIFIER_MAP = {
Qualifiers.ONE_OR_MORE: argparse.ONE_OR_MORE
}
ARGPARSE_IGNORE_KEYS = ('short_help', 'nested_options')
ARGPARSE_FLAG_ACTIONS = [
"store_true",
"store_false",
"count",
"version",
"help",
"debug",
"manual",
"append_const",
"store_const"
]
def to_argparse(

View File

@ -193,7 +193,7 @@ def to_help_message(
value,
dec,
)
for key, value, dec in argument.nested_options
for key, value, _, dec in argument.nested_options
]
)