8000 Remove tablewriter dependency by volfgox · Pull Request #1351 · mgechev/revive · GitHub
[go: up one dir, main page]

Skip to content

Conversation

volfgox
Copy link
Contributor
@volfgox volfgox commented May 15, 2025

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] [2] [3]

Dependency Cleanup:

  • Removed the tablewriter library from the go.mod file. [1]

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] [2]

Closes #1345

@volfgox volfgox marked this pull request as ready for review May 15, 2025 22:32
Comment on lines 19 to 21
if w := utf8.RuneCountInString(col); w > colWidths[i] {
colWidths[i] = w
}
Copy link
Contributor

Choose a rea 8000 son for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's about this, no?

Suggested change
if w := utf8.RuneCountInString(col); w > colWidths[i] {
colWidths[i] = w
}
colWidths[i] = max(colWidths[i], utf8.RuneCountInString(col))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm pretty sure slices.MaxFunc can help also, if you want to try

https://pkg.go.dev/slices#MaxFunc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review.

The 2 for loops can be replaced with the following using MaxFunc.

	for i := range colwidths {
		colwidths[i] = utf8.runecountinstring(slices.maxfunc(rows, func(row1, row2 []string) int {
			return utf8.runecountinstring(row1[i]) - utf8.runecountinstring(row2[i])
		})[i])
	}

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's wait for you to try text/tabwriter as @denisvmedia suggested.

@denisvmedia
Copy link
Collaborator
denisvmedia commented May 16, 2025

Thank you for your PR. Can we have unit tests for that? Also, did you consider using text/tabwriter?

@volfgox
Copy link
Contributor Author
volfgox commented May 16, 2025

Thank you for your PR. Can we have unit tests for that?

I relied on existing tests as the code was refactoring without adding new functionality. I can add unit tests for the formatTable function.

@volfgox
Copy link
Contributor Author
volfgox commented May 16, 2025

Also, did you consider using text/tabwriter?

Nice shout. I'll look into it. Thanks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to rename this file to table_utils.go (or something like that)

Why? Because this name allows to still easily distinguish formatters implementation files (e.g., checkstyle.go, default.go, stylish.go ... ) from auxiliary files in the formatter packet directory. Keeping table.go might make think that there is a table formatter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree with using table_utils.go, I propose an alternative: placing the formatTable function at the end of either the friendly.go or stylish.go file. There's no need to create too many files — it's not the Go way.

Comment on lines 131 to 139
var buf strings.Builder
tw := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
for _, row := range rows {
fmt.Fprintf(tw, "\t")
for _, col := range row {
fmt.Fprintf(tw, "%s\t", col)
}
tw.Write([]byte("\n"))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a matter of style, but we can use the same bytes.Buffer and don't use fmt.Fprintf:

Suggested change
var buf strings.Builder
tw := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
for _, row := range rows {
fmt.Fprintf(tw, "\t")
for _, col := range row {
fmt.Fprintf(tw, "%s\t", col)
}
tw.Write([]byte("\n"))
}
buf := new(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'})
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexandear idiomatically bytes.Buffer needs no initialization, so it should be var buf bytes.Buffer.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I left buf := new(bytes.Buffer) because it was in the original version 🙂

@volfgox volfgox force-pushed the chore/remove-tablewriter-dependency branch from 2c75826 to b491505 Compare May 17, 2025 08:52
var buf bytes.Buffer
tw := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
for _, row := range rows {
tw.Write([]byte{'\t'})
Copy link
Contributor

Choose a reason for hiding this comment

67E6

The reason will be displayed to describe this comment to others. Learn more.

I don't get why lines start with tab

https://go.dev/play/p/tu8LLRA5LCZ

---|---a|---b|---aligned|

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To build this indentation:

Screenshot 2025-05-17 at 10 52 45

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we are fine that result are left aligned like this

https://go.dev/play/p/7UT-dXUTa5K

Screenshot_20250517_134834_Firefox.jpg

Note: I used debug mode and dash padding to debug how data are displayed. We won't use this. It's for helping understanding.

So now, I'm fine if everyone agree it's fine that words are left aligned

I have no idea how code was formatted before with the tabwritter lib we had.

From what I saw, there is nothing in std tawritter lib to align the first column to right, and second to left.

@chavacava chavacava merged commit f3f77bb into mgechev:master May 17, 2025
7 checks passed
@chavacava
Copy link
Collaborator

Thanks everybody @volfgox @denisvmedia @alexandear @ccoVeille

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove github.com/olekukonko/tablewriter dependency

5 participants

0