GNU bug report logs - #26572
problem with "diff --color": escape sequences shouldn't span multiple lines

Previous Next

Package: diffutils;

Reported by: <ian_bruce <at> mail.ru>

Date: Thu, 20 Apr 2017 00:54:01 UTC

Severity: normal

Full log


View this message in rfc822 format

From: <ian_bruce <at> mail.ru>
To: 26572 <at> debbugs.gnu.org
Subject: bug#26572: problem with "diff --color": escape sequences shouldn't span multiple lines
Date: Thu, 20 Apr 2017 05:27:02 -0700
[Message part 1 (text/plain, inline)]
On Wed, 19 Apr 2017 17:39:36 -0700
<ian_bruce <at> mail.ru> wrote:

> In any situation where the output of "diff --color" is subjected to
> further processing, colour-escaped text which spans multiple lines
> will cause a problem, if either the first or last line of the block is
> somehow removed or hidden, as with "less". The solution is to repeat
> the escape sequences before and after each newline:
> 
>     <COLOUR-ESCAPE-IN>first  line of block<COLOUR-ESCAPE-OUT>\n
>     <COLOUR-ESCAPE-IN>second line of block<COLOUR-ESCAPE-OUT>\n
>     <COLOUR-ESCAPE-IN>third  line of block<COLOUR-ESCAPE-OUT>\n

For now, this works much better than "diff --color":

    cdiff() 
    { 
      diff "$@" |
        sed -e '/^\(---\|+++\)/s/.*/\c[\[1m&\c[\[0m/' \
            -e      '/^[@0-9]/s/.*/\c[\[36m&\c[\[0m/' \
            -e        '/^[-<]/s/.*/\c[\[31m&\c[\[0m/' \
            -e        '/^[+>]/s/.*/\c[\[32m&\c[\[0m/'
    }

    cdiff -u file.c~ file.c | less -R

The attached images illustrate the differences in output:

    diff --color=always -u file.c~ file.c | less   # diff-color-less.png

    cdiff               -u file.c~ file.c | less   # diff-sed-less.png

It seems like the problem would be solved completely, if the output of
"diff --color" was changed to this form.


-- Ian Bruce
[diff-color-less.png (image/png, attachment)]
[diff-sed-less.png (image/png, attachment)]

This bug report was last modified 8 years and 58 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.