@@ -9,78 +9,85 @@ import (
99 "github.com/conventionalcommit/commitlint/lint"
1010)
1111
12+ const (
13+ truncateSize = 25
14+ )
15+
1216// DefaultFormatter represent default formatter
1317type DefaultFormatter struct {}
1418
1519// Name returns name of formatter
1620func (f * DefaultFormatter ) Name () string { return "default" }
1721
1822// Format formats the lint.Failure
19- func (f * DefaultFormatter ) Format (res * lint.Result ) (string , error ) {
20- return formatFailure (res ), nil
23+ func (f * DefaultFormatter ) Format (result * lint.Result ) (string , error ) {
24+ return f . formatFailure (result . Input (), result . Issues () ), nil
2125}
2226
23- func formatFailure (res * lint.Result ) string {
24- if res . IsOK () {
27+ func ( f * DefaultFormatter ) formatFailure (msg string , issues [] * lint.Issue ) string {
28+ if len ( issues ) == 0 {
2529 return " ✔ commit message"
2630 }
27- return writeFailure (res )
31+ return f . writeFailure (msg , issues )
2832}
2933
30- func writeFailure (res * lint.Result ) string {
34+ func ( f * DefaultFormatter ) writeFailure (msg string , issues [] * lint.Issue ) string {
3135 str := & strings.Builder {}
3236
33- quotedStr := strconv .Quote (truncate (25 , res . Input () ))
37+ quotedStr := strconv .Quote (truncate (truncateSize , msg ))
3438
3539 str .WriteString ("commitlint\n " )
3640 str .WriteString ("\n → input: " + quotedStr )
3741
38- errs , warns , others := bySeverity (res )
42+ errs , warns , others := f . bySeverity (issues )
3943
40- writeRuleFailure (str , "❌" , "Errors" , errs )
41- writeRuleFailure (str , "!" , "Warnings" , warns )
42- writeRuleFailure (str , "?" , "Other Severities" , others )
44+ f . writeIssues (str , "❌" , "Errors" , errs )
45+ f . writeIssues (str , "!" , "Warnings" , warns )
46+ f . writeIssues (str , "?" , "Other Severities" , others )
4347
4448 fmt .Fprintf (str , "\n \n Total %d errors, %d warnings, %d other severities" , len (errs ), len (warns ), len (others ))
4549 return strings .Trim (str .String (), "\n " )
4650}
4751
48- func writeRuleFailure ( w * strings.Builder , sign , title string , resArr []* lint.Issue ) {
49- if len (resArr ) == 0 {
52+ func ( f * DefaultFormatter ) writeIssues ( w * strings.Builder , sign , title string , issues []* lint.Issue ) {
53+ if len (issues ) == 0 {
5054 return
5155 }
5256
5357 w .WriteString ("\n \n " + title + ":" )
54- for _ , ruleRes := range resArr {
55- writeMessages (w , ruleRes , sign )
58+ for _ , issue := range issues {
59+ f . writeIssue (w , sign , issue )
5660 }
5761}
5862
59- func writeMessages (w * strings.Builder , ruleRes * lint.Issue , sign string ) {
60- msgs := ruleRes .Message ()
61-
62- if len (msgs ) == 0 {
63- return
64- }
65-
63+ func (f * DefaultFormatter ) writeIssue (w * strings.Builder , sign string , issue * lint.Issue ) {
6664 space := " "
6765
68- if len (msgs ) == 1 {
69- msg := msgs [0 ]
70- // ❌ rule-name: message
71- fmt .Fprintf (w , "\n %s %s: %s" , space + sign , ruleRes .Name (), msg )
72- return
73- }
74-
7566 // ❌ rule-name:
7667 // - message1
7768 // - message2
78- fmt .Fprintf (w , "\n %s %s:" , space + sign , ruleRes .Name ())
79- for _ , msg := range ruleRes .Message () {
69+
70+ fmt .Fprintf (w , "\n %s %s:" , space + sign , issue .Name ())
71+ for _ , msg := range issue .Message () {
8072 fmt .Fprintf (w , "\n %s - %s" , space + space , msg )
8173 }
8274}
8375
76+ // bySeverity returns all messages with given severity
77+ func (f * DefaultFormatter ) bySeverity (issues []* lint.Issue ) (errs , warns , others []* lint.Issue ) {
78+ for _ , r := range issues {
79+ switch r .Severity () {
80+ case lint .SeverityError :
81+ errs = append (errs , r )
82+ case lint .SeverityWarn :
83+ warns = append (warns , r )
84+ default :
85+ others = append (others , r )
86+ }
87+ }
88+ return errs , warns , others
89+ }
90+
8491func truncate (maxSize int , input string ) string {
8592 if len (input ) < maxSize {
8693 return input
0 commit comments