Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: health check #1267

Merged
merged 8 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions autoload/health/fzf_lua.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function! health#fzf_lua#check()
lua require("fzf-lua._health").check()
endfunction
115 changes: 115 additions & 0 deletions lua/fzf-lua/_health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
local M = {}

local start = vim.health.start or vim.health.report_start
local ok = vim.health.ok or vim.health.report_ok
local warn = vim.health.warn or vim.health.report_warn
local error = vim.health.error or vim.health.report_error
local uv = vim.uv or vim.loop

function M.check()
local is_win = jit.os:find("Windows")
local utils = require("fzf-lua.utils")

local function have(tool)
if vim.fn.executable(tool) == 0 then
warn("'" .. tool .. "' not found")
else
local version = vim.fn.system(tool .. " --version") or ""
version = vim.trim(vim.split(version, "\n")[1])
ok("'" .. tool .. "' `" .. version .. "`")
return true
end
end

start("fzf-lua [required]")
local required = {
{ "fzf", "sk" },
is_win and { "rg" } or { "rg", "grep" },
is_win and { "fd", "find", "dir" } or { "fd", "find" },
folke marked this conversation as resolved.
Show resolved Hide resolved
}

for _, reqs in ipairs(required) do
local found = false
for _, tool in ipairs(reqs) do
if have(tool) then
found = true
break
end
end
if not found then
local str = table.concat(
vim.tbl_map(function(tool)
return "`" .. tool .. "`"
end, reqs),
", "
)
error("One of " .. str .. " is required")
end
end

local run = vim.fn.stdpath("run")
if vim.fn.isdirectory(run) == 0 then
ibhagwan marked this conversation as resolved.
Show resolved Hide resolved
error(
"Your 'run' directory is invalid `"
.. run
.. "`.\nPlease make sure `XDG_RUNTIME_DIR` is set correctly."
)
end

if vim.fn.executable("fzf") == 1 then
local version = utils.fzf_version()
if version < 0.53 then
warn("'fzf' `>= 0.53` is recommended.")
end
end

start("fzf-lua [optional]")
if pcall(require, "nvim-web-devicons") then
ok("`nvim-web-devicons` found")
else
warn("`nvim-web-devicons` not found")
end
for _, tool in ipairs({ "rg", "fd", "bat", "delta", "git" }) do
have(tool)
end

start("fzf-lua [optional:media]")
for _, tool in ipairs({ "chafa", "viu", "ueberzugpp" }) do
folke marked this conversation as resolved.
Show resolved Hide resolved
have(tool)
end

start("fzf-lua [env]")
if vim.env.FZF_DEFAULT_OPTS == nil then
ok("`FZF_DEFAULT_OPTS` is not set")
else
ok("`$FZF_DEFAULT_OPTS` is set to:\n" .. M.format(vim.env.FZF_DEFAULT_OPTS))
end
if vim.env.FZF_DEFAULT_OPTS_FILE == nil then
ok("`FZF_DEFAULT_OPTS_FILE` is not set")
else
ok("`FZF_DEFAULT_OPTS_FILE` is set to `" .. vim.env.FZF_DEFAULT_OPTS_FILE .. "`")
end
end

---@param str string
function M.format(str)
str = str:gsub("%s+", " ")
local options = vim.split(vim.trim(str), " -", { plain = true })
local lines = {}
for o, opt in ipairs(options) do
opt = o == 1 and opt or ("-" .. opt)
opt = vim.trim(opt)
opt = opt .. string.rep(" ", math.ceil(#opt / 30) * 30 - #opt)
if #lines == 0 or #lines[#lines] > 80 then
table.insert(lines, opt)
else
lines[#lines] = lines[#lines] .. "" .. opt
end
end
lines = vim.tbl_map(function(line)
return vim.trim(line)
end, lines)
return table.concat(lines, "\n")
end

return M