-
-
Notifications
You must be signed in to change notification settings - Fork 282
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
[grep-] add loader for output of grep/ripgrep #2443
Conversation
For using ripgrep to search data that is not UTF-8, like binary data, the use of the visidata/visidata/loaders/grep.py Line 41 in 4721764
The
|
I added the ability to parse the output of standard |
visidata/loaders/grep.py
Outdated
if vd.options.grep_base_dir and not os.path.isabs(given): | ||
given = vd.options.grep_base_dir + os.sep + row.file | ||
p = Path(given) | ||
except: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never use a bare except:
in Python. At minimum, use except Exception:
, as almost all exceptions inherit from Exception. This is a general rule for Python, but in fact is important for VisiData, as its EscapeException
inherits from Python's BaseException
(which is the true base class for all exceptions), and is used when the user cancels an operation with Ctrl+C. So anywhere a bare except:
is used in VisiData, may lead to a situation where the user cannot cancel an operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, interesting! Okay, I've fixed it to be more specific.
This is really awesome, @midichef! I think this is possibly close to being merge-able as is. The main thing that I'd want, is to create a page of documentation which describes how to use it (perhaps as a Guide). Some of the internal comments, like the shell functions at the top, would then be moved there.
I don't think this is strictly necessary. Someone who needs to open it in a different editor can create a small wrapper script which parses the |
Okay I've added a guide. Let me know if I should change the guide in any way, to improve its helpfulness. I think using vd for grep is a powerful combination, and I'd like to make sure the guide explains it well for new users. |
32f5dd9
to
8eeef9e
Compare
Hi @midichef! Thank you so much for writing a guide! I made some edits, that I summarised in the commit. I wanted to write an explanation for some of them:
|
- move .grep file format description to /formats - add index for GrepSheet - change tabs to spaces - bold and camel case **GrepSheet** - add macro for sysopen-row - remove in-sentence macro for options.grep_base_dir - add brief explanation of $EDITOR - remove .bashrc snippet
8eeef9e
to
5e2063d
Compare
-H will make a filename appear even in the case of grepping only 1 file.
Thanks for explaining the chanegs. I'll keep these in mind for the future. |
This is a loader for working with output of ripgrep, when run as
rg --sort path --json pattern |vd -f grep
(
--sort path
makes ripgrep's search result order be deterministic, but slower due to loss of parallelism in searching files). This is a great use case for visidata. It's very helpful to filter grep results within visidata, and then useEnter
to jump into editing the file, and quit out of the editor, and jump into a different file withEnter
.The ripgrep output format is quite advanced and implements much more than this loader takes advantage of. For example, ripgrep says which column a match starts at, not just which line.
This commit is only a draft. One aspect that definitely needs to be changed is the command to open a file at a specific line. Right now it uses commands of the form
$EDITOR +5
, which works with vim and emacsclient, but not other editors. How should we let the user customize their command to open a file in an editor at a given line number?One use case to keep in mind is annotating grep results by adding columns. For example, if I'm debugging visidata and I want to audit the calls to a particular function like
openSource()
, I'll runrg --sort path --json openSource |vd -f grep
. Then I'll add a column for notes on whether that particular line is safe, and edit it for each line as I inspect each use. When I'm pausing my audit for the day, I'll save the file asnotes.grep
, and the next day I'll resume by loading:vd notes.grep
.And one feature to consider, for power users, would be to add lines of context to each row. These would come from
rg -A
andrg -B
, which work just the same asgrep -A
andgrep -B
, showing lines after and before the match. If each row had these lines of context it would allow very powerful filtering of results. I think it'd be quite helpful as a shell tool, and could draw more users to visidata. But it's not a feature I can work on at the moment.