-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhealth.lua
404 lines (382 loc) · 13.4 KB
/
health.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
---@mod kitty-scrollback.health
local M = {}
---@type KsbPrivate
local p
---@type KsbOpts
local opts ---@diagnostic disable-line: unused-local
M.setup = function(private, options)
p = private
opts = options ---@diagnostic disable-line: unused-local
end
local function check_kitty_remote_control()
vim.health.start('kitty-scrollback: Kitty remote control')
local cmd = {
'kitty',
'@',
'ls',
}
local sys_opts = {}
local proc = vim.system(cmd, sys_opts or {})
local result = proc:wait()
local ok = result.code == 0
local code_msg = '`kitty @ ls` exited with code *' .. result.code .. '*'
if ok then
vim.health.ok(code_msg)
return true
else
local stderr = result.stderr:gsub('\n', '') or ''
local msg = {}
if stderr:match('.*/dev/tty.*') then
msg = M.advice().listen_on
end
if stderr:match('.*allow_remote_control.*') then
msg = M.advice().allow_remote_control
end
local advice = {
table.concat(msg, '\n'),
}
local kitty_opts = {}
if type(p) == 'table' and next(p.kitty_data) then
kitty_opts = p.kitty_data.kitty_opts
table.insert(
advice,
table.concat({
'*allow_remote_control* and *listen_on* are currently configured as:',
' `allow_remote_control ' .. kitty_opts.allow_remote_control .. '`',
' `listen_on ' .. kitty_opts.listen_on .. '`',
}, '\n')
)
else
table.insert(advice, 'ERROR Failed to read `allow_remote_control` and `listen_on`')
end
vim.health.error(code_msg .. '\n `' .. stderr .. '` ', advice)
end
return false
end
local function check_has_kitty_data()
vim.health.start('kitty-scrollback: Kitty data')
if type(p) == 'table' and next(p.kitty_data) then
vim.health.ok('Kitty data available\n>lua\n' .. vim.inspect(p.kitty_data) .. '\n')
return true
else
local kitty_scrollback_kitten =
vim.api.nvim_get_runtime_file('python/kitty_scrollback_nvim.py', false)[1]
local checkhealth_command = '`kitty @ kitten '
.. kitty_scrollback_kitten
.. ' --config ksb_builtin_checkhealth`'
vim.health.warn('No Kitty data available unable to perform a complete healthcheck', {
'Add the config options `checkhealth = true` to your *config* or execute the command `:KittyScrollbackCheckHealth` '
.. 'to run `checkhealth` within the context of a Kitten',
checkhealth_command,
})
end
return false
end
local function check_clipboard()
local function is_blank(s)
return s:find('^%s*$') ~= nil
end
vim.health.start('kitty-scrollback: clipboard')
local clipboard_tool = vim.fn['provider#clipboard#Executable']() -- copied from health.lua
if vim.fn.has('clipboard') > 0 and not is_blank(clipboard_tool) then
vim.health.ok('Clipboard tool found: *' .. clipboard_tool .. '*')
else
vim.health.warn(
'Neovim does not have a clipboard provider.\n Some functionality will not work when there is no clipboard '
.. 'provider, such as copying Kitty scrollback buffer text to the system clipboard.',
'See `:help` |provider-clipboard| for more information on enabling system clipboard integration.'
)
end
end
local function check_kitty_shell_integration()
vim.health.start('kitty-scrollback: Kitty shell integration')
if not next(p or {}) then
vim.health.error('No Kitty data')
return
end
-- use last_cmd_output because it is valid and requires shell integration
if M.is_valid_extent_keyword('last_cmd_output') then
vim.health.ok('Kitty shell integration is enabled')
else
vim.health.warn(
'Kitty shell integration is disabled and/or `no-prompt-mark` is set.\n Some functionality will not work when Kitty shell '
.. 'integration is disabled or `no-prompt-mark` is set, such as capturing the last command output.',
table.concat(M.advice().kitty_shell_integration, '\n')
)
end
end
local function check_sed()
vim.health.start('kitty-scrollback: sed')
local sed_path = vim.fn.exepath('sed')
if not sed_path or sed_path == '' then
vim.health.error('*sed* : command not found\n')
return
end
local esc = vim.fn.eval([["\e"]])
local cmd = {
'sed',
'-E',
'-e',
's/$/' .. esc .. '[0m/g',
'-e',
's/' .. esc .. '\\[\\?25.' .. esc .. '\\[.*;.*H' .. esc .. '\\[.*//g',
}
local ok, sed_proc = pcall(vim.system, cmd, {
stdin = 'expected' .. esc .. '[?25h' .. esc .. '[1;1H' .. esc .. '[notexpected',
})
local result = {}
if ok then
result = sed_proc:wait()
else
result.code = -999
result.stdout = ''
result.stderr = sed_proc
end
ok = ok and result.code == 0 and result.stdout == 'expected'
if ok then
vim.health.ok(
'`'
.. table.concat(cmd, ' ')
.. '` exited with code *'
.. result.code
.. '* and stdout `'
.. result.stdout
.. '`\n'
.. ' `sed: '
.. sed_path
.. '`'
)
else
local result_err = result.stderr:gsub('\n', '')
if result_err ~= '' then
result_err = ' `' .. result_err .. '`'
end
vim.health.error(
'`'
.. table.concat(cmd, ' ')
.. '` exited with code *'
.. result.code
.. '* and stdout `'
.. result.stdout
.. '` (should be `expected`)\n'
.. result_err
.. '`\n'
.. ' `sed: '
.. sed_path
.. '`'
)
end
end
M.check_nvim_version = function(version, check_only)
if not check_only then
vim.health.start('kitty-scrollback: Neovim version 0.9+')
end
local nvim_version = 'NVIM ' .. tostring(vim.version())
if vim.fn.has(version) > 0 then
if not check_only then
vim.health.ok(nvim_version)
if vim.fn.has('nvim-0.10') <= 0 then
vim.health.info(
'If you are using a version of nvim that is less than 0.10, then formatting on this checkhealth may be malformed'
)
end
end
return true
else
if not check_only then
vim.health.error(nvim_version, M.advice().nvim_version)
end
end
return false
end
M.check_kitty_version = function(check_only)
if not check_only then
vim.health.start('kitty-scrollback: Kitty version 0.29+')
end
local kitty_version = p.kitty_data.kitty_version
local kitty_version_str = 'kitty ' .. table.concat(kitty_version, '.')
if vim.version.cmp(kitty_version, { 0, 29, 0 }) >= 0 then
if not check_only then
vim.health.ok(kitty_version_str)
end
return true
else
if not check_only then
vim.health.error(kitty_version_str, M.advice().kitty_version)
end
end
return false
end
local function check_kitty_debug_config()
vim.health.start('kitty-scrollback: Kitty debug config')
local kitty_debug_config_kitten =
vim.api.nvim_get_runtime_file('python/kitty_debug_config.py', false)[1]
local debug_config_log = vim.fn.stdpath('data') .. '/kitty-scrollback.nvim/debug_config.log'
local result =
vim.system({ 'kitty', '@', 'kitten', kitty_debug_config_kitten, debug_config_log }):wait()
if result.code == 0 then
if vim.fn.filereadable(debug_config_log) then
vim.health.ok(table.concat(vim.fn.readfile(debug_config_log), '\n '))
else
vim.health.error('cannot read ' .. debug_config_log)
end
else
local stderr = result.stderr:gsub('\n', '') or ''
vim.health.error(stderr)
end
end
local function check_kitty_scrollback_nvim_version()
local current_version = nil
local tag_cmd = { 'git', 'describe', '--exact-match', '--tags' }
local ksb_dir =
vim.fn.fnamemodify(vim.api.nvim_get_runtime_file('lua/kitty-scrollback', false)[1], ':h:h')
local tag_cmd_result = vim.system(tag_cmd, { cwd = ksb_dir }):wait()
if tag_cmd_result.code == 0 then
current_version = tag_cmd_result.stdout
else
local commit_cmd = { 'git', 'rev-parse', '--short', 'HEAD' }
local commit_cmd_result = vim.system(commit_cmd, { cwd = ksb_dir }):wait()
if commit_cmd_result.code == 0 then
current_version = commit_cmd_result.stdout
end
end
local version_found = current_version and current_version ~= ''
local header = '*kitty-scrollback.nvim* @ '
.. (
version_found and '`' .. current_version:gsub('%s$', '`\n') ---@diagnostic disable-line: need-check-nil
or 'ERROR failed to determine version\n'
)
local health_fn = not version_found and vim.health.warn
or function(msg)
vim.health.ok(' ' .. msg)
end
vim.health.start('kitty-scrollback: kitty-scrollback.nvim version')
health_fn([[ `|`\___/`|` ]] .. header .. [[
=) `^`Y`^` (=
\ *^* / If you have any issues or questions using *kitty-scrollback.nvim* then
` )=*=( ` please create an issue at
/ \ https://github.com/mikesmithgh/kitty-scrollback.nvim/issues and
| | provide the `KittyScrollbackCheckHealth` report.
/| | | |\
\| | `|`_`|`/\
/_// ___/ *Bonus* *points* *for* *cat* *memes*
\_) ]])
-- Always consider true even if git version is not found to provide additional health checks
return true
end
M.check = function()
if
M.check_nvim_version('nvim-0.9')
and check_kitty_scrollback_nvim_version()
and check_kitty_remote_control()
and check_has_kitty_data()
and M.check_kitty_version()
then
check_clipboard()
check_kitty_shell_integration()
check_sed()
check_kitty_debug_config()
end
end
---@class KsbAdvice
---@field allow_remote_control table
---@field listen_on table
---@field kitty_shell_integration table
---@field nvim_version table
---@field kitty_version table
---@return KsbAdvice
M.advice = function()
local extent = 'nil'
local ansi = 'nil'
local clear_selection = 'nil'
if opts then
extent = opts.kitty_get_text.extent
ansi = tostring(opts.kitty_get_text.ansi)
clear_selection = tostring(opts.kitty_get_text.clear_selection)
end
local shell_integration = 'nil'
if p then
shell_integration = table.concat(p.kitty_data.kitty_opts.shell_integration, ' ')
end
return {
nvim_version = {
'Neovim version 0.9 or greater is required to work with kitty-scrollback.nvim',
},
kitty_version = {
'Kitty version 0.29 or greater is required to work with kitty-scrollback.nvim',
},
allow_remote_control = {
'Kitty must be configured to allow remote control connections. Add the configuration',
'*allow_remote_control* to Kitty. For example, `allow_remote_control socket-only`',
'Changing *allow_remote_control* by reloading the config is not supported so you must ',
'completely close and reopen Kitty for the change to take effect.',
'',
'Compatible values with kitty-scrollback.nvim for the option *allow_remote_control* are',
'`yes`, `socket`, or `socket-only`.',
'',
'See https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.allow_remote_control for additional',
'information on configuring the *allow_remote_control* option.',
'',
},
listen_on = {
'Kitty must be configured to listen on a Unix socket for remote control connections.',
'Add the configuration *listen_on* to Kitty. For example, `listen_on unix:/tmp/mykitty`',
'Changing *listen_on* by reloading the config is not supported so you must completely',
'close and reopen Kitty for the change to take effect.',
'',
'See https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.listen_on for additional information',
'on configuring the *listen_on* option.',
'',
'If *listen_on* is properly configured, check that the option *allow_remote_control* is',
'set to either `yes`, `socket`, or `socket-only`.',
'',
'See https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.allow_remote_control for additional',
'information on configuring the *allow_remote_control* option.',
'',
},
kitty_shell_integration = {
'Remove `disabled` and `no-prompt-mark` from *shell_integration* if present in your Kitty configuration.',
'',
'Current *kitty_get_text* options:',
'` opts = {`',
'` kitty_get_text = {`',
[[` extent = ']] .. extent .. [[',`]],
[[` ansi = ]] .. ansi .. [[,`]],
[[` clear_selection = ]] .. clear_selection .. [[,`]],
'` },`',
'` }`',
'',
'See https://sw.kovidgoyal.net/kitty/remote-control/#cmdoption-kitty-get-text-extent for',
'more information on *extent* or run `kitty @ get-text --help`',
'',
'Current *shell_integration* options:',
[[` KITTY_SHELL_INTEGRATION=']] .. shell_integration .. [['`]],
'',
'See https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.shell_integration for more information',
'on *shell_integration*',
},
}
end
---@param extent string
M.is_valid_extent_keyword = function(extent)
local valid = false
local standard = { 'screen', 'all', 'selection' }
local standard_extent = vim.tbl_filter(function(e)
return e == extent:lower()
end, standard)
if #standard_extent > 0 then
return true
end
local shell_integration = p.kitty_data.kitty_opts.shell_integration
for _, keyword in pairs(shell_integration) do
local k = keyword:lower()
if k == 'disabled' or k == 'no-prompt-mark' then
return false
end
if k == 'enabled' then
valid = true
end
end
return valid
end
return M