Repository: mgechev/revive (go)

Issue:
Remove github.com/olekukonko/tablewriter dependency
**Is your feature request related to a problem? Please describe.**

From https://github.com/mgechev/revive/pull/1341#issuecomment-2876232422:

> Do we actually need this dependency?
> The formatter does a very basic use of library's functionalities, we could just print the lines in an equivalent format without relying on a table.

**Describe the solution you'd like**

Replace usages of `tablewriter.NewWriter` with `fmt.Sprintf`.

**Additional context**

We are using an old version of `tablewriter` and need to update it in #1341 to fix #1342. However, we’re encountering some difficulties during the migration.

Pull request description:
Remove tablewriter dependency
This pull request refactors the table formatting logic by replacing the dependency on `tablewriter` with a custom `formatTable` function. It also removes unused imports and updates the `Friendly` and `Stylish` formatters to use the new table formatting implementation. Below are the most important changes:

### Table Formatting Refactor:
* Added a new `formatTable` function in `formatter/table.go` to handle table formatting without external dependencies. This function aligns columns and ensures consistent formatting.
* Updated `Friendly` and `Stylish` formatters to use the new `formatTable` function instead of the removed `tablewriter` logic. [[1]](diffhunk://#diff-d6c7b119190a1976a3a1f3ed73ca9df3bfee46ab82509928b91be00b469c512aL67-R70) [[2]](diffhunk://#diff-d6c7b119190a1976a3a1f3ed73ca9df3bfee46ab82509928b91be00b469c512aL128-R126) [[3]](diffhunk://#diff-2561f2dbb72eb563b94e61ffc5fc7c7d61995eca006572fd8e1306c46e353841L66-R66)

### Dependency Cleanup:
* Removed the `tablewriter` library from the `go.mod` file. [[1]](diffhunk://#diff-33ef32bf6c23acb95f5902d7097b7a1d5128ca061167ec0716715b0b9eeaa5f6L12)

### Code Simplification:
* Removed the `table` method from the `Friendly` formatter as it is no longer needed due to the introduction of `formatTable`.
* Simplified method receivers in `Friendly` by removing unnecessary pointer references where they are not required. [[1]](diffhunk://#diff-d6c7b119190a1976a3a1f3ed73ca9df3bfee46ab82509928b91be00b469c512aL67-R70) [[2]](diffhunk://#diff-d6c7b119190a1976a3a1f3ed73ca9df3bfee46ab82509928b91be00b469c512aL112-R110)

Closes #1345 

<!-- ### IMPORTANT ### -->
<!-- Please do not create a Pull Request without creating an issue first.** -->
<!-- If you're fixing a typo or improving the documentation, you may not have to open an issue. -->

<!-- ### CHECKLIST ### -->
<!-- Please, describe in details what's your motivation for this PR -->
<!-- Did you add tests? -->
<!-- Does your code follow the coding style of the rest of the repository? -->
<!-- Does the GitHub Action build passes? -->

<!-- ### FOOTER (OPTIONAL) ### -->
<!-- If you're closing an issue, add "Closes #XXXX" in your comment. This way, the PR will be linked to the issue automatically. -->


Patch:
diff --git a/formatter/friendly.go b/formatter/friendly.go
index 6ba9ade..3b7bea0 100644
--- a/formatter/friendly.go
+++ b/formatter/friendly.go
@@ -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()
 }
diff --git a/formatter/stylish.go b/formatter/stylish.go
index bb3d7cd..fce5baa 100644
--- a/formatter/stylish.go
+++ b/formatter/stylish.go
@@ -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)
diff --git a/go.mod b/go.mod
index 07dfe1c..9f968d7 100644
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,6 @@ require (
 	github.com/fatih/structtag v1.2.0
 	github.com/hashicorp/go-version v1.7.0
 	github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517
-	github.com/olekukonko/tablewriter v0.0.5
 	github.com/spf13/afero v1.14.0
 	golang.org/x/mod v0.24.0
 	golang.org/x/sync v0.14.0
@@ -20,9 +19,7 @@ require (
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/mattn/go-colorable v0.1.14 // indirect
 	github.com/mattn/go-isatty v0.0.20 // indirect
-	github.com/mattn/go-runewidth v0.0.16 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
-	github.com/rivo/uniseg v0.4.7 // indirect
 	golang.org/x/sys v0.33.0 // indirect
 	golang.org/x/text v0.25.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
diff --git a/go.sum b/go.sum
index 216e804..5513451 100644
--- a/go.sum
+++ b/go.sum
@@ -14,18 +14,10 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
 github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
 github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
-github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
-github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
-github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
 github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517 h1:zpIH83+oKzcpryru8ceC6BxnoG8TBrhgAvRg8obzup0=
 github.com/mgechev/dots v0.0.0-20210922191527-e955255bf517/go.mod h1:KQ7+USdGKfpPjXk4Ga+5XxQM4Lm4e3gAogrreFAYpOg=
-github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
-github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
-github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
-github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
 github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA=
 github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo=
 github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=


Test patch:
diff --git a/formatter/friendly_test.go b/formatter/friendly_test.go
index e0deb90..5684b2f 100644
--- a/formatter/friendly_test.go
+++ b/formatter/friendly_test.go
@@ -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)
+			}
+		})
+	}
+}