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

91 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-22 04:04:41 +02:00
"github.com/mgechev/revive/linter"
2017-08-28 01:57:16 +02: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-22 04:04:41 +02:00
Metadata linter.FormatterMetadata
2017-08-28 01:57:16 +02:00
}
2018-01-25 01:41:40 +02:00
func formatFailure(failure linter.Failure, severity linter.Severity) []string {
2017-11-20 04:44:42 +02:00
fString := color.BlueString(failure.Failure)
2018-01-25 01:41:40 +02:00
fTypeStr := string(severity)
2017-11-20 04:44:42 +02:00
fType := color.RedString(fTypeStr)
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:41:40 +02:00
if severity == linter.SeverityWarning {
2017-11-20 04:44:42 +02:00
fType = color.YellowString(fTypeStr)
2017-08-28 01:57:16 +02:00
}
return []string{failure.GetFilename(), pos, fType, fString}
}
// Format formats the failures gotten from the linter.
2018-01-25 01:41:40 +02:00
func (f *CLIFormatter) Format(failures <-chan linter.Failure, config linter.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-01-25 01:41:40 +02:00
currentType := linter.SeverityWarning
if config, ok := config[f.RuleName]; ok && config.Severity == linter.SeverityError {
currentType = linter.SeverityError
2017-08-28 01:57:16 +02:00
totalErrors++
}
2018-01-25 01:41:40 +02:00
result = append(result, formatFailure(f, linter.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
}