@@ -17,6 +17,86 @@ 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+ local function report_pretty (results )
59+ for f , diags in pairs (results ) do
60+ local path = furi .relpath (furi .decode (f ))
61+
62+ local lines = {} --- @type string[]
63+ pcall (function ()
64+ for line in io.lines (path ) do
65+ table.insert (lines , line )
66+ end
67+ end )
68+
69+ for _ , d in ipairs (diags ) do
70+ local rstart = d .range .start
71+ local rend = d .range [' end' ]
72+ print (
73+ (' %s%s:%s:%s%s [%s%s%s] %s %s(%s)%s' ):format (
74+ colors .blue ,
75+ path ,
76+ rstart .line ,
77+ rstart .character ,
78+ colors .reset ,
79+ severity_colors [d .severity ],
80+ severity_str [d .severity ],
81+ colors .reset ,
82+ d .message ,
83+ colors .magenta ,
84+ d .code ,
85+ colors .reset
86+ )
87+ )
88+ if # lines > 0 then
89+ io.write (' ' , lines [rstart .line + 1 ], ' \n ' )
90+ io.write (' ' , colors .grey , (' ' ):rep (rstart .character ), ' ^' )
91+ if rstart .line == rend .line then
92+ io.write ((' ^' ):rep (rend .character - rstart .character - 1 ))
93+ end
94+ io.write (colors .reset , ' \n ' )
95+ end
96+ end
97+ end
98+ end
99+
20100function export .runCLI ()
21101 lang (LOCALE )
22102
@@ -89,14 +169,14 @@ function export.runCLI()
89169
90170 -- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
91171 local diagStatus = config .get (rootUri , ' Lua.diagnostics.neededFileStatus' )
92- for diag , status in pairs (diagStatus ) do
172+ for d , status in pairs (diagStatus ) do
93173 if status == ' Any' or status == ' Any!' then
94- diagStatus [diag ] = ' Opened!'
174+ diagStatus [d ] = ' Opened!'
95175 end
96176 end
97- for diag , status in pairs (protoDiag .getDefaultStatus ()) do
177+ for d , status in pairs (protoDiag .getDefaultStatus ()) do
98178 if status == ' Any' or status == ' Any!' then
99- diagStatus [diag ] = ' Opened!'
179+ diagStatus [d ] = ' Opened!'
100180 end
101181 end
102182 config .set (rootUri , ' Lua.diagnostics.neededFileStatus' , diagStatus )
@@ -134,7 +214,8 @@ function export.runCLI()
134214 end
135215 end
136216 if not QUIET then
137- io.write (' \x0D ' )
217+ -- Write out empty space to ensure that the progress bar is cleared.
218+ io.write (' \x0D ' , (' ' ):rep (80 ), ' \x0D ' )
138219 end
139220 end )
140221
@@ -146,18 +227,25 @@ function export.runCLI()
146227 end
147228 end
148229
149- local outpath = CHECK_OUT_PATH
150- if outpath == nil then
151- outpath = LOGPATH .. ' /check.json'
230+ local outpath = nil
231+
232+ if CHECK_FORMAT == nil or CHECK_FORMAT == ' pretty' then
233+ report_pretty (results )
234+ end
235+
236+ if CHECK_FORMAT == ' json' or CHECK_OUT_PATH then
237+ outpath = CHECK_OUT_PATH or LOGPATH .. ' /check.json'
238+ -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
239+ util .saveFile (outpath , jsonb .beautify (results ))
152240 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 ))
155241
156242 if not QUIET then
157243 if count == 0 then
158244 print (lang .script (' CLI_CHECK_SUCCESS' ))
245+ elseif outpath then
246+ print (lang .script (' CLI_CHECK_RESULTS_OUTPATH' , count , outpath ))
159247 else
160- print (lang .script (' CLI_CHECK_RESULTS ' , count , outpath ))
248+ print (lang .script (' CLI_CHECK_RESULTS_PRETTY ' , count ))
161249 end
162250 end
163251 return count == 0 and 0 or 1
0 commit comments