@@ -17,6 +17,96 @@ 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+ yellow = ' \27 [33m' ,
27+ blue = ' \27 [34m' ,
28+ magenta = ' \27 [35m' ,
29+ grey = ' \27 [90m' ,
30+ reset = ' \27 [0m'
31+ }
32+ else
33+ colors = {
34+ red = ' ' ,
35+ green = ' ' ,
36+ yellow = ' ' ,
37+ blue = ' ' ,
38+ magenta = ' ' ,
39+ grey = ' ' ,
40+ reset = ' '
41+ }
42+ end
43+
44+ local severity_colors = {
45+ [1 ] = colors .red ,
46+ [2 ] = colors .yellow ,
47+ [3 ] = colors .blue ,
48+ [4 ] = colors .green ,
49+ }
50+
51+ local severity_str = {
52+ [1 ] = ' error' ,
53+ [2 ] = ' warning' ,
54+ [3 ] = ' info' ,
55+ [4 ] = ' hint'
56+ }
57+
58+ --- @param path string
59+ --- @return string
60+ local function relpath (path )
61+ local pwd = os.getenv (' PWD' )
62+ if pwd and path :sub (1 , # pwd ) == pwd then
63+ path = path :sub (# pwd + 2 )
64+ end
65+ return path
66+ end
67+
68+ local function report_pretty (results )
69+ for f , diags in pairs (results ) do
70+ local path = relpath (f :match (' ^file://(.+)$' ) or f )
71+
72+ local lines = {} --- @type string[]
73+ pcall (function ()
74+ for line in io.lines (path ) do
75+ table.insert (lines , line )
76+ end
77+ end )
78+
79+ for _ , d in ipairs (diags ) do
80+ local rstart = d .range .start
81+ local rend = d .range [' end' ]
82+ print (
83+ (' %s%s:%s:%s%s [%s%s%s] %s %s(%s)%s' ):format (
84+ colors .blue ,
85+ path ,
86+ rstart .line ,
87+ rstart .character ,
88+ colors .reset ,
89+ severity_colors [d .severity ],
90+ severity_str [d .severity ],
91+ colors .reset ,
92+ d .message ,
93+ colors .magenta ,
94+ d .code ,
95+ colors .reset
96+ )
97+ )
98+ if # lines > 0 then
99+ io.write (lines [rstart .line + 1 ], ' \n ' )
100+ io.write (colors .grey , (' ' ):rep (rstart .character ), ' ^' )
101+ if rstart .line == rend .line then
102+ io.write ((' ^' ):rep (rend .character - rstart .character - 1 ))
103+ end
104+ io.write (colors .reset , ' \n ' )
105+ end
106+ end
107+ end
108+ end
109+
20110function export .runCLI ()
21111 lang (LOCALE )
22112
@@ -89,14 +179,14 @@ function export.runCLI()
89179
90180 -- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
91181 local diagStatus = config .get (rootUri , ' Lua.diagnostics.neededFileStatus' )
92- for diag , status in pairs (diagStatus ) do
182+ for d , status in pairs (diagStatus ) do
93183 if status == ' Any' or status == ' Any!' then
94- diagStatus [diag ] = ' Opened!'
184+ diagStatus [d ] = ' Opened!'
95185 end
96186 end
97- for diag , status in pairs (protoDiag .getDefaultStatus ()) do
187+ for d , status in pairs (protoDiag .getDefaultStatus ()) do
98188 if status == ' Any' or status == ' Any!' then
99- diagStatus [diag ] = ' Opened!'
189+ diagStatus [d ] = ' Opened!'
100190 end
101191 end
102192 config .set (rootUri , ' Lua.diagnostics.neededFileStatus' , diagStatus )
@@ -134,7 +224,8 @@ function export.runCLI()
134224 end
135225 end
136226 if not QUIET then
137- io.write (' \x0D ' )
227+ -- Write out empty space to ensure that the progress bar is cleared.
228+ io.write (' \x0D ' , (' ' ):rep (80 ), ' \x0D ' )
138229 end
139230 end )
140231
@@ -146,18 +237,25 @@ function export.runCLI()
146237 end
147238 end
148239
149- local outpath = CHECK_OUT_PATH
150- if outpath == nil then
151- outpath = LOGPATH .. ' /check.json'
240+ local outpath = nil
241+
242+ if CHECK_FORMAT == nil or CHECK_FORMAT == ' pretty' then
243+ report_pretty (results )
244+ end
245+
246+ if CHECK_FORMAT == ' json' or CHECK_OUT_PATH then
247+ outpath = CHECK_OUT_PATH or LOGPATH .. ' /check.json'
248+ -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
249+ util .saveFile (outpath , jsonb .beautify (results ))
152250 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 ))
155251
156252 if not QUIET then
157253 if count == 0 then
158254 print (lang .script (' CLI_CHECK_SUCCESS' ))
255+ elseif outpath then
256+ print (lang .script (' CLI_CHECK_RESULTS_OUTPATH' , count , outpath ))
159257 else
160- print (lang .script (' CLI_CHECK_RESULTS ' , count , outpath ))
258+ print (lang .script (' CLI_CHECK_RESULTS_PRETTY ' , count ))
161259 end
162260 end
163261 return count == 0 and 0 or 1
0 commit comments