1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-03 00:26:51 +02:00
Files
revive/formatter/stylish.go
Hazhir Derakhshi f3f77bb387 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
2025-05-17 19:33:14 +02:00

82 lines
2.0 KiB
Go

package formatter
import (
"fmt"
"github.com/fatih/color"
"github.com/mgechev/revive/lint"
)
// Stylish is an implementation of the Formatter interface
// which formats the errors to JSON.
type Stylish struct {
Metadata lint.FormatterMetadata
}
// Name returns the name of the formatter
func (*Stylish) Name() string {
return "stylish"
}
func formatFailure(failure lint.Failure, severity lint.Severity) []string {
fString := color.CyanString(failure.Failure)
fURL := ruleDescriptionURL(failure.RuleName)
fName := color.RedString(fURL)
lineColumn := failure.Position
pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)
if severity == lint.SeverityWarning {
fName = color.YellowString(fURL)
}
return []string{failure.GetFilename(), pos, fName, fString}
}
// Format formats the failures gotten from the lint.
func (*Stylish) Format(failures <-chan lint.Failure, config lint.Config) (string, error) {
var result [][]string
totalErrors := 0
total := 0
for f := range failures {
total++
currentType := severity(config, f)
if currentType == lint.SeverityError {
totalErrors++
}
result = append(result, formatFailure(f, lint.Severity(currentType)))
}
ps := "problems"
if total == 1 {
ps = "problem"
}
fileReport := map[string][][]string{}
for _, row := range result {
if _, ok := fileReport[row[0]]; !ok {
fileReport[row[0]] = [][]string{}
}
fileReport[row[0]] = append(fileReport[row[0]], []string{row[1], row[2], row[3]})
}
output := ""
for filename, val := range fileReport {
c := color.New(color.Underline)
output += c.SprintfFunc()(filename + "\n")
output += table(val) + "\n"
}
suffix := fmt.Sprintf(" %d %s (%d errors) (%d warnings)", total, ps, totalErrors, total-totalErrors)
switch {
case total > 0 && totalErrors > 0:
suffix = color.RedString("\n ✖" + suffix)
case total > 0 && totalErrors == 0:
suffix = color.YellowString("\n ✖" + suffix)
default:
suffix, output = "", ""
}
return output + suffix, nil
}