@@ -21,7 +21,7 @@ type FormattingOptions struct {
2121}
2222
2323const (
24- foregroundColorEscapeGrey = "\u001b [90m"
24+ ForegroundColorEscapeGrey = "\u001b [90m"
2525 foregroundColorEscapeRed = "\u001b [91m"
2626 foregroundColorEscapeYellow = "\u001b [93m"
2727 foregroundColorEscapeBlue = "\u001b [94m"
@@ -31,51 +31,53 @@ const (
3131const (
3232 gutterStyleSequence = "\u001b [7m"
3333 gutterSeparator = " "
34- resetEscapeSequence = "\u001b [0m"
34+ ResetEscapeSequence = "\u001b [0m"
3535 ellipsis = "..."
3636)
3737
3838func FormatDiagnosticsWithColorAndContext (output io.Writer , diags []* ast.Diagnostic , formatOpts * FormattingOptions ) {
3939 if len (diags ) == 0 {
4040 return
4141 }
42-
4342 for i , diagnostic := range diags {
4443 if i > 0 {
4544 fmt .Fprint (output , formatOpts .NewLine )
4645 }
46+ FormatDiagnosticWithColorAndContext (output , diagnostic , formatOpts )
47+ }
48+ }
4749
48- if diagnostic .File () != nil {
49- file := diagnostic .File ()
50- pos := diagnostic .Loc ().Pos ()
51- WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
52- fmt .Fprint (output , " - " )
53- }
50+ func FormatDiagnosticWithColorAndContext (output io.Writer , diagnostic * ast.Diagnostic , formatOpts * FormattingOptions ) {
51+ if diagnostic .File () != nil {
52+ file := diagnostic .File ()
53+ pos := diagnostic .Loc ().Pos ()
54+ WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
55+ fmt .Fprint (output , " - " )
56+ }
5457
55- writeWithStyleAndReset (output , diagnostic .Category ().Name (), getCategoryFormat (diagnostic .Category ()))
56- fmt .Fprintf (output , "%s TS%d: %s" , foregroundColorEscapeGrey , diagnostic .Code (), resetEscapeSequence )
57- WriteFlattenedDiagnosticMessage (output , diagnostic , formatOpts .NewLine )
58+ writeWithStyleAndReset (output , diagnostic .Category ().Name (), getCategoryFormat (diagnostic .Category ()))
59+ fmt .Fprintf (output , "%s TS%d: %s" , ForegroundColorEscapeGrey , diagnostic .Code (), ResetEscapeSequence )
60+ WriteFlattenedDiagnosticMessage (output , diagnostic , formatOpts .NewLine )
5861
59- if diagnostic .File () != nil && diagnostic .Code () != diagnostics .File_appears_to_be_binary .Code () {
60- fmt .Fprint (output , formatOpts .NewLine )
61- writeCodeSnippet (output , diagnostic .File (), diagnostic .Pos (), diagnostic .Len (), getCategoryFormat (diagnostic .Category ()), "" , formatOpts )
62- fmt .Fprint (output , formatOpts .NewLine )
63- }
62+ if diagnostic .File () != nil && diagnostic .Code () != diagnostics .File_appears_to_be_binary .Code () {
63+ fmt .Fprint (output , formatOpts .NewLine )
64+ writeCodeSnippet (output , diagnostic .File (), diagnostic .Pos (), diagnostic .Len (), getCategoryFormat (diagnostic .Category ()), "" , formatOpts )
65+ fmt .Fprint (output , formatOpts .NewLine )
66+ }
6467
65- if (diagnostic .RelatedInformation () != nil ) && (len (diagnostic .RelatedInformation ()) > 0 ) {
66- for _ , relatedInformation := range diagnostic .RelatedInformation () {
67- file := relatedInformation .File ()
68- if file != nil {
69- fmt .Fprint (output , formatOpts .NewLine )
70- fmt .Fprint (output , " " )
71- pos := relatedInformation .Pos ()
72- WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
73- fmt .Fprint (output , " - " )
74- WriteFlattenedDiagnosticMessage (output , relatedInformation , formatOpts .NewLine )
75- writeCodeSnippet (output , file , pos , relatedInformation .Len (), foregroundColorEscapeCyan , " " , formatOpts )
76- }
68+ if (diagnostic .RelatedInformation () != nil ) && (len (diagnostic .RelatedInformation ()) > 0 ) {
69+ for _ , relatedInformation := range diagnostic .RelatedInformation () {
70+ file := relatedInformation .File ()
71+ if file != nil {
7772 fmt .Fprint (output , formatOpts .NewLine )
73+ fmt .Fprint (output , " " )
74+ pos := relatedInformation .Pos ()
75+ WriteLocation (output , file , pos , formatOpts , writeWithStyleAndReset )
76+ fmt .Fprint (output , " - " )
77+ WriteFlattenedDiagnosticMessage (output , relatedInformation , formatOpts .NewLine )
78+ writeCodeSnippet (output , file , pos , relatedInformation .Len (), foregroundColorEscapeCyan , " " , formatOpts )
7879 }
80+ fmt .Fprint (output , formatOpts .NewLine )
7981 }
8082 }
8183}
@@ -104,7 +106,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
104106 fmt .Fprint (writer , indent )
105107 fmt .Fprint (writer , gutterStyleSequence )
106108 fmt .Fprintf (writer , "%*s" , gutterWidth , ellipsis )
107- fmt .Fprint (writer , resetEscapeSequence )
109+ fmt .Fprint (writer , ResetEscapeSequence )
108110 fmt .Fprint (writer , gutterSeparator )
109111 fmt .Fprint (writer , formatOpts .NewLine )
110112 i = lastLine - 1
@@ -125,7 +127,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
125127 fmt .Fprint (writer , indent )
126128 fmt .Fprint (writer , gutterStyleSequence )
127129 fmt .Fprintf (writer , "%*d" , gutterWidth , i + 1 )
128- fmt .Fprint (writer , resetEscapeSequence )
130+ fmt .Fprint (writer , ResetEscapeSequence )
129131 fmt .Fprint (writer , gutterSeparator )
130132 fmt .Fprint (writer , lineContent )
131133 fmt .Fprint (writer , formatOpts .NewLine )
@@ -134,7 +136,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
134136 fmt .Fprint (writer , indent )
135137 fmt .Fprint (writer , gutterStyleSequence )
136138 fmt .Fprintf (writer , "%*s" , gutterWidth , "" )
137- fmt .Fprint (writer , resetEscapeSequence )
139+ fmt .Fprint (writer , ResetEscapeSequence )
138140 fmt .Fprint (writer , gutterSeparator )
139141 fmt .Fprint (writer , squiggleColor )
140142 if i == firstLine {
@@ -159,7 +161,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
159161 fmt .Fprint (writer , strings .Repeat ("~" , len (lineContent )))
160162 }
161163
162- fmt .Fprint (writer , resetEscapeSequence )
164+ fmt .Fprint (writer , ResetEscapeSequence )
163165 }
164166}
165167
@@ -196,7 +198,7 @@ func getCategoryFormat(category diagnostics.Category) string {
196198 case diagnostics .CategoryWarning :
197199 return foregroundColorEscapeYellow
198200 case diagnostics .CategorySuggestion :
199- return foregroundColorEscapeGrey
201+ return ForegroundColorEscapeGrey
200202 case diagnostics .CategoryMessage :
201203 return foregroundColorEscapeBlue
202204 }
@@ -208,7 +210,7 @@ type FormattedWriter func(output io.Writer, text string, formatStyle string)
208210func writeWithStyleAndReset (output io.Writer , text string , formatStyle string ) {
209211 fmt .Fprint (output , formatStyle )
210212 fmt .Fprint (output , text )
211- fmt .Fprint (output , resetEscapeSequence )
213+ fmt .Fprint (output , ResetEscapeSequence )
212214}
213215
214216func WriteLocation (output io.Writer , file * ast.SourceFile , pos int , formatOpts * FormattingOptions , writeWithStyleAndReset FormattedWriter ) {
@@ -359,9 +361,9 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
359361 }
360362 return fmt .Sprintf ("%s%s:%d%s" ,
361363 fileName ,
362- foregroundColorEscapeGrey ,
364+ ForegroundColorEscapeGrey ,
363365 line + 1 ,
364- resetEscapeSequence ,
366+ ResetEscapeSequence ,
365367 )
366368}
367369
@@ -383,3 +385,15 @@ func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatO
383385 WriteFlattenedDiagnosticMessage (output , diagnostic , formatOpts .NewLine )
384386 fmt .Fprint (output , formatOpts .NewLine )
385387}
388+
389+ func FormatDiagnosticsStatusWithColorAndTime (output io.Writer , time string , diag * ast.Diagnostic , formatOpts * FormattingOptions ) {
390+ fmt .Fprint (output , "[" )
391+ writeWithStyleAndReset (output , time , ForegroundColorEscapeGrey )
392+ fmt .Fprint (output , "] " )
393+ WriteFlattenedDiagnosticMessage (output , diag , formatOpts .NewLine )
394+ }
395+
396+ func FormatDiagnosticsStatusAndTime (output io.Writer , time string , diag * ast.Diagnostic , formatOpts * FormattingOptions ) {
397+ fmt .Fprint (output , time , " - " )
398+ WriteFlattenedDiagnosticMessage (output , diag , formatOpts .NewLine )
399+ }
0 commit comments