grep: avoid duplicated lines when we're coloring output
For the default uncolored output, we'll just output a line once and then
move on. For colored output, we'll output multiple matches per
line with context from the line interspersed and may end up writing out
some match context multiple times as we don't persist which part of the
lines have already been printed.
Fix it by tracking the length of line printed thus far in printline()
and retaining it across successive calls to printline() in the same
line. printline() should indicate whether it terminated the line or not
to avoid tracking the logic for that in multiple places: -o lines are
always terminated, so it's generally only some --color contexts where we
wouldn't have terminated.
Add a test to make sure that we're only printing one line going forward.
Reported and tested by: Jamie Landeg-Jones <jamie catflap org>
Reviewed by: emaste
(cherry picked from commit 4c9ffb13dd74159bd3ed7e1c4c706dbd15a70df2)