refactor: remove fn_bool

It's better to use vim.fn directly instead of creating minor
abstractions like fn_bool.
This commit is contained in:
dundargoc 2024-04-02 12:36:13 +02:00 committed by dundargoc
parent 7560aee595
commit 9dd112dd48
6 changed files with 48 additions and 65 deletions

View File

@ -1,18 +1,6 @@
local M = {} local M = {}
local health = require('vim.health') local health = require('vim.health')
local fn_bool = function(key)
return function(...)
return vim.fn[key](...) == 1
end
end
local has = fn_bool('has')
local executable = fn_bool('executable')
local empty = fn_bool('empty')
local filereadable = fn_bool('filereadable')
local filewritable = fn_bool('filewritable')
local shell_error = function() local shell_error = function()
return vim.v.shell_error ~= 0 return vim.v.shell_error ~= 0
end end
@ -62,11 +50,11 @@ local function check_config()
local init_lua = vim.fn.stdpath('config') .. '/init.lua' local init_lua = vim.fn.stdpath('config') .. '/init.lua'
local init_vim = vim.fn.stdpath('config') .. '/init.vim' local init_vim = vim.fn.stdpath('config') .. '/init.vim'
local vimrc = empty(vim.env.MYVIMRC) and init_lua or vim.env.MYVIMRC local vimrc = vim.env.MYVIMRC or init_lua
if not filereadable(vimrc) and not filereadable(init_vim) then if vim.fn.filereadable(vimrc) == 0 and vim.fn.filereadable(init_vim) == 0 then
ok = false ok = false
local has_vim = filereadable(vim.fn.expand('~/.vimrc')) local has_vim = vim.fn.filereadable(vim.fn.expand('~/.vimrc')) == 1
health.warn( health.warn(
('%s user config file: %s'):format( ('%s user config file: %s'):format(
-1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable', -1 == vim.fn.getfsize(vimrc) and 'Missing' or 'Unreadable',
@ -77,7 +65,7 @@ local function check_config()
end end
-- If $VIM is empty we don't care. Else make sure it is valid. -- If $VIM is empty we don't care. Else make sure it is valid.
if not empty(vim.env.VIM) and not filereadable(vim.env.VIM .. '/runtime/doc/nvim.txt') then if vim.env.VIM and vim.fn.filereadable(vim.env.VIM .. '/runtime/doc/nvim.txt') == 0 then
ok = false ok = false
health.error('$VIM is invalid: ' .. vim.env.VIM) health.error('$VIM is invalid: ' .. vim.env.VIM)
end end
@ -121,17 +109,17 @@ local function check_config()
local writeable = true local writeable = true
local shadaopt = vim.fn.split(vim.o.shada, ',') local shadaopt = vim.fn.split(vim.o.shada, ',')
local shadafile = ( local shadafile = (
empty(vim.o.shada) and vim.o.shada vim.o.shada == '' and vim.o.shada
or vim.fn.substitute(vim.fn.matchstr(shadaopt[#shadaopt], '^n.\\+'), '^n', '', '') or vim.fn.substitute(vim.fn.matchstr(shadaopt[#shadaopt], '^n.\\+'), '^n', '', '')
) )
shadafile = ( shadafile = (
empty(vim.o.shadafile) vim.o.shadafile == ''
and (empty(shadafile) and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fn.expand( and (shadafile == '' and vim.fn.stdpath('state') .. '/shada/main.shada' or vim.fn.expand(
shadafile shadafile
)) ))
or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile) or (vim.o.shadafile == 'NONE' and '' or vim.o.shadafile)
) )
if not empty(shadafile) and empty(vim.fn.glob(shadafile)) then if shadafile ~= '' and vim.fn.glob(shadafile) == '' then
-- Since this may be the first time Nvim has been run, try to create a shada file. -- Since this may be the first time Nvim has been run, try to create a shada file.
if not pcall(vim.cmd.wshada) then if not pcall(vim.cmd.wshada) then
writeable = false writeable = false
@ -139,12 +127,15 @@ local function check_config()
end end
if if
not writeable not writeable
or (not empty(shadafile) and (not filereadable(shadafile) or not filewritable(shadafile))) or (
shadafile ~= ''
and (vim.fn.filereadable(shadafile) == 0 or vim.fn.filewritable(shadafile) ~= 1)
)
then then
ok = false ok = false
health.error( health.error(
'shada file is not ' 'shada file is not '
.. ((not writeable or filereadable(shadafile)) and 'writeable' or 'readable') .. ((not writeable or vim.fn.filereadable(shadafile) == 1) and 'writeable' or 'readable')
.. ':\n' .. ':\n'
.. shadafile .. shadafile
) )
@ -160,7 +151,7 @@ local function check_performance()
-- Check buildtype -- Check buildtype
local buildtype = vim.fn.matchstr(vim.fn.execute('version'), [[\v\cbuild type:?\s*[^\n\r\t ]+]]) local buildtype = vim.fn.matchstr(vim.fn.execute('version'), [[\v\cbuild type:?\s*[^\n\r\t ]+]])
if empty(buildtype) then if buildtype == '' then
health.error('failed to get build type from :version') health.error('failed to get build type from :version')
elseif vim.regex([[\v(MinSizeRel|Release|RelWithDebInfo)]]):match_str(buildtype) then elseif vim.regex([[\v(MinSizeRel|Release|RelWithDebInfo)]]):match_str(buildtype) then
health.ok(buildtype) health.ok(buildtype)
@ -196,12 +187,12 @@ local function check_rplugin_manifest()
local require_update = false local require_update = false
local handle_path = function(path) local handle_path = function(path)
local python_glob = vim.fn.glob(path .. '/rplugin/python*', true, true) local python_glob = vim.fn.glob(path .. '/rplugin/python*', true, true)
if empty(python_glob) then if python_glob == '' then
return return
end end
local python_dir = python_glob[1] local python_dir = python_glob[1]
local python_version = vim.fn.fnamemodify(python_dir, ':t') local python_version = vim.fs.basename(python_dir)
local scripts = vim.fn.glob(python_dir .. '/*.py', true, true) local scripts = vim.fn.glob(python_dir .. '/*.py', true, true)
vim.list_extend(scripts, vim.fn.glob(python_dir .. '/*/__init__.py', true, true)) vim.list_extend(scripts, vim.fn.glob(python_dir .. '/*/__init__.py', true, true))
@ -213,12 +204,12 @@ local function check_rplugin_manifest()
script = vim.fn.tr(vim.fn.fnamemodify(script, ':h'), '\\', '/') script = vim.fn.tr(vim.fn.fnamemodify(script, ':h'), '\\', '/')
end end
if not existing_rplugins[script] then if not existing_rplugins[script] then
local msg = vim.fn.printf('"%s" is not registered.', vim.fn.fnamemodify(path, ':t')) local msg = vim.fn.printf('"%s" is not registered.', vim.fs.basename(path))
if python_version == 'pythonx' then if python_version == 'pythonx' then
if not has('python3') then if vim.fn.has('python3') == 0 then
msg = msg .. ' (python3 not available)' msg = msg .. ' (python3 not available)'
end end
elseif not has(python_version) then elseif vim.fn.has(python_version) == 0 then
msg = msg .. vim.fn.printf(' (%s not available)', python_version) msg = msg .. vim.fn.printf(' (%s not available)', python_version)
else else
require_update = true require_update = true
@ -232,7 +223,7 @@ local function check_rplugin_manifest()
end end
end end
for _, path in ipairs(vim.fn.map(vim.fn.split(vim.o.runtimepath, ','), 'resolve(v:val)')) do for _, path in ipairs(vim.fn.map(vim.split(vim.o.runtimepath, ','), 'resolve(v:val)')) do
handle_path(path) handle_path(path)
end end
@ -244,7 +235,7 @@ local function check_rplugin_manifest()
end end
local function check_tmux() local function check_tmux()
if empty(vim.env.TMUX) or not executable('tmux') then if not vim.env.TMUX or vim.fn.executable('tmux') == 0 then
return return
end end
@ -255,7 +246,7 @@ local function check_tmux()
if shell_error() then if shell_error() then
health.error('command failed: ' .. cmd .. '\n' .. out) health.error('command failed: ' .. cmd .. '\n' .. out)
return 'error' return 'error'
elseif empty(val) then elseif val == '' then
cmd = 'tmux show-option -qvgs ' .. option -- try session scope cmd = 'tmux show-option -qvgs ' .. option -- try session scope
out = vim.fn.system(vim.fn.split(cmd)) out = vim.fn.system(vim.fn.split(cmd))
val = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g') val = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
@ -274,7 +265,7 @@ local function check_tmux()
{ 'set escape-time in ~/.tmux.conf:\nset-option -sg escape-time 10', suggest_faq } { 'set escape-time in ~/.tmux.conf:\nset-option -sg escape-time 10', suggest_faq }
local tmux_esc_time = get_tmux_option('escape-time') local tmux_esc_time = get_tmux_option('escape-time')
if tmux_esc_time ~= 'error' then if tmux_esc_time ~= 'error' then
if empty(tmux_esc_time) then if tmux_esc_time == '' then
health.error('`escape-time` is not set', suggestions) health.error('`escape-time` is not set', suggestions)
elseif tonumber(tmux_esc_time) > 300 then elseif tonumber(tmux_esc_time) > 300 then
health.error('`escape-time` (' .. tmux_esc_time .. ') is higher than 300ms', suggestions) health.error('`escape-time` (' .. tmux_esc_time .. ') is higher than 300ms', suggestions)
@ -286,7 +277,7 @@ local function check_tmux()
-- check focus-events -- check focus-events
local tmux_focus_events = get_tmux_option('focus-events') local tmux_focus_events = get_tmux_option('focus-events')
if tmux_focus_events ~= 'error' then if tmux_focus_events ~= 'error' then
if empty(tmux_focus_events) or tmux_focus_events ~= 'on' then if tmux_focus_events == '' or tmux_focus_events ~= 'on' then
health.warn( health.warn(
"`focus-events` is not enabled. |'autoread'| may not work.", "`focus-events` is not enabled. |'autoread'| may not work.",
{ '(tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf:\nset-option -g focus-events on' } { '(tmux 1.9+ only) Set `focus-events` in ~/.tmux.conf:\nset-option -g focus-events on' }
@ -301,7 +292,7 @@ local function check_tmux()
local cmd = 'tmux show-option -qvg default-terminal' local cmd = 'tmux show-option -qvg default-terminal'
local out = vim.fn.system(vim.fn.split(cmd)) local out = vim.fn.system(vim.fn.split(cmd))
local tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g') local tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
if empty(tmux_default_term) then if tmux_default_term == '' then
cmd = 'tmux show-option -qvgs default-terminal' cmd = 'tmux show-option -qvgs default-terminal'
out = vim.fn.system(vim.fn.split(cmd)) out = vim.fn.system(vim.fn.split(cmd))
tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g') tmux_default_term = vim.fn.substitute(out, [[\v(\s|\r|\n)]], '', 'g')
@ -341,7 +332,7 @@ local function check_tmux()
end end
local function check_terminal() local function check_terminal()
if not executable('infocmp') then if vim.fn.executable('infocmp') == 0 then
return return
end end
@ -354,13 +345,12 @@ local function check_terminal()
if if
shell_error() shell_error()
and ( and (
not has('win32') vim.fn.has('win32') == 0
or empty( or vim.fn.matchstr(
vim.fn.matchstr(
out, out,
[[infocmp: couldn't open terminfo file .\+\%(conemu\|vtpcon\|win32con\)]] [[infocmp: couldn't open terminfo file .\+\%(conemu\|vtpcon\|win32con\)]]
) )
) == ''
) )
then then
health.error('command failed: ' .. cmd .. '\n' .. out) health.error('command failed: ' .. cmd .. '\n' .. out)
@ -368,14 +358,14 @@ local function check_terminal()
health.info( health.info(
vim.fn.printf( vim.fn.printf(
'key_backspace (kbs) terminfo entry: `%s`', 'key_backspace (kbs) terminfo entry: `%s`',
(empty(kbs_entry) and '? (not found)' or kbs_entry) (kbs_entry == '' and '? (not found)' or kbs_entry)
) )
) )
health.info( health.info(
vim.fn.printf( vim.fn.printf(
'key_dc (kdch1) terminfo entry: `%s`', 'key_dc (kdch1) terminfo entry: `%s`',
(empty(kbs_entry) and '? (not found)' or kdch1_entry) (kbs_entry == '' and '? (not found)' or kdch1_entry)
) )
) )
end end

View File

@ -1,5 +1,4 @@
local health = vim.health local health = vim.health
local executable = health.executable
local M = {} local M = {}
@ -8,8 +7,8 @@ function M.check()
if if
os.getenv('TMUX') os.getenv('TMUX')
and executable('tmux') and vim.fn.executable('tmux') == 1
and executable('pbpaste') and vim.fn.executable('pbpaste') == 1
and not health.cmd_ok('pbpaste') and not health.cmd_ok('pbpaste')
then then
local tmux_version = string.match(vim.fn.system('tmux -V'), '%d+%.%d+') local tmux_version = string.match(vim.fn.system('tmux -V'), '%d+%.%d+')

View File

@ -1,5 +1,4 @@
local health = vim.health local health = vim.health
local executable = health.executable
local iswin = vim.loop.os_uname().sysname == 'Windows_NT' local iswin = vim.loop.os_uname().sysname == 'Windows_NT'
local M = {} local M = {}
@ -12,8 +11,12 @@ function M.check()
end end
if if
not executable('node') vim.fn.executable('node') == 0
or (not executable('npm') and not executable('yarn') and not executable('pnpm')) or (
vim.fn.executable('npm') == 0
and vim.fn.executable('yarn') == 0
and vim.fn.executable('pnpm') == 0
)
then then
health.warn( health.warn(
'`node` and `npm` (or `yarn`, `pnpm`) must be in $PATH.', '`node` and `npm` (or `yarn`, `pnpm`) must be in $PATH.',
@ -50,9 +53,9 @@ function M.check()
health.info('Nvim node.js host: ' .. host) health.info('Nvim node.js host: ' .. host)
local manager = 'npm' local manager = 'npm'
if executable('yarn') then if vim.fn.executable('yarn') == 1 then
manager = 'yarn' manager = 'yarn'
elseif executable('pnpm') then elseif vim.fn.executable('pnpm') == 1 then
manager = 'pnpm' manager = 'pnpm'
end end

View File

@ -1,5 +1,4 @@
local health = vim.health local health = vim.health
local executable = health.executable
local iswin = vim.loop.os_uname().sysname == 'Windows_NT' local iswin = vim.loop.os_uname().sysname == 'Windows_NT'
local M = {} local M = {}
@ -60,7 +59,7 @@ local function check_bin(bin)
if not is(bin, 'file') and (not iswin or not is(bin .. '.exe', 'file')) then if not is(bin, 'file') and (not iswin or not is(bin .. '.exe', 'file')) then
health.error('"' .. bin .. '" was not found.') health.error('"' .. bin .. '" was not found.')
return false return false
elseif not executable(bin) then elseif vim.fn.executable(bin) == 0 then
health.error('"' .. bin .. '" is not executable.') health.error('"' .. bin .. '" is not executable.')
return false return false
end end
@ -69,7 +68,7 @@ end
-- Fetch the contents of a URL. -- Fetch the contents of a URL.
local function download(url) local function download(url)
local has_curl = executable('curl') local has_curl = vim.fn.executable('curl') == 1
if has_curl and vim.fn.system({ 'curl', '-V' }):find('Protocols:.*https') then if has_curl and vim.fn.system({ 'curl', '-V' }):find('Protocols:.*https') then
local out, rc = health.system({ 'curl', '-sL', url }, { stderr = true, ignore_error = true }) local out, rc = health.system({ 'curl', '-sL', url }, { stderr = true, ignore_error = true })
if rc ~= 0 then if rc ~= 0 then
@ -77,7 +76,7 @@ local function download(url)
else else
return out return out
end end
elseif executable('python') then elseif vim.fn.executable('python') == 1 then
local script = "try:\n\ local script = "try:\n\
from urllib.request import urlopen\n\ from urllib.request import urlopen\n\
except ImportError:\n\ except ImportError:\n\
@ -284,7 +283,7 @@ function M.check()
if if
path_bin ~= vim.fs.normalize(python_exe) path_bin ~= vim.fs.normalize(python_exe)
and vim.tbl_contains(python_multiple, path_bin) and vim.tbl_contains(python_multiple, path_bin)
and executable(path_bin) and vim.fn.executable(path_bin) == 1
then then
python_multiple[#python_multiple + 1] = path_bin python_multiple[#python_multiple + 1] = path_bin
end end
@ -472,7 +471,7 @@ function M.check()
bin_dir, bin_dir,
table.concat( table.concat(
vim.tbl_map(function(v) vim.tbl_map(function(v)
return vim.fn.fnamemodify(v, ':t') return vim.fs.basename(v)
end, venv_bins), end, venv_bins),
', ' ', '
) )

View File

@ -1,5 +1,4 @@
local health = vim.health local health = vim.health
local executable = health.executable
local iswin = vim.loop.os_uname().sysname == 'Windows_NT' local iswin = vim.loop.os_uname().sysname == 'Windows_NT'
local M = {} local M = {}
@ -11,7 +10,7 @@ function M.check()
return return
end end
if not executable('ruby') or not executable('gem') then if vim.fn.executable('ruby') == 0 or vim.fn.executable('gem') == 0 then
health.warn( health.warn(
'`ruby` and `gem` must be in $PATH.', '`ruby` and `gem` must be in $PATH.',
'Install Ruby and verify that `ruby` and `gem` commands work.' 'Install Ruby and verify that `ruby` and `gem` commands work.'

View File

@ -386,7 +386,7 @@ local path2name = function(path)
path = path:gsub('^.*/lua/', '') path = path:gsub('^.*/lua/', '')
-- Remove the filename (health.lua) -- Remove the filename (health.lua)
path = vim.fn.fnamemodify(path, ':h') path = vim.fs.dirname(path)
-- Change slashes to dots -- Change slashes to dots
path = path:gsub('/', '.') path = path:gsub('/', '.')
@ -497,11 +497,4 @@ function M._check(mods, plugin_names)
vim.print('') vim.print('')
end end
local fn_bool = function(key)
return function(...)
return vim.fn[key](...) == 1
end
end
M.executable = fn_bool('executable')
return M return M