Skip to content

Commit

Permalink
chore: backport version tostring function (#63)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Jackie Li <[email protected]>
  • Loading branch information
mikesmithgh and jackielii authored Nov 23, 2023
1 parent 1140ddd commit 5a17f3f
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 58 deletions.
50 changes: 50 additions & 0 deletions lua/kitty-scrollback/backport.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local ksb_health = require('kitty-scrollback.health')

local M = {}

local function backport_version()
if type(vim.version().__tostring) ~= 'function' then
-- NOTE: copied __tostring from
-- https://github.com/neovim/neovim/blob/879617c9bbbacb0d0f778ff6dd53cc7c95794abe/runtime/lua/vim/version.lua

local Version = {}
function Version:__tostring()
local ret = table.concat({ self.major, self.minor, self.patch }, '.')
if self.prerelease then
ret = ret .. '-' .. self.prerelease
end
if self.build and self.build ~= vim.NIL then
ret = ret .. '+' .. self.build
end
return ret
end

setmetatable(vim.version, {
--- Returns the current Nvim version.
__call = function()
local version = vim.fn.api_info().version
-- Workaround: vim.fn.api_info().version reports "prerelease" as a boolean.
version.prerelease = version.prerelease and 'dev' or nil
return setmetatable(version, Version)
end,
})
end
end

local function backport_health()
vim.health.start = vim.health.start or vim.health.report_start
vim.health.info = vim.health.info or vim.health.report_info
vim.health.ok = vim.health.ok or vim.health.report_ok
vim.health.warn = vim.health.warn or vim.health.report_warn
vim.health.error = vim.health.error or vim.health.report_error
end

M.setup = function()
if ksb_health.check_nvim_version('nvim-0.10', true) then
return
end
backport_version()
backport_health()
end

return M
80 changes: 39 additions & 41 deletions lua/kitty-scrollback/health.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local ksb_util = require('kitty-scrollback.util')
---@mod kitty-scrollback.health
local M = {}

Expand All @@ -13,13 +12,8 @@ M.setup = function(private, options)
opts = options ---@diagnostic disable-line: unused-local
end

local start_health = vim.health.start or vim.health.report_start
local ok_health = vim.health.ok or vim.health.report_ok
local warn_health = vim.health.warn or vim.health.report_warn
local error_health = vim.health.error or vim.health.report_error

local function check_kitty_remote_control()
start_health('kitty-scrollback: Kitty remote control')
vim.health.start('kitty-scrollback: Kitty remote control')
local cmd = {
'kitty',
'@',
Expand All @@ -31,7 +25,7 @@ local function check_kitty_remote_control()
local ok = result.code == 0
local code_msg = '`kitty @ ls` exited with code *' .. result.code .. '*'
if ok then
ok_health(code_msg)
vim.health.ok(code_msg)
return true
else
local stderr = result.stderr:gsub('\n', '') or ''
Expand Down Expand Up @@ -59,23 +53,23 @@ local function check_kitty_remote_control()
else
table.insert(advice, 'ERROR Failed to read `allow_remote_control` and `listen_on`')
end
error_health(code_msg .. '\n `' .. stderr .. '` ', advice)
vim.health.error(code_msg .. '\n `' .. stderr .. '` ', advice)
end
return false
end

local function check_has_kitty_data()
start_health('kitty-scrollback: Kitty data')
vim.health.start('kitty-scrollback: Kitty data')
if type(p) == 'table' and next(p.kitty_data) then
ok_health('Kitty data available\n>lua\n' .. vim.inspect(p.kitty_data) .. '\n')
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`'
warn_health('No Kitty data available unable to perform a complete healthcheck', {
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,
Expand All @@ -88,12 +82,12 @@ local function check_clipboard()
local function is_blank(s)
return s:find('^%s*$') ~= nil
end
start_health('kitty-scrollback: clipboard')
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
ok_health('Clipboard tool found: *' .. clipboard_tool .. '*')
vim.health.ok('Clipboard tool found: *' .. clipboard_tool .. '*')
else
warn_health(
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.'
Expand All @@ -102,16 +96,16 @@ local function check_clipboard()
end

local function check_kitty_shell_integration()
start_health('kitty-scrollback: Kitty shell integration')
vim.health.start('kitty-scrollback: Kitty shell integration')
if not next(p or {}) then
error_health('No Kitty data')
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
ok_health('Kitty shell integration is enabled')
vim.health.ok('Kitty shell integration is enabled')
else
warn_health(
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')
Expand All @@ -120,10 +114,10 @@ local function check_kitty_shell_integration()
end

local function check_sed()
start_health('kitty-scrollback: sed')
vim.health.start('kitty-scrollback: sed')
local sed_path = vim.fn.exepath('sed')
if not sed_path or sed_path == '' then
error_health('*sed* : command not found\n')
vim.health.error('*sed* : command not found\n')
return
end

Expand All @@ -149,7 +143,7 @@ local function check_sed()
end
ok = ok and result.code == 0 and result.stdout == 'expected'
if ok then
ok_health(
vim.health.ok(
'`'
.. table.concat(cmd, ' ')
.. '` exited with code *'
Expand All @@ -166,7 +160,7 @@ local function check_sed()
if result_err ~= '' then
result_err = ' `' .. result_err .. '`'
end
error_health(
vim.health.error(
'`'
.. table.concat(cmd, ' ')
.. '` exited with code *'
Expand All @@ -183,60 +177,64 @@ local function check_sed()
end
end

M.check_nvim_version = function(check_only)
-- fallback to older health report calls if using < 0.10
M.check_nvim_version = function(version, check_only)
if not check_only then
start_health('kitty-scrollback: Neovim version 0.10+')
vim.health.start('kitty-scrollback: Neovim version 0.10+')
end
local nvim_version = 'NVIM ' .. ksb_util.nvim_version_tostring()
if vim.fn.has('nvim-0.10') > 0 then
local nvim_version = 'NVIM ' .. tostring(vim.version())
if vim.fn.has(version) > 0 then
if not check_only then
ok_health(nvim_version)
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
error_health(nvim_version, M.advice().nvim_version)
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
start_health('kitty-scrollback: Kitty version 0.29+')
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
ok_health(kitty_version_str)
vim.health.ok(kitty_version_str)
end
return true
else
if not check_only then
error_health(kitty_version_str, M.advice().kitty_version)
vim.health.error(kitty_version_str, M.advice().kitty_version)
end
end
return false
end

local function check_kitty_debug_config()
start_health('kitty-scrollback: 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
ok_health(table.concat(vim.fn.readfile(debug_config_log), '\n '))
vim.health.ok(table.concat(vim.fn.readfile(debug_config_log), '\n '))
else
error_health('cannot read ' .. debug_config_log)
vim.health.error('cannot read ' .. debug_config_log)
end
else
local stderr = result.stderr:gsub('\n', '') or ''
error_health(stderr)
vim.health.error(stderr)
end
end

Expand All @@ -261,11 +259,11 @@ local function check_kitty_scrollback_nvim_version()
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 warn_health
local health_fn = not version_found and vim.health.warn
or function(msg)
ok_health(' ' .. msg)
vim.health.ok(' ' .. msg)
end
start_health('kitty-scrollback: kitty-scrollback.nvim version')
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
Expand All @@ -283,7 +281,7 @@ end

M.check = function()
if
M.check_nvim_version() -- always check first to avoid undefined calls in versions < 0.10
M.check_nvim_version('nvim-0.10') -- always check first to avoid undefined calls in versions < 0.10
and check_kitty_scrollback_nvim_version()
and check_kitty_remote_control()
and check_has_kitty_data()
Expand Down
10 changes: 6 additions & 4 deletions lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ local ksb_util
local ksb_autocmds
---@module 'kitty-scrollback.health'
local ksb_health
---@module 'kitty-scrollback.backport'
local ksb_backport

local M = {}

Expand Down Expand Up @@ -247,6 +249,7 @@ local function load_requires()
ksb_util = require('kitty-scrollback.util')
ksb_autocmds = require('kitty-scrollback.autocommands')
ksb_health = require('kitty-scrollback.health')
ksb_backport = require('kitty-scrollback.backport')
end

---Setup and configure kitty-scrollback.nvim
Expand All @@ -267,15 +270,16 @@ M.setup = function(kitty_data_str)
local user_opts = config_fn and config_fn(p.kitty_data) or {}
opts = vim.tbl_deep_extend('force', default_opts, user_opts)

ksb_backport.setup()
ksb_health.setup(p, opts)
if opts.checkhealth then
vim.o.foldenable = false
vim.cmd.checkhealth('kitty-scrollback')
return
end
if not ksb_health.check_nvim_version(true) then
if not ksb_health.check_nvim_version('nvim-0.10', true) then
local prompt_msg = 'kitty-scrollback.nvim: Fatal error, on version NVIM '
.. ksb_util.nvim_version_tostring()
.. tostring(vim.version())
.. '. '
.. table.concat(ksb_health.advice().nvim_version)
local response = vim.fn.confirm(prompt_msg, '&Quit\n&Continue')
Expand Down Expand Up @@ -386,8 +390,6 @@ M.launch = function()
-- increase the number of columns temporary so that the width is used during the
-- terminal command kitty @ get-text. this avoids hard wrapping lines to the
-- current window size. Note: a larger min_cols appears to impact performance
-- do not worry about setting vim.o.columns back to original value that is taken
-- care of when we trigger kitty to send a SIGWINCH to the nvim process
local min_cols = 300
p.orig_columns = vim.o.columns
if vim.o.columns < min_cols then
Expand Down
13 changes: 0 additions & 13 deletions lua/kitty-scrollback/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,6 @@ M.remove_process_exited = function()
return false
end

-- nvim 0.10+ has tostring builtin but need this for previous versions during healthcheck
M.nvim_version_tostring = function()
local nvim_ver = vim.version()
local ret = table.concat({ nvim_ver.major, nvim_ver.minor, nvim_ver.patch }, '.')
if nvim_ver.prerelease then
ret = ret .. '-' .. nvim_ver.prerelease
end
if nvim_ver.build and nvim_ver.build ~= vim.NIL then
ret = ret .. '+' .. nvim_ver.build
end
return ret
end

M.restore_and_redraw = function()
if p.orig_columns then
vim.o.columns = p.orig_columns
Expand Down

0 comments on commit 5a17f3f

Please sign in to comment.