Skip to content

Commit

Permalink
feat: counsel-jq works equally for JSON and YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
munen committed Mar 29, 2021
1 parent f9468f0 commit 8cadd2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
35 changes: 22 additions & 13 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
* counsel-jq

** Live queries against JSON and YAML data

[[https://melpa.org/#/counsel-jq][file:https://melpa.org/packages/counsel-jq-badge.svg]]

TL;DR: If you're a fan of [[https://stedolan.github.io/jq/][jq]] and you're using Emacs
TL;DR: If you're a fan of [[https://stedolan.github.io/jq/][jq]] or [[https://github.com/mikefarah/yq][yq]] and you're using Emacs
[[https://github.com/abo-abo/swiper][Ivy/Swiper/Counsel]], then this package is for you.

Longer version: If you are working with complex nested JSON
structures, you are probably familiar with [[https://stedolan.github.io/jq/][jq]] which is like sed for
JSON data and great at what it does. However, being a command-line
tool like sed, the feedback for writing queries and seeing their
results is a discrete process and not live.
Longer version: If you are working with complex nested JSON (or YAML)
structures, you are probably familiar with [[https://stedolan.github.io/jq/][jq]] (or [[https://github.com/mikefarah/yq][yq]]) which is like
sed for JSON data and great at what it does. However, being a
command-line tool like sed, the feedback for writing queries and
seeing their results is a discrete process and not live.

#+begin_quote
Cool. That might even be a feature that would draw in new Emacs users!
Expand All @@ -22,20 +24,27 @@ modern completion frameworks like [[https://github.com/abo-abo/swiper][Ivy]], th
inputs, as well.

counsel-jq is a package with which you can quickly test queries and
traverse a complex JSON structure whilst having live feedback. Just
call =M-x counsel-jq= in a buffer containing JSON, then start writing
your =jq= query string and see the output appear live in the message
area. Whenever you're happy, hit =RET= and the results will be
displayed to you in the buffer =*jq-json*=.
traverse a complex JSON and YAML structure whilst having live
feedback. Just call =M-x counsel-jq= in a buffer containing JSON or
YAML, then start writing your =jq= or =yq= query string and see the
output appear live in the message area. Whenever you're happy, hit
=RET= and the results will be displayed to you in the buffer
=*jq-json*=.

Demo:

[[file:images/demo-counsel-jq.gif][./images/demo-counsel-jq.gif]]

** Configuration

By default, the resulting =*jq-json*= buffer will have the major mode
=js-mode=, but that can be customized with the
To define whether you want to use =yq= over =jq= as processing tool,
call =M-x customize= and set =counsel-jq-command= to =yq=.

In the same manner you can define the name of the results buffer by
customizing the =counsel-jq-buffer= variable.

Lastly, by default, the results buffer =*jq-json*= buffer will have
the major mode =js-mode=, but that can be customized with the
=counsel-jq-json-buffer-mode= variable if you prefer =json-mode=,
=rsjx-mode= or any other mode.

Expand Down
6 changes: 3 additions & 3 deletions counsel-jq.el
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
;;; counsel-jq.el --- Live preview of "jq" queries using counsel -*- lexical-binding: t -*-
;;; Version: 1.0.1
;;; Version: 1.1.0
;;; Author: Alain M. Lafon <[email protected])
;;; Package-Requires: ((swiper "0.12.0") (ivy "0.12.0") (emacs "24.1"))
;;; Keywords: convenience, data, matching
Expand All @@ -16,10 +16,10 @@
:require 'counsel-jq
:group 'counsel-jq)

(defvar counsel-jq-command "jq"
(defcustom counsel-jq-command "jq"
"Command for `counsel-jq'.")

(defvar counsel-jq-buffer "*jq-json*"
(defcustom counsel-jq-buffer "*jq-json*"
"Buffer for the `counsel-jq' query results.")

(defun counsel-jq-json (&optional query)
Expand Down

0 comments on commit 8cadd2e

Please sign in to comment.