aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/diff
diff options
context:
space:
mode:
authorTom Payne <twpayne@gmail.com>2020-04-29 04:15:25 +0100
committerTom Payne <twpayne@gmail.com>2020-04-30 02:47:49 +0100
commitaab967d28e8ece09c1d94241cb8da25be1e6b6cf (patch)
treeb47cd823381bf1d641da87bdb1e59a3891945e39 /plumbing/format/diff
parentc7b6d19fba4a389926bdf6fcf74fb628d439c82c (diff)
downloadgo-git-aab967d28e8ece09c1d94241cb8da25be1e6b6cf.tar.gz
plumbing: diff, don't emit unnecessary resets
Diffstat (limited to 'plumbing/format/diff')
-rw-r--r--plumbing/format/diff/colorconfig.go9
-rw-r--r--plumbing/format/diff/unified_encoder.go21
-rw-r--r--plumbing/format/diff/unified_encoder_test.go16
3 files changed, 24 insertions, 22 deletions
diff --git a/plumbing/format/diff/colorconfig.go b/plumbing/format/diff/colorconfig.go
index 1e72e7f..6fd4158 100644
--- a/plumbing/format/diff/colorconfig.go
+++ b/plumbing/format/diff/colorconfig.go
@@ -86,10 +86,11 @@ func NewColorConfig(options ...ColorConfigOption) ColorConfig {
return cc
}
-// Reset returns the ANSI escape sequence to reset a color set from cc. If cc is
-// nil or empty then no reset is needed so it returns the empty string.
-func (cc ColorConfig) Reset() string {
- if len(cc) == 0 {
+// Reset returns the ANSI escape sequence to reset the color with key set from
+// cc. If no color was set then no reset is needed so it returns the empty
+// string.
+func (cc ColorConfig) Reset(key ColorKey) string {
+ if cc[key] == "" {
return ""
}
return color.Reset
diff --git a/plumbing/format/diff/unified_encoder.go b/plumbing/format/diff/unified_encoder.go
index 6472db2..bd115f8 100644
--- a/plumbing/format/diff/unified_encoder.go
+++ b/plumbing/format/diff/unified_encoder.go
@@ -142,21 +142,21 @@ func (e *UnifiedEncoder) header(from, to File, isBinary bool) error {
e.pathLines(isBinary, aDir+from.Path(), bDir+to.Path())
}
- e.buf.WriteString(e.color.Reset())
+ e.buf.WriteString(e.color.Reset(Meta))
case from == nil:
e.buf.WriteString(e.color[Meta])
fmt.Fprintf(&e.buf, diffInit, to.Path(), to.Path())
fmt.Fprintf(&e.buf, newFileMode, to.Mode())
fmt.Fprintf(&e.buf, indexNoMode, plumbing.ZeroHash, to.Hash())
e.pathLines(isBinary, noFilePath, bDir+to.Path())
- e.buf.WriteString(e.color.Reset())
+ e.buf.WriteString(e.color.Reset(Meta))
case to == nil:
e.buf.WriteString(e.color[Meta])
fmt.Fprintf(&e.buf, diffInit, from.Path(), from.Path())
fmt.Fprintf(&e.buf, deletedFileMode, from.Mode())
fmt.Fprintf(&e.buf, indexNoMode, from.Hash(), plumbing.ZeroHash)
e.pathLines(isBinary, aDir+from.Path(), noFilePath)
- e.buf.WriteString(e.color.Reset())
+ e.buf.WriteString(e.color.Reset(Meta))
}
return nil
@@ -338,13 +338,13 @@ func (c *hunk) WriteTo(buf *bytes.Buffer, color ColorConfig) {
}
buf.WriteString(chunkEnd)
- buf.WriteString(color.Reset())
+ buf.WriteString(color.Reset(Frag))
if c.ctxPrefix != "" {
buf.WriteByte(' ')
buf.WriteString(color[Func])
buf.WriteString(c.ctxPrefix)
- buf.WriteString(color.Reset())
+ buf.WriteString(color.Reset(Func))
}
buf.WriteByte('\n')
@@ -377,22 +377,23 @@ type op struct {
}
func (o *op) String(color ColorConfig) string {
- var setColor, prefix, suffix string
+ var prefix, suffix string
+ var colorKey ColorKey
switch o.t {
case Add:
prefix = addLine
- setColor = color[New]
+ colorKey = New
case Delete:
prefix = deleteLine
- setColor = color[Old]
+ colorKey = Old
case Equal:
prefix = equalLine
- setColor = color[Context]
+ colorKey = Context
}
n := len(o.text)
if n > 0 && o.text[n-1] != '\n' {
suffix = noNewLine
}
- return fmt.Sprintf(prefix, setColor, o.text, color.Reset(), suffix)
+ return fmt.Sprintf(prefix, color[colorKey], o.text, color.Reset(colorKey), suffix)
}
diff --git a/plumbing/format/diff/unified_encoder_test.go b/plumbing/format/diff/unified_encoder_test.go
index 83bc3c2..1e44572 100644
--- a/plumbing/format/diff/unified_encoder_test.go
+++ b/plumbing/format/diff/unified_encoder_test.go
@@ -896,7 +896,7 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f
"--- a/README.md\n" +
"+++ b/README.md\n" + color.Reset +
color.Cyan + "@@ -1,2 +1,2 @@" + color.Reset + "\n" +
- color.Normal + " hello\n" + color.Reset +
+ " hello\n" +
color.Red + "-world\n" + color.Reset +
color.Green + "+bug\n" + color.Reset,
}, {
@@ -951,19 +951,19 @@ index 0adddcde4fd38042c354518351820eb06c417c82..d39ae38aad7ba9447b5e7998b2e4714f
"+++ b/onechunk.txt\n" + color.Reset +
color.Cyan + "@@ -1,2 +1 @@" + color.Reset + "\n" +
color.Red + "-A\n" + color.Reset +
- color.Normal + " B\n" + color.Reset +
+ " B\n" +
color.Cyan + "@@ -7,3 +6,2 @@" + color.Reset + " " + color.Reverse + "F" + color.Reset + "\n" +
- color.Normal + " G\n" + color.Reset +
+ " G\n" +
color.Red + "-H\n" + color.Reset +
- color.Normal + " I\n" + color.Reset +
+ " I\n" +
color.Cyan + "@@ -14,3 +12,2 @@" + color.Reset + " " + color.Reverse + "M" + color.Reset + "\n" +
- color.Normal + " N\n" + color.Reset +
+ " N\n" +
color.Red + "-Ñ\n" + color.Reset +
- color.Normal + " O\n" + color.Reset +
+ " O\n" +
color.Cyan + "@@ -21,3 +18,2 @@" + color.Reset + " " + color.Reverse + "S" + color.Reset + "\n" +
- color.Normal + " T\n" + color.Reset +
+ " T\n" +
color.Red + "-U\n" + color.Reset +
- color.Normal + " V\n" + color.Reset,
+ " V\n",
}}
type testPatch struct {