@@ -17,6 +17,100 @@ require 'vm'
17
17
18
18
local export = {}
19
19
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 pwd
60
+
61
+ --- @param path string
62
+ --- @return string
63
+ local function relpath (path )
64
+ if not pwd then
65
+ pwd = furi .decode (furi .encode (fs .current_path ():string ()))
66
+ end
67
+ if pwd and path :sub (1 , # pwd ) == pwd then
68
+ path = path :sub (# pwd + 2 )
69
+ end
70
+ return path
71
+ end
72
+
73
+ local function report_pretty (uri , diags )
74
+ local path = relpath (furi .decode (uri ))
75
+
76
+ local lines = {} --- @type string[]
77
+ pcall (function ()
78
+ for line in io.lines (path ) do
79
+ table.insert (lines , line )
80
+ end
81
+ end )
82
+
83
+ for _ , d in ipairs (diags ) do
84
+ local rstart = d .range .start
85
+ local rend = d .range [' end' ]
86
+ local severity = severity_str [d .severity ]
87
+ print (
88
+ (' %s%s:%s:%s%s [%s%s%s] %s %s(%s)%s' ):format (
89
+ colors .blue ,
90
+ path ,
91
+ rstart .line + 1 , -- Use 1-based indexing
92
+ rstart .character + 1 , -- Use 1-based indexing
93
+ colors .reset ,
94
+ severity_colors [severity ],
95
+ severity ,
96
+ colors .reset ,
97
+ d .message ,
98
+ colors .magenta ,
99
+ d .code ,
100
+ colors .reset
101
+ )
102
+ )
103
+ if # lines > 0 then
104
+ io.write (' ' , lines [rstart .line + 1 ], ' \n ' )
105
+ io.write (' ' , colors .grey , (' ' ):rep (rstart .character ), ' ^' )
106
+ if rstart .line == rend .line then
107
+ io.write ((' ^' ):rep (rend .character - rstart .character - 1 ))
108
+ end
109
+ io.write (colors .reset , ' \n ' )
110
+ end
111
+ end
112
+ end
113
+
20
114
local function clear_line ()
21
115
-- Write out empty space to ensure that the previous lien is cleared.
22
116
io.write (' \x0D ' , (' ' ):rep (80 ), ' \x0D ' )
@@ -86,6 +180,7 @@ function export.runCLI()
86
180
87
181
local numThreads = tonumber (NUM_THREADS or 1 )
88
182
local threadId = tonumber (THREAD_ID or 1 )
183
+ local quiet = QUIET or numThreads > 1
89
184
90
185
if type (CHECK_WORKER ) ~= ' string' then
91
186
print (lang .script (' CLI_CHECK_ERROR_TYPE' , type (CHECK_WORKER )))
@@ -127,9 +222,13 @@ function export.runCLI()
127
222
128
223
client :register (' textDocument/publishDiagnostics' , function (params )
129
224
results [params .uri ] = params .diagnostics
225
+ if not QUIET and (CHECK_FORMAT == nil or CHECK_FORMAT == ' pretty' ) then
226
+ clear_line ()
227
+ report_pretty (params .uri , params .diagnostics )
228
+ end
130
229
end )
131
230
132
- if not QUIET then
231
+ if not quiet then
133
232
io.write (lang .script (' CLI_CHECK_INITING' ))
134
233
end
135
234
@@ -153,15 +252,15 @@ function export.runCLI()
153
252
diag .doDiagnostic (uri , true )
154
253
-- Print regularly but always print the last entry to ensure
155
254
-- that logs written to files don't look incomplete.
156
- if not QUIET and (os.clock () - lastClock > 0.2 or i == # uris ) then
255
+ if not quiet and (os.clock () - lastClock > 0.2 or i == # uris ) then
157
256
lastClock = os.clock ()
158
257
client :update ()
159
258
report_progress (i , max , results )
160
259
end
161
260
end
162
261
end
163
- if not QUIET then
164
- io.write ( ' \x0D ' )
262
+ if not quiet then
263
+ clear_line ( )
165
264
end
166
265
end )
167
266
@@ -173,16 +272,21 @@ function export.runCLI()
173
272
end
174
273
end
175
274
176
- local outpath = CHECK_OUT_PATH or LOGPATH .. ' /check.json '
275
+ local outpath = nil
177
276
178
- -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
179
- util .saveFile (outpath , jsonb .beautify (results ))
277
+ if CHECK_FORMAT == ' json' or CHECK_OUT_PATH then
278
+ outpath = CHECK_OUT_PATH or LOGPATH .. ' /check.json'
279
+ -- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
280
+ util .saveFile (outpath , jsonb .beautify (results ))
281
+ end
180
282
181
- if not QUIET then
283
+ if not quiet then
182
284
if count == 0 then
183
285
print (lang .script (' CLI_CHECK_SUCCESS' ))
286
+ elseif outpath then
287
+ print (lang .script (' CLI_CHECK_RESULTS_OUTPATH' , count , outpath ))
184
288
else
185
- print (lang .script (' CLI_CHECK_RESULTS ' , count , outpath ))
289
+ print (lang .script (' CLI_CHECK_RESULTS_PRETTY ' , count ))
186
290
end
187
291
end
188
292
return count == 0 and 0 or 1
0 commit comments