Skip to content

Commit 53180ab

Browse files
committed
Remove spurious blank lines from rule output
The `github.com/olekukonko/tablewriter` package used for formatting of the rule output does not correctly handle text with explicit (as opposed by ones added during auto-wrapping) line breaks. With reflow enabled, these explicit line breaks are removed and the line wrapped solely based on the column width, just as it would be without the explicit line breaks. For example, given this rule message: ``` .exe file(s) found. Presence of these files blocks addition to the Library Manager index: deleteme-testdata\bar.exe deleteme-testdata\examples\baz.exe deleteme-testdata\examples\qux\asdf.exe (Rule LS007) ``` The output is: ``` ERROR: .exe file(s) found. Presence of these files blocks addition to the Library Manager index: deleteme-testdata/bar.exe deleteme-testdata/examples/baz.exe deleteme-testdata/examples/qux/asdf.exe (Rule LS007) ``` With reflow disabled, each explicit line break becomes a blank line: ``` ERROR: .exe file(s) found. Presence of these files blocks addition to the Library Manager index: deleteme-testdata/bar.exe deleteme-testdata/examples/baz.exe deleteme-testdata/examples/qux/asdf.exe (Rule LS007) ``` I could not find any way to coerce the package to produce the desired output: ``` ERROR: .exe file(s) found. Presence of these files blocks addition to the Library Manager index: deleteme-testdata/bar.exe deleteme-testdata/examples/baz.exe deleteme-testdata/examples/qux/asdf.exe (Rule LS007) ``` so I had to resort to post-processing of the non-reflowed output to remove the spurious blank lines.
1 parent 3fb6daf commit 53180ab

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Diff for: internal/result/result.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"io"
24+
"regexp"
2425
"strings"
2526
"text/template"
2627

@@ -96,6 +97,8 @@ func (results *Type) Initialize() {
9697
}
9798
}
9899

100+
var blankLineRegexp = regexp.MustCompile("\n[[:space:]]*\n")
101+
99102
// Record records the result of a rule and returns a text summary for it.
100103
func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleconfiguration.Type, ruleResult ruleresult.Type, ruleOutput string) string {
101104
ruleLevel, err := rulelevel.RuleLevel(ruleConfiguration, ruleResult, lintedProject)
@@ -124,10 +127,13 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
124127
table.SetColumnSeparator("")
125128
table.SetNoWhiteSpace(true)
126129
table.SetColWidth(width - len(prefix))
130+
table.SetReflowDuringAutoWrap(false) // Reflow removes explicit line breaks.
127131
table.Append([]string{prefix, message})
128132
table.Render()
133+
// Remove blank lines on explicit line breaks caused by tablewriter bug.
134+
cleanedOutput := blankLineRegexp.ReplaceAllLiteralString(formattedOutput.String(), "\n")
129135

130-
return formattedOutput.String()
136+
return cleanedOutput
131137
}
132138

133139
if configuration.Verbose() {

0 commit comments

Comments
 (0)