Subject: Re: problem with "diff --color": escape sequences shouldn't span multiple lines

From: <[email-masked]>
Date: Thu, 20 Apr 2017 05:27:02 -0700

On Wed, 19 Apr 2017 17:39:36 -0700
<[email-masked]> 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