-
Notifications
You must be signed in to change notification settings - Fork 17
/
jq.plugin.zsh
executable file
·41 lines (36 loc) · 1.08 KB
/
jq.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
if [[ -o zle ]]; then
__lbuffer_strip_trailing_pipe() {
# Strip a trailing pipe and its surrounding whitespace.
sed -E 's/[[:space:]]*\|[[:space:]]*$//' <<<"$LBUFFER"
}
__get_query() {
if [ "${JQ_ZSH_PLUGIN_EXPAND_ALIASES:-1}" -eq 1 ]; then
unset 'functions[_jq-plugin-expand]'
functions[_jq-plugin-expand]=$(__lbuffer_strip_trailing_pipe)
(($+functions[_jq-plugin-expand])) && COMMAND=${functions[_jq-plugin-expand]#$'\t'}
# shellcheck disable=SC2086
jq-repl -- ${COMMAND}
return $?
else
# shellcheck disable=SC2086
jq-repl -- $(__lbuffer_strip_trailing_pipe)
return $?
fi
}
jq-complete() {
local query
query="$(__get_query)"
local ret=$?
if [ -n "$query" ]; then
LBUFFER="$(__lbuffer_strip_trailing_pipe) | ${JQ_REPL_JQ:-jq}"
[[ -z "$JQ_REPL_ARGS" ]] || LBUFFER="${LBUFFER} ${JQ_REPL_ARGS}"
LBUFFER="${LBUFFER} '$query'"
fi
zle reset-prompt
return $ret
}
zle -N jq-complete
# bind `alt + j` to jq-complete
bindkey '\ej' jq-complete
fi
export PATH=$PATH:${0:A:h}/bin