srid / neuron

Future-proof note-taking and publishing based on Zettelkasten (superseded by Emanote: https://github.com/srid/emanote)
https://neuron.zettel.page
GNU Affero General Public License v3.0
1.52k stars 150 forks source link

Introduce JSONL format for query #612

Closed psiska closed 3 years ago

psiska commented 3 years ago

To support querying by tools which expects whole information on one line. E.g. Neovim telescope plugin where I need to query neuron DB returns result split on multiple lines. Which is nice for human, however impose burden on program interaction.

This PR introduces the --jsonl flag to 'query' sub-command. If this flag is active one entry will be printed at each line. Which is super convenient format for command line processing.

Example

neuron/doc $ /nix/store/i5lagbbinqf4i5b77liz3qp5spih98nd-neuron-1.9.27.2/bin/neuron query  --id index
   Plugins enabled: dirtree, links, tags, uptree
   Loading directory tree (49 .md files) ...
   Building graph (49 notes) ...
[
    {
        "ID": "index",
        "Meta": {
            "dirtree": {
                "display": false
            },
            "tags": []
        },
        "Path": "./index.md",
        "Slug": "index",
        "Title": "Neuron Zettelkasten"
    }
]

After

neuron/doc $ /nix/store/i5lagbbinqf4i5b77liz3qp5spih98nd-neuron-1.9.27.2/bin/neuron query --jsonl --id index
   Plugins enabled: dirtree, links, tags, uptree
   Loading directory tree (49 .md files) ...
   Building graph (49 notes) ...
{"Path":"./index.md","Slug":"index","ID":"index","Meta":{"dirtree":{"display":false},"tags":[]},"Title":"Neuron Zettelkasten"}
srid commented 3 years ago

Hey, thanks for the PR. This seems like an useful feature in general (even though I'm not familiar with the telescope plugin), however I'm not sure if it is worth baking into neuron itself? Especially as you can achieve this using a tool like jq

❯ neuron query 2> /dev/null | jq '.[]'  | jq -c | tail -3
{"ID":"ghcid","Meta":{"slug":"ghcid","tags":[]},"Path":"./ghcid.md","Slug":"ghcid","Title":"ghcid"}
{"ID":"index","Meta":{"tags":[]},"Path":"./index.md","Slug":"index","Title":"Sridhar Ratnakumar"}
{"ID":"ka Project","Meta":{"slug":"ka","tags":[]},"Path":"./ka Project.md","Slug":"ka","Title":"ka Project"}

❯ neuron query --id index 2> /dev/null | jq -c
{"ID":"index","Meta":{"tags":[]},"Path":"./index.md","Slug":"index","Title":"Sridhar Ratnakumar"}
psiska commented 3 years ago

I can see your point, in keeping neuron as small as possible (correct me if I am wrong, but I have an impression this is a goal).

You are right that I can achieve that with some other external tools like 'jq'. No problem in here. On the other hand this change is quite small, contained and does not add other dependencies.

The 'jq' path requires pipe-ing output from one command to another which implies either shell or handling stream by myself (telescope is in lua so no shell is involved). Another factor is that it requires to have 'jq' installed.

So my point is that this change make it easier for me and hopefully for other who will wish to integrate the neuron into their pipelines. ( I am thinking about something like pandoc but for information manipulation)

If it helps to get this feature in I can redesign the flag to be more future-proof.

srid commented 3 years ago

Okay, then let's consider these:

srid commented 3 years ago

By the way, could you share your neovim telescope config? I'm curious about that plugin.

psiska commented 3 years ago

So I got back to the patch. Hopefully I got your comments rights. Let's see :)

When it comes to my telescope config. There is nothing special atm. Just binding of default pickers to shortcuts. What I am trying to achieve is to integrate neuron into it (even if I am aware of neuron2.vim).

srid commented 3 years ago

@psiska Looks perfect! Just one last change: could increment the version (to 1.9.30.0) in neuron.cabal and add an entry (referencing this PR) in CHANGELOG.md?

(Make sure to merge with master if you haven't already)

psiska commented 3 years ago

I've added the entry in the Changelog and rebase against upstream/master.