@@ -17,6 +17,77 @@ require 'vm'
1717
1818local export = {}
1919
20+ local colors
21+
22+ if not os.getenv (' NO_COLOR' ) then
23+ colors = {
24+ red = ' \27 [31m' ,
25+ green = ' \27 [32m' ,
26+ blue = ' \27 [34m' ,
27+ yellow = ' \27 [33m' ,
28+ grey = ' \27 [90m' ,
29+ reset = ' \27 [0m'
30+ }
31+ else
32+ colors = {
33+ red = ' ' ,
34+ green = ' ' ,
35+ blue = ' ' ,
36+ yellow = ' ' ,
37+ grey = ' ' ,
38+ reset = ' '
39+ }
40+ end
41+
42+ local severity_colors = {
43+ [1 ] = colors .red ,
44+ [2 ] = colors .yellow ,
45+ [3 ] = colors .blue ,
46+ [4 ] = colors .green ,
47+ }
48+
49+ local severity_str = {
50+ [1 ] = ' error' ,
51+ [2 ] = ' warning' ,
52+ [3 ] = ' info' ,
53+ [4 ] = ' hint'
54+ }
55+
56+ --- @param path string
57+ --- @return string
58+ local function relpath (path )
59+ local pwd = os.getenv (' PWD' )
60+ if pwd and path :sub (1 , # pwd ) == pwd then
61+ path = path :sub (# pwd + 2 )
62+ end
63+ return path
64+ end
65+
66+ local function report_pretty (results )
67+ for f , diags in pairs (results ) do
68+ local path = relpath (f :match (' ^file://(.+)$' ) or f )
69+
70+ for _ , d in ipairs (diags ) do
71+ print (
72+ (' %s%s:%s:%s%s [%s%s%s] %s %s(%s)%s' ):format (
73+ colors .blue ,
74+ path ,
75+ d .range .start .line ,
76+ d .range .start .character ,
77+ colors .reset ,
78+ severity_colors [d .severity ],
79+ severity_str [d .severity ],
80+ colors .reset ,
81+ d .message ,
82+ colors .grey ,
83+ d .code ,
84+ colors .reset
85+ )
86+ )
87+ end
88+ end
89+ end
90+
2091function export .runCLI ()
2192 lang (LOCALE )
2293
@@ -89,14 +160,14 @@ function export.runCLI()
89160
90161 -- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
91162 local diagStatus = config .get (rootUri , ' Lua.diagnostics.neededFileStatus' )
92- for diag , status in pairs (diagStatus ) do
163+ for d , status in pairs (diagStatus ) do
93164 if status == ' Any' or status == ' Any!' then
94- diagStatus [diag ] = ' Opened!'
165+ diagStatus [d ] = ' Opened!'
95166 end
96167 end
97- for diag , status in pairs (protoDiag .getDefaultStatus ()) do
168+ for d , status in pairs (protoDiag .getDefaultStatus ()) do
98169 if status == ' Any' or status == ' Any!' then
99- diagStatus [diag ] = ' Opened!'
170+ diagStatus [d ] = ' Opened!'
100171 end
101172 end
102173 config .set (rootUri , ' Lua.diagnostics.neededFileStatus' , diagStatus )
@@ -134,7 +205,8 @@ function export.runCLI()
134205 end
135206 end
136207 if not QUIET then
137- io.write (' \x0D ' )
208+ -- Write out empty space to ensure that the progress bar is cleared.
209+ io.write (' \x0D ' , (' ' ):rep (80 ), ' \x0D ' )
138210 end
139211 end )
140212
@@ -146,18 +218,25 @@ function export.runCLI()
146218 end
147219 end
148220
149- local outpath = CHECK_OUT_PATH
150- if outpath == nil then
151- outpath = LOGPATH .. ' /check.json'
221+ local outpath = nil
222+
223+ if CHECK_FORMAT == ' pretty' then
224+ report_pretty (results )
225+ end
226+
227+ if CHECK_FORMAT == ' json' or CHECK_OUT_PATH then
228+ outpath = CHECK_OUT_PATH or LOGPATH .. ' /check.json'
229+ -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
230+ util .saveFile (outpath , jsonb .beautify (results ))
152231 end
153- -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
154- util .saveFile (outpath , jsonb .beautify (results ))
155232
156233 if not QUIET then
157234 if count == 0 then
158235 print (lang .script (' CLI_CHECK_SUCCESS' ))
236+ elseif outpath then
237+ print (lang .script (' CLI_CHECK_RESULTS_OUTPATH' , count , outpath ))
159238 else
160- print (lang .script (' CLI_CHECK_RESULTS ' , count , outpath ))
239+ print (lang .script (' CLI_CHECK_RESULTS_PRETTY ' , count ))
161240 end
162241 end
163242 return count == 0 and 0 or 1
0 commit comments