diff options
author | Joe Tsai <joetsai@digital-static.net> | 2020-06-10 11:00:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-10 11:00:30 -0700 |
commit | 9b300311a803504267fb9fc5040a430aa7013956 (patch) | |
tree | 1ad0851b6a1214bb1c5b54e49eba2b42f8bb82ce /cmp/report_text.go | |
parent | 367e530b4ed74b514889e7945f0b18145337eff3 (diff) | |
download | go-cmp-9b300311a803504267fb9fc5040a430aa7013956.tar.gz |
Batch reporter output for simple lists of textLine elements (#208)
Rather than having a single element on each line,
which hurts readability due to the need for scrolling quite a bit.
Batch multiple elements for simple lists to be on a single line.
Fixes #170
Diffstat (limited to 'cmp/report_text.go')
-rw-r--r-- | cmp/report_text.go | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/cmp/report_text.go b/cmp/report_text.go index 8b8fcab..7849d65 100644 --- a/cmp/report_text.go +++ b/cmp/report_text.go @@ -16,6 +16,8 @@ import ( var randBool = rand.New(rand.NewSource(time.Now().Unix())).Intn(2) == 0 +const maxColumnLength = 80 + type indentMode int func (n indentMode) appendIndent(b []byte, d diffMode) []byte { @@ -221,7 +223,7 @@ func (s textList) formatCompactTo(b []byte, d diffMode) ([]byte, textNode) { } // Force multi-lined output when printing a removed/inserted node that // is sufficiently long. - if (d == diffInserted || d == diffRemoved) && len(b[n0:]) > 80 { + if (d == diffInserted || d == diffRemoved) && len(b[n0:]) > maxColumnLength { multiLine = true } if !multiLine { @@ -246,6 +248,40 @@ func (s textList) formatExpandedTo(b []byte, d diffMode, n indentMode) []byte { func(r textRecord) int { return len(r.Value.(textLine)) }, ) + // Format lists of simple lists in a batched form. + // If the list is sequence of only textLine values, + // then batch multiple values on a single line. + var isSimple bool + for _, r := range s { + _, isLine := r.Value.(textLine) + isSimple = r.Diff == 0 && r.Key == "" && isLine && r.Comment == nil + if !isSimple { + break + } + } + if isSimple { + n++ + var batch []byte + emitBatch := func() { + if len(batch) > 0 { + b = n.appendIndent(append(b, '\n'), d) + b = append(b, bytes.TrimRight(batch, " ")...) + batch = batch[:0] + } + } + for _, r := range s { + line := r.Value.(textLine) + if len(batch)+len(line)+len(", ") > maxColumnLength { + emitBatch() + } + batch = append(batch, line...) + batch = append(batch, ", "...) + } + emitBatch() + n-- + return n.appendIndent(append(b, '\n'), d) + } + // Format the list as a multi-lined output. n++ for i, r := range s { |