# [PATCH] git-gui: fix ANSI-color parsing

2 messages from 2010-11-15 to 2010-11-19. Participants: Bert Wesarg, Pat Thoyts.
Thread: https://gitlist.dev/t/25749

## Bert Wesarg, 2010-11-15 10:00

Subject: [PATCH] git-gui: fix ANSI-color parsing
Message-ID: <1289815233-11391-1-git-send-email-bert.wesarg@googlemail.com>
URL: https://gitlist.dev/e/1289815233-11391-1-git-send-email-bert.wesarg%40googlemail.com

```
git diff always outputs color reset commands, even when the color for the
current part is disabled (ie. normal). But the current ANSI-color parsing code
assumes that color start and reset commands appear in matching pairs.

Relax this assumption.

Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>

---
 git-gui/lib/diff.tcl |   12 ++++++++++--
 1 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/git-gui/lib/diff.tcl b/git-gui/lib/diff.tcl
index dcf0711..0579fa6 100644
--- a/git-gui/lib/diff.tcl
+++ b/git-gui/lib/diff.tcl
@@ -337,12 +337,20 @@ proc parse_color_line {line} {
 	set result ""
 	set markup [list]
 	set regexp {\033\[((?:\d+;)*\d+)?m}
+	set need_reset 0
 	while {[regexp -indices -start $start $regexp $line match code]} {
 		foreach {begin end} $match break
 		append result [string range $line $start [expr {$begin - 1}]]
-		lappend markup [string length $result] \
-			[eval [linsert $code 0 string range $line]]
+		set pos [string length $result]
+		set col [eval [linsert $code 0 string range $line]]
 		set start [incr end]
+		if {$col eq "0" || $col eq ""} {
+			if {!$need_reset} continue
+			set need_reset 0
+		} else {
+			set need_reset 1
+		}
+		lappend markup $pos $col
 	}
 	append result [string range $line $start end]
 	if {[llength $markup] < 4} {set markup {}}
-- 
tg: (6f10c41..) bw/git-gui/fix-ansi-color-parsing (depends on: master)

```

## Pat Thoyts, 2010-11-19 09:34

Subject: Re: [PATCH] git-gui: fix ANSI-color parsing
Message-ID: <87sjyxhd2v.fsf@fox.patthoyts.tk>
URL: https://gitlist.dev/e/87sjyxhd2v.fsf%40fox.patthoyts.tk
In-Reply-To: <1289815233-11391-1-git-send-email-bert.wesarg@googlemail.com>

```
Bert Wesarg <bert.wesarg@googlemail.com> writes:

>git diff always outputs color reset commands, even when the color for the
>current part is disabled (ie. normal). But the current ANSI-color parsing code
>assumes that color start and reset commands appear in matching pairs.
>
>Relax this assumption.
>
>Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com>

Thanks for this. Applied to git-gui repository.
-- 
Pat Thoyts                            http://www.patthoyts.tk/
PGP fingerprint 2C 6E 98 07 2C 59 C8 97  10 CE 11 E6 04 E0 B9 DD

```
