rhysd / hgrep

Grep with human-friendly search results

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to visually indicate matches when consuming grep -nH output?

sdbbs opened this issue · comments

commented

I have installed via cargo install hgrep under MINGW64 (Windows 10).

Assuming an input file like myfile.c:

/*
 * foo has changed [...] and is now a 2-parameters function
 */
// foo(24)
int myvar = foo(42, 28);

... then if I run hgrep on the file directly (e.g. hgrep foo myfile.c) - then the matches are nicely indicated (in addition to corresponding line numbers):

image

... however if I try to Eat grep -nH output (e.g. grep -nH foo myfile.c | hgrep) - then the matches are NOT indicated anymore, only the corresponding line numbers are:

image

Can I somehow get the matched substrings to be indicated, also when consuming grep -nH output?

Highlighting matches is a feature only for built-in ripgrep. So it is not available when using hgrep with grep -nH. This is a limitation of the format of -nH option. It does not include column numbers of the match so it is not possible to know the match positions.

commented

I do not want to reopen the issue, just wanted to say many thanks for the answer, @rhysd - that explains it. And also wanted to comment on this:

It does not include column numbers of the match so it is not possible to know the match positions.

If one uses grep --color=always, then wouldn't it be possible to look for the ANSI color escape codes, and use their positions as match positions? If we do grep --color=always foo myfile.c > out.txt, we can see that e.g. last line match is actually int myvar = �[01;31m�[Kfoo�[m�[K(42, 28);, where �[01;31m�[K would be start of (default grep) red color for match, and �[m�[K for end of match ... Or in other words - if = 0x1B = ESC = \e = ^[, then ^[[01;31m^[[K would represent the start of match, and ^[[m^[[K the end of match.