Skip to content

Commit

Permalink
lose arrow dependencies and add yank support for GNU/Linux (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: windowsrefund <mtf8>
  • Loading branch information
windowsrefund authored Aug 23, 2024
1 parent 48befbc commit ded47a1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
63 changes: 33 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# jq zsh plugin

Interactively build [jq](https://stedolan.github.io/jq/) expressions
([gojq](https://github.com/itchyny/gojq) is also supported).
Interactively build [jq](https://stedolan.github.io/jq/) expressions ([gojq](https://github.com/itchyny/gojq)
is also supported).

This zsh plugin gives you jq superpowers!

[Fish](https://fishshell.com/) users: you can use this friendly fork: [rmartine-ias/jq-fish-plugin](https://github.com/rmartine-ias/jq-fish-plugin)
[Fish](https://fishshell.com/) users: you can use this friendly fork:
[rmartine-ias/jq-fish-plugin](https://github.com/rmartine-ias/jq-fish-plugin)

## Table of contents

Expand All @@ -27,15 +28,16 @@ This zsh plugin gives you jq superpowers!
## Installation

Besides [jq](https://stedolan.github.io/jq/), this plugin also requires
[fzf](https://github.com/junegunn/fzf#installation) ([a recent version](https://github.com/reegnz/jq-zsh-plugin/issues/19)) to be installed and available on your
[fzf](https://github.com/junegunn/fzf#installation)
([a recent version](https://github.com/reegnz/jq-zsh-plugin/issues/19)) to be installed and available on your
PATH.

The following installation methods are proven to work:

* [Oh My Zsh](#oh-my-zsh)
* [zplug](#zplug)
* [Antigen](#antigen)
* [Zgen](#zgen)
- [Oh My Zsh](#oh-my-zsh)
- [zplug](#zplug)
- [Antigen](#antigen)
- [Zgen](#zgen)

### [Oh My Zsh](https://ohmyz.sh)

Expand Down Expand Up @@ -78,7 +80,8 @@ zgen load reegnz/jq-zsh-plugin

- type out a command that you expect to produce json on its standard output
- press alt + j
- start typing jq expression and watch it being evaluated in real time (like a true [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)!)
- start typing jq expression and watch it being evaluated in real time (like a true
[REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)!)
- use up/down and hit tab to select one of the suggestions
- or type out a jq query on your own
- press enter, and the jq expression is appended to your initial command!
Expand All @@ -89,22 +92,24 @@ Bringing up the jq query builder for a shell command: `alt + j`

During interactive querying, the following shortcuts can be used:

| Shortcut | Effect |
| ------ | -------- |
| `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 |
| Shortcut | Effect |
| ------------------------ | ------------------------------------------------ |
| `ctrl-k` or `up` | Navigate path queries |
| `ctrl-j` or `down` | Navigate path queries |
| `tab` | Select path query |
| `ctrl-p` or `shift-up` | Scroll up |
| `ctrl-n` or `shift-down` | Scroll down |
| `ctrl-alt-p` | Scroll up half page |
| `ctrl-alt-n` | Scroll down half page |
| `alt-up` | Scroll up full page |
| `alt-down` | Scroll down full page |
| `ctrl-r` | Reload input |
| `ctrl-y` | Yank selected path to clipboard (GNU/Linux only) |

## gojq support

If you want to use an alternative `jq` implementation, like
[gojq](https://github.com/itchyny/gojq) then you can override the default jq
command used by the plugin. Set the following environment variable:
If you want to use an alternative `jq` implementation, like [gojq](https://github.com/itchyny/gojq) then you
can override the default jq command used by the plugin. Set the following environment variable:

```sh
JQ_REPL_JQ=gojq
Expand All @@ -114,13 +119,11 @@ JQ_REPL_JQ=gojq

The project consists of the following components:

- a `jq.plugin.zsh` providing a [user-defined zsh line-editor
widget](https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html),
- a `jq.plugin.zsh` providing a
[user-defined zsh line-editor widget](https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html),
utilizing the `jq-repl` command
- a `jq-repl` command to interactively build jq expressions, utilizing fzf for
its UI
- a `jq-paths` command to get all valid jq paths in the provided JSON document,
used for suggesting paths.
- a `jq-repl` command to interactively build jq expressions, utilizing fzf for its UI
- a `jq-paths` command to get all valid jq paths in the provided JSON document, used for suggesting paths.

## Troubleshooting

Expand All @@ -145,8 +148,8 @@ bindkey `^j` jq-complete

### Disable expanding shell aliases

The plugin automatically expands shell aliases in a command before passing it
to `jq-repl`. To disable, put the following line into your `.zshrc`:
The plugin automatically expands shell aliases in a command before passing it to `jq-repl`. To disable, put
the following line into your `.zshrc`:

```sh
JQ_ZSH_PLUGIN_EXPAND_ALIASES=0
Expand Down
11 changes: 11 additions & 0 deletions bin/jq-repl
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ if [ -z "$fzf_notation" ]; then
fzf_notation="#"
fi

# default ignore
FZF_COPY_TO_CLIPBOARD="execute-silent(ignore)"

# other platforms can be supported here as needed
command -v xclip > /dev/null && \
FZF_COPY_TO_CLIPBOARD="execute-silent(echo -n {} | xclip -in -sel clip)+abort"

eval "$FZF_JQ_REPL_COMMAND" |
fzf \
--preview "jq-repl-preview {q} $input $output" \
--preview-window="down:90%" \
--height="99%" \
--query="." \
--bind "tab:replace-query,return:print-query" \
--bind "ctrl-p:preview-up,ctrl-n:preview-down" \
--bind "ctrl-alt-p:preview-half-page-up,ctrl-alt-n:preview-half-page-down" \
--bind "shift-up:preview-up,shift-down:preview-down" \
--bind "alt-up:preview-page-up,alt-down:preview-page-down" \
--bind "ctrl-y:$FZF_COPY_TO_CLIPBOARD" \
--bind "ctrl-r:reload${fzf_notation}${FZF_JQ_REPL_COMMAND}${fzf_notation}+refresh-preview"

0 comments on commit ded47a1

Please sign in to comment.