Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json output option to allow scripting #359

Closed
timotheecour opened this issue Feb 12, 2017 · 3 comments
Closed

json output option to allow scripting #359

timotheecour opened this issue Feb 12, 2017 · 3 comments

Comments

@timotheecour
Copy link

timotheecour commented Feb 12, 2017

Could we have a json output option?
use case: making it easier to integrate in other tools [1]
eg:

rg '\bWORLD\b' $args --output=json
{"path":"foo/bar.txt","line":3,"offset":258,"column":4,"length":5,"match":"WORLD" ,"context":"hello WORLD !" }

offset corresponds to byte offset in file (useful for efficient access for the other tool)
length is in bytes (in case of non-ascii)

The alternative (manual parsing) is error prone, eg:

  • if filename is foo/bar:13:foo.txt (contains :)
  • if the escape codes colorizing the matched word are hard to find because the context contains some escape codes itself (eg it can actually happens if it's a log file)
  • relying on --column doesn't work for multiple matches in 1 line (ok just saw --vimgrep but doens't show colors, so we can't get the extent of each match)

[1] NOTE: one of these other tools is multiline search built on top of ripgrep

@BurntSushi
Copy link
Owner

I think what you're asking for is "structured output," where JSON might be your preference. In that case, I think this is a dupe of #244. To be clear, I do not want to be in the business of producing a bunch of different output formats because there is so much complexity involved. (Every option that influences output has to be considered against the new format.)

NOTE: one of these other tools is multiline search built on top of ripgrep

While I'm in favor of adding some type of structured output to ripgrep, I would rather see these types of tools built with libripgrep than with ripgrep itself.

@timotheecour
Copy link
Author

timotheecour commented Feb 13, 2017

@BurntSushi

  • since these structured output (json or not, doesn't matter) will be meant for machine consumption, it would make sense to keep options to a bare minimum for this mode: ignore color, and have a single formatting option (doesn't matter so long all information is there to get to the match; byte offset as i said above would be useful, to allow efficient machine access to a match without having to parse the file).

  • One option maybe: whether or not to show the corresponding matching line of text from the file.

  • is there any doc on libripgrep, maybe a code snippet? can I link against it say from C-like languages?

@BurntSushi
Copy link
Owner

Then this should be closed as a duplicate.

There are many options, and their effects all need to be individually reasoned through.

is there any doc on libripgrep, maybe a code snippet?

Please see the link in my previous comment. It's not done yet. Libripgrep refers to a collection of libraries. The endgame is to make ripgrep itself very small with most logic in reusable libraries. A lot of it is already done.

can I link against it say from C-like languages?

Someone (probably not me) will need to put in the work to build a C interface, and then ffi bindings for their favorite language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants