@@ -17,6 +17,95 @@ 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+ white = ' \27 [37m' ,
30+ grey = ' \27 [90m' ,
31+ reset = ' \27 [0m'
32+ }
33+ else
34+ colors = {
35+ red = ' ' ,
36+ green = ' ' ,
37+ yellow = ' ' ,
38+ blue = ' ' ,
39+ magenta = ' ' ,
40+ white = ' ' ,
41+ grey = ' ' ,
42+ reset = ' '
43+ }
44+ end
45+
46+ --- @type table<DiagnosticSeverity , string>
47+ local severity_colors = {
48+ Error = colors .red ,
49+ Warning = colors .yellow ,
50+ Information = colors .white ,
51+ Hint = colors .white ,
52+ }
53+
54+ local severity_str = {} --- @type table<integer,DiagnosticSeverity>
55+ for k , v in pairs (define .DiagnosticSeverity ) do
56+ severity_str [v ] = k
57+ end
58+
59+ local function report_pretty (uri , diags )
60+ local path = furi .relpath (furi .decode (uri ))
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+ local severity = severity_str [d .severity ]
73+ print (
74+ (' %s%s:%s:%s%s [%s%s%s] %s %s(%s)%s' ):format (
75+ colors .blue ,
76+ path ,
77+ rstart .line + 1 , -- Use 1-based indexing
78+ rstart .character + 1 , -- Use 1-based indexing
79+ colors .reset ,
80+ severity_colors [severity ],
81+ severity ,
82+ colors .reset ,
83+ d .message ,
84+ colors .magenta ,
85+ d .code ,
86+ colors .reset
87+ )
88+ )
89+ if # lines > 0 then
90+ io.write (' ' , lines [rstart .line + 1 ], ' \n ' )
91+ io.write (' ' , colors .grey , (' ' ):rep (rstart .character ), ' ^' )
92+ if rstart .line == rend .line then
93+ io.write ((' ^' ):rep (rend .character - rstart .character - 1 ))
94+ end
95+ io.write (colors .reset , ' \n ' )
96+ end
97+ end
98+ end
99+
100+ local function clear_line ()
101+ -- Write out empty space to ensure that the previous lien is cleared.
102+ io.write (' \x0D ' , (' ' ):rep (80 ), ' \x0D ' )
103+ end
104+
105+ local function quiet ()
106+ return QUIET or QUIET_WORKER
107+ end
108+
20109function export .runCLI ()
21110 lang (LOCALE )
22111
@@ -65,9 +154,14 @@ function export.runCLI()
65154
66155 client :register (' textDocument/publishDiagnostics' , function (params )
67156 results [params .uri ] = params .diagnostics
157+ -- Do not check QUIET_WORKER here, so results are printed as they are found.
158+ if not QUIET and (CHECK_FORMAT == nil or CHECK_FORMAT == ' pretty' ) then
159+ clear_line ()
160+ report_pretty (params .uri , params .diagnostics )
161+ end
68162 end )
69163
70- if not QUIET then
164+ if not quiet () then
71165 io.write (lang .script (' CLI_CHECK_INITING' ))
72166 end
73167
@@ -89,14 +183,14 @@ function export.runCLI()
89183
90184 -- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
91185 local diagStatus = config .get (rootUri , ' Lua.diagnostics.neededFileStatus' )
92- for diag , status in pairs (diagStatus ) do
186+ for d , status in pairs (diagStatus ) do
93187 if status == ' Any' or status == ' Any!' then
94- diagStatus [diag ] = ' Opened!'
188+ diagStatus [d ] = ' Opened!'
95189 end
96190 end
97- for diag , status in pairs (protoDiag .getDefaultStatus ()) do
191+ for d , status in pairs (protoDiag .getDefaultStatus ()) do
98192 if status == ' Any' or status == ' Any!' then
99- diagStatus [diag ] = ' Opened!'
193+ diagStatus [d ] = ' Opened!'
100194 end
101195 end
102196 config .set (rootUri , ' Lua.diagnostics.neededFileStatus' , diagStatus )
@@ -109,7 +203,7 @@ function export.runCLI()
109203 files .open (uri )
110204 diag .doDiagnostic (uri , true )
111205 -- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
112- if ( os.clock () - lastClock > 0.2 or i == # uris ) and not QUIET then
206+ if not quiet () and ( os.clock () - lastClock > 0.2 or i == # uris ) then
113207 lastClock = os.clock ()
114208 client :update ()
115209 local output = ' \x0D '
@@ -133,8 +227,8 @@ function export.runCLI()
133227 end
134228 end
135229 end
136- if not QUIET then
137- io.write ( ' \x0D ' )
230+ if not quiet () then
231+ clear_line ( )
138232 end
139233 end )
140234
@@ -146,18 +240,21 @@ function export.runCLI()
146240 end
147241 end
148242
149- local outpath = CHECK_OUT_PATH
150- if outpath == nil then
151- outpath = LOGPATH .. ' /check.json'
243+ local outpath = nil
244+
245+ if CHECK_FORMAT == ' json' or CHECK_OUT_PATH then
246+ outpath = CHECK_OUT_PATH or LOGPATH .. ' /check.json'
247+ -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
248+ util .saveFile (outpath , jsonb .beautify (results ))
152249 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 ))
155250
156- if not QUIET then
251+ if not quiet () then
157252 if count == 0 then
158253 print (lang .script (' CLI_CHECK_SUCCESS' ))
254+ elseif outpath then
255+ print (lang .script (' CLI_CHECK_RESULTS_OUTPATH' , count , outpath ))
159256 else
160- print (lang .script (' CLI_CHECK_RESULTS ' , count , outpath ))
257+ print (lang .script (' CLI_CHECK_RESULTS_PRETTY ' , count ))
161258 end
162259 end
163260 return count == 0 and 0 or 1
0 commit comments