Skip to content

Commit

Permalink
Apply shfmt on scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
reegnz committed Dec 15, 2023
1 parent 1b13ab3 commit cf16e78
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
22 changes: 11 additions & 11 deletions bin/jq-repl-preview
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ trap 'rm -f "$out"' EXIT

# for colorizing stderr
red() {
while read -r line; do
printf "\033[31m%s\033[0m\n" "$line"
done
while read -r line; do
printf "\033[31m%s\033[0m\n" "$line"
done
}

# prints preview query result to stdout, colorizes stderr to red
do_query() {
# JQ_REPL_ARGS has to be unquoted so it's passed as cli args to jq so
# word splitting is desirable here
# shellcheck disable=SC2086
$JQ_REPL_JQ --color-output ${JQ_REPL_ARGS:-} "$query" "$input_file"
# JQ_REPL_ARGS has to be unquoted so it's passed as cli args to jq so
# word splitting is desirable here
# shellcheck disable=SC2086
$JQ_REPL_JQ --color-output ${JQ_REPL_ARGS:-} "$query" "$input_file"
} 2> >(red)

echo "Last successful jq expression: $query" | red >"$out"
echo "Last success output:" | red >>"$out"
if do_query | tee -a "$out"; then
# save successful query plus the result
mv "$out" "$last_success_file"
# save successful query plus the result
mv "$out" "$last_success_file"
else
# print previously saved successful query
cat "$last_success_file"
# print previously saved successful query
cat "$last_success_file"
fi
42 changes: 21 additions & 21 deletions jq.plugin.zsh
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
if [[ -o zle ]]; then

__lbuffer_strip_trailing_pipe() {
__lbuffer_strip_trailing_pipe() {
# Strip a trailing pipe and its surrounding whitespace.
sed -E 's/[[:space:]]*\|[[:space:]]*$//' <<<"$LBUFFER"
}
}

__get_query() {
__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 $?
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 $?
# shellcheck disable=SC2086
jq-repl -- $(__lbuffer_strip_trailing_pipe)
return $?
fi
}
}

jq-complete() {
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'"
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
zle -N jq-complete
# bind `alt + j` to jq-complete
bindkey '\ej' jq-complete
fi

export PATH=$PATH:${0:A:h}/bin

0 comments on commit cf16e78

Please sign in to comment.