1
0
mirror of https://github.com/mgechev/revive.git synced 2025-03-29 21:47:12 +02:00
revive/formatter/cli_formatter.go

89 lines
2.1 KiB
Go
Raw Normal View History

2017-08-29 10:47:29 -07:00
package formatter
2017-08-27 16:57:16 -07:00
import (
"bytes"
"fmt"
2017-11-19 18:44:42 -08:00
"github.com/fatih/color"
2018-01-21 18:04:41 -08:00
"github.com/mgechev/revive/linter"
2017-08-27 16:57:16 -07:00
"github.com/olekukonko/tablewriter"
)
const (
errorEmoji = ""
warningEmoji = ""
)
// CLIFormatter is an implementation of the Formatter interface
// which formats the errors to JSON.
type CLIFormatter struct {
2018-01-21 18:04:41 -08:00
Metadata linter.FormatterMetadata
2017-08-27 16:57:16 -07:00
}
2018-01-21 18:04:41 -08:00
func formatFailure(failure linter.Failure) []string {
2017-11-19 18:44:42 -08:00
fString := color.BlueString(failure.Failure)
2017-08-27 16:57:16 -07:00
fTypeStr := string(failure.Type)
2017-11-19 18:44:42 -08:00
fType := color.RedString(fTypeStr)
2017-08-27 16:57:16 -07:00
lineColumn := failure.Position
2017-11-19 18:44:42 -08:00
pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)
2018-01-21 18:04:41 -08:00
if failure.Type == linter.FailureTypeWarning {
2017-11-19 18:44:42 -08:00
fType = color.YellowString(fTypeStr)
2017-08-27 16:57:16 -07:00
}
return []string{failure.GetFilename(), pos, fType, fString}
}
// Format formats the failures gotten from the linter.
2018-01-23 17:14:23 -08:00
func (f *CLIFormatter) Format(failures <-chan linter.Failure) (string, error) {
2017-08-27 16:57:16 -07:00
var result [][]string
var totalErrors = 0
2018-01-23 17:14:23 -08:00
var total = 0
2018-01-23 18:19:06 -08:00
2018-01-23 17:14:23 -08:00
for f := range failures {
2017-08-27 16:57:16 -07:00
result = append(result, formatFailure(f))
2018-01-23 17:14:23 -08:00
total++
2018-01-21 18:04:41 -08:00
if f.Type == linter.FailureTypeError {
2017-08-27 16:57:16 -07:00
totalErrors++
}
}
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-19 18:44:42 -08:00
c := color.New(color.Underline)
output += c.SprintfFunc()(filename + "\n")
2017-08-27 16:57:16 -07:00
output += buf.String() + "\n"
}
2017-08-29 10:53:29 -07:00
suffix := fmt.Sprintf(" %d %s (%d errors) (%d warnings)", total, ps, totalErrors, total-totalErrors)
2017-08-27 16:57:16 -07:00
2017-08-29 10:53:29 -07:00
if total > 0 && totalErrors > 0 {
2017-11-19 18:44:42 -08:00
suffix = color.RedString("\n ✖" + suffix)
2017-08-29 10:53:29 -07:00
} else if total > 0 && totalErrors == 0 {
2017-11-19 18:44:42 -08:00
suffix = color.YellowString("\n ✖" + suffix)
2017-08-29 10:53:29 -07:00
} else {
2017-11-19 18:44:42 -08:00
suffix = color.GreenString("\n" + suffix)
2017-08-29 10:53:29 -07:00
}
2017-08-27 16:57:16 -07:00
return output + suffix, nil
}