Skip to content

Commit

Permalink
Add reload feature
Browse files Browse the repository at this point in the history
Until now the REPL executed the shell command once, and you could not
reload the json.

Now if you hit Ctrl+R the shell command is re-run and you can see the
preview with the updated JSON input.
  • Loading branch information
reegnz committed Sep 13, 2021
1 parent 98650d6 commit 59b419c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ on your PATH.

The project consists of two components:
- a `jq-repl` command
- a `jq-paths` command
- a `jq.plugin.zsh` providing line-editor feature using `jq-repl`


Expand Down Expand Up @@ -55,13 +56,14 @@ During interactive querying, the following shortcuts can be used:

| Shortcut | Effect |
| ------ | -------- |
| up | Navigate path queries |
| down | Navigate path queries |
| tab | Select path query |
| `up` | Navigate path queries |
| `down` | Navigate path queries |
| `tab` | Select path query |
| `shift + up` | Scroll up |
| `shift + down` | Scroll down |
| `alt + up` | Scroll up full page |
| `alt + down` | Scroll down full page |
| `ctrl+r` | Reload input |

## Demos

Expand Down
7 changes: 7 additions & 0 deletions bin/jq-paths
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env jq -r -f
# path logic inspired by https://github.com/stedolan/jq/issues/243
[
path(..) |
map(select(type=="string") // "[]") |
join(".") | split(".[]") | join("[]")
] | map("." + .) | unique | .[]
39 changes: 25 additions & 14 deletions bin/jq-repl
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
#!/bin/sh
if [ -z "$1" ] || [ "$1" = "-" ]; then
input=$(mktemp)
trap 'rm -f "$input"' EXIT
cat /dev/stdin > "$input"
#
# if 1st arg is '-' read from stdin
# if 1st arg is '--' take the command after it verbatim and execute it to get the input
# if 1st arg is anything else, treat it as a file
set -eu

if [ -n "${1:-}" ] && [ "$1" != "-" ] && [ "$1" != "--" ]; then
input="$1"
else
input=$(mktemp)
trap 'rm -f "$input"' EXIT
fi

if [ -z "${1:-}" ] || [ "$1" = "-" ]; then
cat /dev/stdin >"$input"
fi
if [ "${1:-}" = "--" ]; then
shift
export FZF_JQ_REPL_COMMAND="$* > $input; jq-paths <$input"
else
input="$1"
export FZF_JQ_REPL_COMMAND="jq-paths < $input"
fi

# path logic inspired by https://github.com/stedolan/jq/issues/243
<"$input" jq -r '[
path(..) |
map(select(type=="string") // "[]") |
join(".") | split(".[]") | join("[]")
] | map("." + .) | unique | .[]' |
fzf \
--preview "jq --color-output $JQ_REPL_ARGS {q} \"$input\"" \
eval "$FZF_JQ_REPL_COMMAND" |
fzf \
--preview "jq --color-output ${JQ_REPL_ARGS:-} {q} \"$input\"" \
--preview-window="down:90%" \
--height="99%" \
--query="." \
--bind "tab:replace-query,return:print-query" \
--bind "alt-up:preview-page-up,alt-down:preview-page-down"
--bind "alt-up:preview-page-up,alt-down:preview-page-down" \
--bind "ctrl-r:reload($FZF_JQ_REPL_COMMAND)+refresh-preview"
2 changes: 1 addition & 1 deletion jq.plugin.zsh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if [[ -o zle ]]; then

__get_query() {
eval "$LBUFFER" | jq-repl
jq-repl -- ${LBUFFER}
return $?
}

Expand Down

0 comments on commit 59b419c

Please sign in to comment.