Skip to content

Commit b653f64

Browse files
committed
feat: add --check_format=json|pretty
Adds a new CLI option --check_format which accepts the values: 'json' (current behaviour), and a new 'pretty' value which prints a colorized human readable report to stdout, similar to common compilers and linters. Results are printed with color unless NO_COLOR is defined in the users environment, as per https://no-color.org. If --check_out_path is provided, then the results are always saved to file regardless of the --check_format value.
1 parent 5275a75 commit b653f64

File tree

7 files changed

+108
-16
lines changed

7 files changed

+108
-16
lines changed

locale/en-us/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'Diagnosis completed, no problems found'
649649
CLI_CHECK_PROGRESS =
650650
'Found {} problems in {} files'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'Diagnosis complete, {} problems found, see {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'Diagnosis complete, {} problems found'
653655
CLI_CHECK_MULTIPLE_WORKERS =
654656
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
655657
CLI_DOC_INITING =

locale/ja-jp/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,10 @@ CLI_CHECK_SUCCESS =
649649
'診断が完了しました。問題は見つかりませんでした'
650650
CLI_CHECK_PROGRESS =
651651
'{} ファイルに渡り、{} 個の問題が発見されました'
652-
CLI_CHECK_RESULTS =
652+
CLI_CHECK_RESULTS_OUTPATH =
653653
'診断が完了しました。{} 個の問題が発見されました。詳しくは {} をご確認ください'
654+
CLI_CHECK_RESULTS_PRETTY =
655+
'診断が完了しました。{} 個の問題が発見されました'
654656
CLI_CHECK_MULTIPLE_WORKERS =
655657
'{} 個のワーカータスクを開始しているため、進行状況の出力が無効になります。完了まで数分かかることがあります。'
656658
CLI_DOC_INITING =

locale/pt-br/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'Diagnóstico completo, nenhum problema encontrado'
649649
CLI_CHECK_PROGRESS = -- TODO: need translate!
650650
'Found {} problems in {} files'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'Diagnóstico completo, {} problemas encontrados, veja {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'Diagnóstico completo, {} problemas encontrados'
653655
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
654656
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
655657
CLI_DOC_INITING = -- TODO: need translate!

locale/zh-cn/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'诊断完成,没有发现问题'
649649
CLI_CHECK_PROGRESS =
650650
'检测到问题 {} 在文件 {} 中'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'诊断完成,共有 {} 个问题,请查看 {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'诊断完成,共有 {} 个问题'
653655
CLI_CHECK_MULTIPLE_WORKERS =
654656
'开启 {} 个工作任务,进度输出将会被禁用。这可能会花费几分钟。'
655657
CLI_DOC_INITING =

locale/zh-tw/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'診斷完成,沒有發現問題'
649649
CLI_CHECK_PROGRESS = -- TODO: need translate!
650650
'Found {} problems in {} files'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'診斷完成,共有 {} 個問題,請查看 {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'診斷完成,共有 {} 個問題'
653655
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
654656
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
655657
CLI_DOC_INITING = -- TODO: need translate!

script/cli/check_worker.lua

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,77 @@ require 'vm'
1717

1818
local 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+
2091
function 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

script/global.d.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ CHECKLEVEL = 'Warning'
6969
---@type string|nil
7070
CHECK_OUT_PATH = ''
7171

72+
---@type string | 'json' | 'pretty'
73+
CHECK_FORMAT = 'pretty'
74+
7275
---@type 'trace' | 'debug' | 'info' | 'warn' | 'error'
7376
LOGLEVEL = 'warn'
7477

0 commit comments

Comments
 (0)