From 7128cd8d0dcb7ceb7e99e8058ed81f75048bb39a Mon Sep 17 00:00:00 2001 From: Noah Masur <7386960+nmasur@users.noreply.github.com> Date: Sat, 16 Nov 2024 22:59:00 -0700 Subject: [PATCH] clean up jqr --- modules/common/shell/bash/scripts/jqr.sh | 23 +++++++++++++++++++++++ modules/common/shell/fish/default.nix | 4 ---- modules/common/shell/fzf.nix | 11 +++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100755 modules/common/shell/bash/scripts/jqr.sh diff --git a/modules/common/shell/bash/scripts/jqr.sh b/modules/common/shell/bash/scripts/jqr.sh new file mode 100755 index 0000000..29a63db --- /dev/null +++ b/modules/common/shell/bash/scripts/jqr.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +# Adapted from: https://gist.github.com/reegnz/b9e40993d410b75c2d866441add2cb55 + +if [[ -z $1 ]] || [[ $1 == "-" ]]; then + input=$(mktemp) + trap 'rm -f $input' EXIT + cat /dev/stdin >"$input" +else + input=$1 +fi + +echo '' | + fzf --phony \ + --height 100% \ + --preview-window='up:80%' \ + --query '.' \ + --print-query \ + --header $'CTRL-O: jq output\nCTRL-Y: copy output\nALT-Y: copy query' \ + --preview "jq --color-output -r {q} $input" \ + --bind "ctrl-o:execute(jq -r {q} $input)+clear-query+accept" \ + --bind "alt-y:execute(echo {q} | pbcopy)" \ + --bind "ctrl-y:execute(jq -r {q} $input | pbcopy)" diff --git a/modules/common/shell/fish/default.nix b/modules/common/shell/fish/default.nix index 029bf2a..1e5e7f8 100644 --- a/modules/common/shell/fish/default.nix +++ b/modules/common/shell/fish/default.nix @@ -59,10 +59,6 @@ description = "Tidy up JSON using jq"; body = "pbpaste | jq '.' | pbcopy"; # Need to fix for non-macOS }; - jqr = { - description = "jq repl for constructing jq syntax"; - body = "echo '' | fzf --query '.' --print-query --preview \"jq {q} < $argv\""; - }; note = { description = "Edit or create a note"; argumentNames = "filename"; diff --git a/modules/common/shell/fzf.nix b/modules/common/shell/fzf.nix index 3cf55ec..1f1d935 100644 --- a/modules/common/shell/fzf.nix +++ b/modules/common/shell/fzf.nix @@ -41,5 +41,16 @@ FZF_CTRL_T_COMMAND = fzfCommand; FZF_DEFAULT_OPTS = "-m --height 50% --border"; }; + + home.packages = [ + (pkgs.writeShellApplication { + name = "jqr"; + runtimeInputs = [ + pkgs.jq + pkgs.fzf + ]; + text = builtins.readFile ./bash/scripts/jqr.sh; + }) + ]; }; }