mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
refactor: remove tablewriter dependency (#1351)
* Rewrite Friendly.table method to replace tablewriter with fmt.Fprintf * Refactor Stylish formatter to use custom table rendering instead of tablewriter * Refactor Friendly and Stylish formatters to utilize a new custom table rendering function * Remove unused dependencies from go.mod and go.sum * Refactor table formatting by replacing formatTable with a new table function in friendly.go * Utilize text/tabwriter in fromatting a table * Refactor table function to use bytes.Buffer for improved performance
This commit is contained in:
@@ -7,10 +7,10 @@ import (
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/mgechev/revive/lint"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
func getErrorEmoji() string {
|
||||
@@ -64,12 +64,12 @@ func (f *Friendly) printFriendlyFailure(sb *strings.Builder, failure lint.Failur
|
||||
sb.WriteString("\n\n")
|
||||
}
|
||||
|
||||
func (f *Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) {
|
||||
func (*Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) {
|
||||
emoji := getWarningEmoji()
|
||||
if severity == lint.SeverityError {
|
||||
emoji = getErrorEmoji()
|
||||
}
|
||||
sb.WriteString(f.table([][]string{{emoji, ruleDescriptionURL(failure.RuleName), color.GreenString(failure.Failure)}}))
|
||||
sb.WriteString(table([][]string{{emoji, ruleDescriptionURL(failure.RuleName), color.GreenString(failure.Failure)}}))
|
||||
}
|
||||
|
||||
func (*Friendly) printFilePosition(sb *strings.Builder, failure lint.Failure) {
|
||||
@@ -109,7 +109,7 @@ func (*Friendly) printSummary(w io.Writer, errors, warnings int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Friendly) printStatistics(w io.Writer, header string, stats map[string]int) {
|
||||
func (*Friendly) printStatistics(w io.Writer, header string, stats map[string]int) {
|
||||
if len(stats) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -125,17 +125,19 @@ func (f *Friendly) printStatistics(w io.Writer, header string, stats map[string]
|
||||
formatted = append(formatted, []string{color.GreenString(fmt.Sprintf("%d", entry.failures)), entry.name})
|
||||
}
|
||||
fmt.Fprintln(w, header)
|
||||
fmt.Fprintln(w, f.table(formatted))
|
||||
fmt.Fprintln(w, table(formatted))
|
||||
}
|
||||
|
||||
func (*Friendly) table(rows [][]string) string {
|
||||
buf := new(bytes.Buffer)
|
||||
table := tablewriter.NewWriter(buf)
|
||||
table.SetBorder(false)
|
||||
table.SetColumnSeparator("")
|
||||
table.SetRowSeparator("")
|
||||
table.SetAutoWrapText(false)
|
||||
table.AppendBulk(rows)
|
||||
table.Render()
|
||||
func table(rows [][]string) string {
|
||||
var buf bytes.Buffer
|
||||
tw := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
|
||||
for _, row := range rows {
|
||||
tw.Write([]byte{'\t'})
|
||||
for _, col := range row {
|
||||
tw.Write(append([]byte(col), '\t'))
|
||||
}
|
||||
tw.Write([]byte{'\n'})
|
||||
}
|
||||
tw.Flush()
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
@@ -31,6 +31,11 @@ func TestFriendly_printStatistics(t *testing.T) {
|
||||
stats: map[string]int{"rule2": 2, "rule1": 1, "rule3": 3},
|
||||
expected: "Warnings:\n 3 rule3 \n 2 rule2 \n 1 rule1 \n\n",
|
||||
},
|
||||
{
|
||||
name: "multiple stats with different length sorted by failures desc",
|
||||
stats: map[string]int{"rule2": 2, "rule1": 1, "rule3": 3, "rule100": 40},
|
||||
expected: "Warnings:\n 40 rule100 \n 3 rule3 \n 2 rule2 \n 1 rule1 \n\n",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -44,3 +49,41 @@ func TestFriendly_printStatistics(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFriendly_table(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input [][]string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "empty input",
|
||||
input: [][]string{},
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
name: "single row",
|
||||
input: [][]string{{"1", "2", "3"}},
|
||||
expected: " 1 2 3 \n",
|
||||
},
|
||||
{
|
||||
name: "multiple rows",
|
||||
input: [][]string{{"1", "2", "3"}, {"4", "5", "6"}},
|
||||
expected: " 1 2 3 \n 4 5 6 \n",
|
||||
},
|
||||
{
|
||||
name: "multiple rows with different column lengths",
|
||||
input: [][]string{{"1", "22", "3"}, {"4", "5", "6"}},
|
||||
expected: " 1 22 3 \n 4 5 6 \n",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := table(tt.input)
|
||||
if got != tt.expected {
|
||||
t.Errorf("got %q, want %q", got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/mgechev/revive/lint"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
)
|
||||
|
||||
// Stylish is an implementation of the Formatter interface
|
||||
@@ -63,17 +61,9 @@ func (*Stylish) Format(failures <-chan lint.Failure, config lint.Config) (string
|
||||
|
||||
output := ""
|
||||
for filename, val := range fileReport {
|
||||
buf := new(bytes.Buffer)
|
||||
table := tablewriter.NewWriter(buf)
|
||||
table.SetBorder(false)
|
||||
table.SetColumnSeparator("")
|
||||
table.SetRowSeparator("")
|
||||
table.SetAutoWrapText(false)
|
||||
table.AppendBulk(val)
|
||||
table.Render()
|
||||
c := color.New(color.Underline)
|
||||
output += c.SprintfFunc()(filename + "\n")
|
||||
output += buf.String() + "\n"
|
||||
output += table(val) + "\n"
|
||||
}
|
||||
|
||||
suffix := fmt.Sprintf(" %d %s (%d errors) (%d warnings)", total, ps, totalErrors, total-totalErrors)
|
||||
|
||||
Reference in New Issue
Block a user