more refinements

This commit is contained in:
Batuhan Taskaya 2022-05-19 14:56:25 +03:00
parent 5af4acc798
commit 0dce332b16
3 changed files with 126 additions and 133 deletions

View File

@ -2,64 +2,79 @@
# Copyright (c) 2015 Github zsh-users # Copyright (c) 2015 Github zsh-users
# Based on the initial work of http://github.com/zsh-users # Based on the initial work of http://github.com/zsh-users
methods=("GET" "POST" "PUT" "DELETE" "HEAD" "OPTIONS" "PATCH" "TRACE" "CONNECT" ) METHODS=("GET" "POST" "PUT" "DELETE" "HEAD" "OPTIONS" "PATCH" "TRACE" "CONNECT" )
_httpie_params () { _httpie_params () {
local ret=1 expl local ret=1 expl
local first_arg=$words[NORMARG] local current=$words[$CURRENT]
if (( CURRENT == NORMARG )) && [[ $words[NORMARG] != *:* ]]; then if (( CURRENT > NORMARG )); then
# URL local predecessor=$words[(( $CURRENT - 1 ))]
_httpie_urls && ret=0
elif (( CURRENT == NORMARG + 1 )) && [[ ${methods[(ie)$first_arg]} -le ${#methods} ]]; then
# When the first argument is the method, suggest the URL as well.
_httpie_urls && ret=0
elif (( CURRENT > NORMARG )); then
# regular param, if we already have a url
# ignore all prefix stuff
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
ret=0
fi fi
# first arg may be a request method
(( CURRENT == NORMARG )) && if (( CURRENT == NORMARG + 0 )); then
_wanted http_method expl 'Request Method' \ _httpie_method && ret=0
compadd GET POST PUT DELETE HEAD OPTIONS PATCH TRACE CONNECT && 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 )) && ! [[ $current == -* ]]; then
_httpie_request_item && ret=0
fi
if (( CURRENT >= NORMARG + 1 )) && ! [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]] && ! [[ $current == -* ]]; then
_httpie_request_item && ret=0
fi
return $ret return $ret
} }
_httpie_urls() { _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 local ret=1
if ! [[ -prefix [-+.a-z0-9]#:// ]]; then if ! [[ -prefix [-+.a-z0-9]#:// ]]; then
@ -73,26 +88,6 @@ _httpie_urls() {
return $ret return $ret
} }
_httpie_printflags () {
local ret=1
# not sure why this is necessary, but it will complete "-pH" style without it
[[ $IPREFIX == "-p" ]] && IPREFIX+=" "
compset -P '(#b)([a-zA-Z]#)'
local -a flags
[[ $match[1] != *H* ]] && flags+=( "H:request headers" )
[[ $match[1] != *B* ]] && flags+=( "B:request body" )
[[ $match[1] != *h* ]] && flags+=( "h:response headers" )
[[ $match[1] != *b* ]] && flags+=( "b:response body" )
[[ $match[1] != *m* ]] && flags+=( "b:response meta" )
_describe -t printflags "print flags" flags -S '' && ret=0
return $ret
}
integer NORMARG integer NORMARG
_arguments -n -C -s \ _arguments -n -C -s \

View File

@ -2,55 +2,70 @@
# Copyright (c) 2015 Github zsh-users # Copyright (c) 2015 Github zsh-users
# Based on the initial work of http://github.com/zsh-users # Based on the initial work of http://github.com/zsh-users
methods=({% for method in methods -%} "{{ method }}" {% endfor -%}) METHODS=({% for method in methods -%} "{{ method }}" {% endfor -%})
_httpie_params () { _httpie_params () {
local ret=1 expl local ret=1 expl
local first_arg=$words[NORMARG] local current=$words[$CURRENT]
if (( CURRENT == NORMARG )) && [[ $words[NORMARG] != *:* ]]; then if (( CURRENT > NORMARG )); then
# URL local predecessor=$words[(( $CURRENT - 1 ))]
_httpie_urls && ret=0
elif (( CURRENT == NORMARG + 1 )) && [[ ${methods[(ie)$first_arg]} -le ${{ '{#' }}methods} ]]; then
# When the first argument is the method, suggest the URL as well.
_httpie_urls && ret=0
elif (( CURRENT > NORMARG )); then
# regular param, if we already have a url
# ignore all prefix stuff
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
ret=0
fi fi
# first arg may be a request method {% raw %}
(( CURRENT == NORMARG )) && if (( CURRENT == NORMARG + 0 )); then
_wanted http_method expl 'Request Method' \ _httpie_method && ret=0
compadd {% for method in methods -%} {{ method }} {% endfor -%} && 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 )) && ! [[ $current == -* ]]; then
_httpie_request_item && ret=0
fi
if (( CURRENT >= NORMARG + 1 )) && ! [[ ${METHODS[(ie)$predecessor]} -le ${#METHODS} ]] && ! [[ $current == -* ]]; then
_httpie_request_item && ret=0
fi
{% endraw %}
return $ret return $ret
} }
_httpie_urls() { _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 local ret=1
if ! [[ -prefix [-+.a-z0-9]#:// ]]; then if ! [[ -prefix [-+.a-z0-9]#:// ]]; then
@ -64,26 +79,6 @@ _httpie_urls() {
return $ret return $ret
} }
_httpie_printflags () {
local ret=1
# not sure why this is necessary, but it will complete "-pH" style without it
[[ $IPREFIX == "-p" ]] && IPREFIX+=" "
compset -P '(#b)([a-zA-Z]#)'
local -a flags
[[ $match[1] != *H* ]] && flags+=( "H:request headers" )
[[ $match[1] != *B* ]] && flags+=( "B:request body" )
[[ $match[1] != *h* ]] && flags+=( "h:response headers" )
[[ $match[1] != *b* ]] && flags+=( "b:response body" )
[[ $match[1] != *m* ]] && flags+=( "b:response meta" )
_describe -t printflags "print flags" flags -S '' && ret=0
return $ret
}
integer NORMARG integer NORMARG
_arguments -n -C -s \ _arguments -n -C -s \

View File

@ -35,9 +35,9 @@ def compile_zsh(node: Node) -> ...:
@compile_zsh.register(If) @compile_zsh.register(If)
def compile_if(node: If) -> str: def compile_if(node: If) -> str:
check = compile(node.check) check = compile_zsh(node.check)
action = compile(node.action) action = compile_zsh(node.action)
return f'if {check}; then\n {action};\nfi' return f'if {check}; then\n {action} && ret=0\nfi'
@compile_zsh.register(Check) @compile_zsh.register(Check)
@ -71,14 +71,17 @@ def compile_check(node: Check) -> str:
@compile_zsh.register(And) @compile_zsh.register(And)
def compile_and(node: And) -> str: def compile_and(node: And) -> str:
return ' && '.join(compile(check) for check in node.checks) return ' && '.join(compile_zsh(check) for check in node.checks)
@compile_zsh.register(Not) @compile_zsh.register(Not)
def compile_not(node: Not) -> str: def compile_not(node: Not) -> str:
return f'! {compile(node.check)}' return f'! {compile_zsh(node.check)}'
@compile_zsh.register(Suggest) @compile_zsh.register(Suggest)
def compile_suggest(node: Suggest) -> str: def compile_suggest(node: Suggest) -> str:
return SUGGESTION_TO_FUNCTION[node.suggestion] return SUGGESTION_TO_FUNCTION[node.suggestion]
for item in main_flow():
print(compile_zsh(item))