#!/usr/bin/env bash
#
# jq-repl - implementing a jq REPL with fzf
# dependencies: fzf | jq
#
# ---
# see: https://gist.github.com/reegnz/b9e40993d410b75c2d866441add2cb55#file-jq-repl
# see: https://github.com/reegnz/jq-zsh-plugin

set -eu

if [ -z "$1" ] || [ "$1" = "-" ]; then
  input=$(mktemp)
  trap 'rm -f "$input"' EXIT
  cat /dev/stdin >"${input}"
else
  input="$1"
fi

echo '' \
  | fzf --disabled \
    --preview-window='up:90%' \
    --print-query \
    --preview="jq --color-output --raw-output {q} ${input}"
