1. Georg Brandl
  2. pygments-main
  3. Issues
Issue #686 wontfix

Incompatibility with grep highlighting

Anonymous created an issue

One very nice use for pygmentize is instead of cat when I want to look at source code in a terminal. When I do this, I often pass the output through grep, which has it's own nice highlighting for strings that matched. These two mostly play together nicely, but there is one small gripe, most easily explained with an example:

I run pygmentize on "test.f" which contains one line starting with ! (a Fortran comment), and the result is: (Sorry about the formatting, I couldn't get the source code thing here to work properly. > means executing a command, the next line is the result, italics are my remarks about the output.)

pygmentize test.f

! This comment contains the string foo and some more

//Pygmentize prints the commented line in gray, good.//

pygmentize test.sh | grep "foo" --color=always

! This comment contains the string foo and some more

//This prints the line in gray, up to "foo", then "foo" is highlighted in red by grep, and the rest of the line is printed in black. The desired behavior is that the rest of the line is printed in gray.//

I don't know if this is something that can be easily fixed in Pygments, or if it can be fixed at all, but I chose to report it anyway. I also realize that you have no obligation to support parsing output through other Linux tools, but it would be a nice feature.

Comments (2)

  1. Dmitri Gribenko

    I'm not a pygments developer, but I think I can comment on this. The problem is that grep thinks that it processes plain text (without any terminal escape sequences). Grep highlights a match by prepending an escape sequence that changes color to a highlight color and appending a "revert to default" at the end of match. In order to restore a correct previous color grep would have to parse escape sequences and keep track of current terminal state.

  2. Georg Brandl repo owner

    Dmitri is correct. This is nothing that Pygments can fix. (Except for repeating the current color code before each character, which is both ugly and would make you unable to grep for a phrase in the first place ;)

  3. Log in to comment