Skip to content

Commit 5401dc2

Browse files
committed
Adjust root module code for github.com/olekukonko/tablewriter 1.x API
The API of the `github.com/olekukonko/tablewriter` module was completely redesigned prior to the 1.0.0 release. This made it incompatible with the code written for the API of the 0.0.x versions of the module. These code changes are purely refactoring. They do not result in any change to the output of Arduino Lint. The previous version of `github.com/olekukonko/tablewriter` had a bug that caused it to incorrectly add a blank line when the cell content contained an explicit line break. That bug has been fixed, and so the code previously required to work around the bug has been removed.
1 parent 6992f84 commit 5401dc2

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

internal/result/result.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"github.com/arduino/arduino-lint/internal/rule/ruleresult"
3535
"github.com/arduino/go-paths-helper"
3636
"github.com/olekukonko/tablewriter"
37+
"github.com/olekukonko/tablewriter/renderer"
38+
"github.com/olekukonko/tablewriter/tw"
3739
)
3840

3941
// Results is the global instance of the rule results result.Type struct
@@ -126,18 +128,46 @@ func (results *Type) Record(lintedProject project.Type, ruleConfiguration ruleco
126128
prefix := fmt.Sprintf("%s: ", level)
127129

128130
formattedOutput := &strings.Builder{}
129-
table := tablewriter.NewWriter(formattedOutput)
130-
table.SetBorder(false)
131-
table.SetColumnSeparator("")
132-
table.SetNoWhiteSpace(true)
133-
table.SetColWidth(width - len(prefix))
134-
table.SetReflowDuringAutoWrap(false) // Reflow removes explicit line breaks.
135-
table.Append([]string{prefix, message})
136-
table.Render()
137-
// Remove blank lines on explicit line breaks caused by tablewriter bug.
138-
cleanedOutput := blankLineRegexp.ReplaceAllLiteralString(formattedOutput.String(), "\n")
139-
140-
return cleanedOutput
131+
132+
tableConfigBuilder := tablewriter.NewConfigBuilder()
133+
// Configure column widths so that the text will be wrapped to the appropriate width.
134+
tableConfigBuilder.ForColumn(0).WithMaxWidth(len(prefix))
135+
tableConfigBuilder.ForColumn(1).WithMaxWidth(width - len(prefix))
136+
// A trailing space is intentionally added to the "prefix" string. Trimming must be disabled to preserve that space.
137+
tableConfigBuilder.WithTrimSpace(tw.Off)
138+
tableConfig := tableConfigBuilder.Build()
139+
140+
tableRendition := tw.Rendition{
141+
// Do not add border characters to the table.
142+
Borders: tw.BorderNone,
143+
Settings: tw.Settings{
144+
/*
145+
Do not add a separator character between columns. The trailing space on the "prefix" string serves as the
146+
separator.
147+
*/
148+
Separators: tw.SeparatorsNone,
149+
},
150+
}
151+
152+
tableRenderer := renderer.NewBlueprint(tableRendition)
153+
154+
table := tablewriter.NewTable(
155+
formattedOutput,
156+
tablewriter.WithConfig(tableConfig),
157+
tablewriter.WithRenderer(tableRenderer),
158+
// Do not add margins to the cell content.
159+
tablewriter.WithPadding(tw.PaddingNone),
160+
)
161+
162+
if err := table.Append([]string{prefix, message}); err != nil {
163+
panic(err)
164+
}
165+
166+
if err := table.Render(); err != nil {
167+
panic(err)
168+
}
169+
170+
return formattedOutput.String()
141171
}
142172

143173
if configuration.Verbose() {

0 commit comments

Comments
 (0)