22package formatter
33
44import (
5+ "fmt"
6+ "strconv"
57 "strings"
68
79 "github.com/conventionalcommit/commitlint/lint"
@@ -15,6 +17,37 @@ func (f *DefaultFormatter) Name() string { return "default" }
1517
1618// Format formats the lint.Result
1719func (f * DefaultFormatter ) Format (res * lint.Result ) (string , error ) {
18- msg := formatResult (res )
19- return strings .Trim (msg , "\n " ), nil
20+ return formatResult (res ), nil
21+ }
22+
23+ func formatResult (res * lint.Result ) string {
24+ if res .IsOK () {
25+ return " ✔ commit message"
26+ }
27+ return writeResult (res )
28+ }
29+
30+ func writeResult (res * lint.Result ) string {
31+ str := & strings.Builder {}
32+
33+ str .WriteString ("commitlint" )
34+ fmt .Fprintf (str , "\n \n → input: %s" , strconv .Quote (truncate (25 , res .Input ())))
35+
36+ if res .HasErrors () {
37+ writeLintResult (str , "Errors" , res .Errors (), "❌" )
38+ }
39+
40+ if res .HasWarns () {
41+ writeLintResult (str , "Warnings" , res .Warns (), "!" )
42+ }
43+
44+ fmt .Fprintf (str , "\n \n Total %d errors, %d warnings" , len (res .Errors ()), len (res .Warns ()))
45+ return str .String ()
46+ }
47+
48+ func writeLintResult (w * strings.Builder , title string , resArr []lint.RuleResult , sign string ) {
49+ fmt .Fprint (w , "\n \n " + title + ":" )
50+ for _ , msg := range resArr {
51+ fmt .Fprintf (w , "\n %s %s: %s" , sign , msg .Name , msg .Message )
52+ }
2053}
0 commit comments