1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/formatter/stylish.go

89 lines
2.3 KiB
Go
Raw Normal View History

2017-08-29 19:47:29 +02:00
package formatter
2017-08-28 01:57:16 +02:00
import (
"bytes"
"fmt"
2017-11-20 04:44:42 +02:00
"github.com/fatih/color"
2018-01-25 01:44:03 +02:00
"github.com/mgechev/revive/lint"
2017-08-28 01:57:16 +02:00
"github.com/olekukonko/tablewriter"
)
2018-05-26 21:08:02 +02:00
// Stylish is an implementation of the Formatter interface
2017-08-28 01:57:16 +02:00
// which formats the errors to JSON.
2018-05-26 21:08:02 +02:00
type Stylish struct {
2018-01-25 01:44:03 +02:00
Metadata lint.FormatterMetadata
2017-08-28 01:57:16 +02:00
}
2018-01-28 02:22:17 +02:00
// Name returns the name of the formatter
2018-05-26 21:08:02 +02:00
func (f *Stylish) Name() string {
return "stylish"
2018-01-28 02:22:17 +02:00
}
2018-01-25 01:44:03 +02:00
func formatFailure(failure lint.Failure, severity lint.Severity) []string {
2017-11-20 04:44:42 +02:00
fString := color.BlueString(failure.Failure)
2018-01-27 21:52:36 +02:00
fName := color.RedString(failure.RuleName)
2017-08-28 01:57:16 +02:00
lineColumn := failure.Position
2017-11-20 04:44:42 +02:00
pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)
2018-01-25 01:44:03 +02:00
if severity == lint.SeverityWarning {
2018-01-27 21:52:36 +02:00
fName = color.YellowString(failure.RuleName)
2017-08-28 01:57:16 +02:00
}
2018-01-27 21:52:36 +02:00
return []string{failure.GetFilename(), pos, fName, fString}
2017-08-28 01:57:16 +02:00
}
2018-01-25 01:44:03 +02:00
// Format formats the failures gotten from the lint.
2018-05-26 21:08:02 +02:00
func (f *Stylish) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
2017-08-28 01:57:16 +02:00
var result [][]string
var totalErrors = 0
2018-01-24 03:14:23 +02:00
var total = 0
2018-01-24 04:19:06 +02:00
2018-01-24 03:14:23 +02:00
for f := range failures {
total++
2018-05-26 22:47:13 +02:00
currentType := severity(config, f)
if currentType == lint.SeverityError {
2017-08-28 01:57:16 +02:00
totalErrors++
}
2018-01-25 01:44:03 +02:00
result = append(result, formatFailure(f, lint.Severity(currentType)))
2017-08-28 01:57:16 +02:00
}
ps := "problems"
if total == 1 {
ps = "problem"
}
fileReport := make(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 {
buf := new(bytes.Buffer)
table := tablewriter.NewWriter(buf)
table.SetBorder(false)
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetAutoWrapText(false)
table.AppendBulk(val)
table.Render()
2017-11-20 04:44:42 +02:00
c := color.New(color.Underline)
output += c.SprintfFunc()(filename + "\n")
2017-08-28 01:57:16 +02:00
output += buf.String() + "\n"
}
2017-08-29 19:53:29 +02:00
suffix := fmt.Sprintf(" %d %s (%d errors) (%d warnings)", total, ps, totalErrors, total-totalErrors)
2017-08-28 01:57:16 +02:00
2017-08-29 19:53:29 +02:00
if total > 0 && totalErrors > 0 {
2017-11-20 04:44:42 +02:00
suffix = color.RedString("\n ✖" + suffix)
2017-08-29 19:53:29 +02:00
} else if total > 0 && totalErrors == 0 {
2017-11-20 04:44:42 +02:00
suffix = color.YellowString("\n ✖" + suffix)
2017-08-29 19:53:29 +02:00
} else {
2017-11-20 04:44:42 +02:00
suffix = color.GreenString("\n" + suffix)
2017-08-29 19:53:29 +02:00
}
2017-08-28 01:57:16 +02:00
return output + suffix, nil
}