antonmedv / fx

Terminal JSON viewer & processor
https://fx.wtf
MIT License
19.08k stars 438 forks source link

[feature] Add ability to output selection when exiting #209

Closed ditsuke closed 1 year ago

ditsuke commented 2 years ago

What

When exiting, we could use either an entry flag or a keyboard shortcut to output selected value to standard output.

Why

This would make fx useful as part of interactive pipelines.

History

Previously implemented in the NodeJS version, but not yet in the rewritten-in-Go version. Previous request: #132.

sinux-l5d commented 2 years ago

I came across fx seeking this exact feature. I'm using AWS a lot currently, and when I manually run commands it would be useful to pipe into interactive fx to choose the property I want.

So I would just have to do $VALUABLE_INFO=$(aws <whichever> | fx) to store for example an IP address (I never remember the path to the value...)

antonmedv commented 2 years ago

I think a fx -select or something will be ok.

sinux-l5d commented 2 years ago

So is it a feature you're still considering?

antonmedv commented 2 years ago

Yes, for sure. Don’t know when I gonna find time to implement it, unless someone decides to support this feature development)

sinux-l5d commented 2 years ago

Hi again, I have a prototype. This works really simply: when the --select/-s flag is passed, the program outputs the value of the selected level/property (model.cursorPath()) when exiting.

To make the program interactive when stdout is piped (aka not a tty), I use stderr. It works, but as I think it might not be a good practice I have a second commit that use stderr only then the select flag is passed and output is not a tty. Bubble Tea seams to work perfectly using stderr. Otherwise, I'd need to dig into how programs like vipe works to ask for user input while piping to other command.

You can try this feature with something like curl -s https://dummyjson.com/products/1 | go run . -s or curl -s https://dummyjson.com/products/1 | go run . -s | cat, and going into the document to select a property: it will output its value.

I'll open a pull request if you're interested :)

pyhedgehog commented 1 year ago

I propose extension to this feature: Three options:

  1. Print value in JSON format.
  2. Print value in raw format (if string)
  3. Print path to selected element (like .x[123]["some strange key"].key.a.b.c[0].d) to be used with fx or jq.
antonmedv commented 1 year ago

Now js has yank support.

pyhedgehog commented 1 year ago

@antonmedv

Now js has yank support.

It's good, but there are two drawbacks:

  1. How to customize what console program think clipboard is? I.e. I'm using windows and connecting to linux via several ssh hops... you understand.
  2. How to use this (as was planned from start) as a dialog-like selector in script? I.e. I've planned to write curl http://example.com/something.json|fx --select-value|jq -c 'to_entries[]'|sponge entries.jsonl.