2022-12-23 07:34:55 +00:00
|
|
|
---@class LazyUtil: LazyUtilCore
|
2022-11-24 21:03:00 +00:00
|
|
|
local M = setmetatable({}, { __index = require("lazy.core.util") })
|
|
|
|
|
|
|
|
function M.file_exists(file)
|
|
|
|
return vim.loop.fs_stat(file) ~= nil
|
|
|
|
end
|
|
|
|
|
2022-12-24 10:26:29 +00:00
|
|
|
---@param opts? LazyViewOptions
|
|
|
|
function M.float(opts)
|
|
|
|
opts = vim.tbl_deep_extend("force", {
|
|
|
|
win_opts = { zindex = 60, border = "none" },
|
|
|
|
margin = { top = 3, left = 2, right = 2 },
|
|
|
|
}, opts or {})
|
|
|
|
return require("lazy.view.float")(opts)
|
|
|
|
end
|
|
|
|
|
2022-11-24 21:03:00 +00:00
|
|
|
function M.open(uri)
|
|
|
|
if M.file_exists(uri) then
|
2022-12-24 10:26:53 +00:00
|
|
|
return M.float({ win_opts = { style = "" }, file = uri })
|
2022-11-24 21:03:00 +00:00
|
|
|
end
|
2022-12-30 19:41:23 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-24 21:03:00 +00:00
|
|
|
local cmd
|
2022-12-30 19:41:23 +00:00
|
|
|
if Config.options.ui.browser then
|
|
|
|
cmd = { Config.options.ui.browser, uri }
|
|
|
|
elseif vim.fn.has("win32") == 1 then
|
2022-12-20 11:34:24 +00:00
|
|
|
cmd = { "explorer", uri }
|
2022-11-24 21:03:00 +00:00
|
|
|
elseif vim.fn.has("macunix") == 1 then
|
|
|
|
cmd = { "open", uri }
|
|
|
|
else
|
2022-12-30 19:41:23 +00:00
|
|
|
if vim.fn.executable("xdg-open") then
|
|
|
|
cmd = { "xdg-open", uri }
|
|
|
|
else
|
|
|
|
cmd = { "open", uri }
|
|
|
|
end
|
2022-11-24 21:03:00 +00:00
|
|
|
end
|
|
|
|
|
2022-12-20 11:34:24 +00:00
|
|
|
local ret = vim.fn.jobstart(cmd, { detach = true })
|
|
|
|
if ret <= 0 then
|
2022-11-24 21:03:00 +00:00
|
|
|
local msg = {
|
|
|
|
"Failed to open uri",
|
|
|
|
ret,
|
|
|
|
vim.inspect(cmd),
|
|
|
|
}
|
|
|
|
vim.notify(table.concat(msg, "\n"), vim.log.levels.ERROR)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-15 13:06:52 +00:00
|
|
|
function M.read_file(file)
|
|
|
|
local fd = assert(io.open(file, "r"))
|
|
|
|
---@type string
|
|
|
|
local data = fd:read("*a")
|
|
|
|
fd:close()
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.write_file(file, contents)
|
|
|
|
local fd = assert(io.open(file, "w+"))
|
|
|
|
fd:write(contents)
|
|
|
|
fd:close()
|
|
|
|
end
|
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
---@generic F: fun()
|
2022-11-24 21:03:00 +00:00
|
|
|
---@param ms number
|
2022-12-23 09:18:19 +00:00
|
|
|
---@param fn F
|
|
|
|
---@return F
|
2022-11-24 21:03:00 +00:00
|
|
|
function M.throttle(ms, fn)
|
|
|
|
local timer = vim.loop.new_timer()
|
|
|
|
local running = false
|
|
|
|
local first = true
|
|
|
|
|
2022-12-23 09:18:19 +00:00
|
|
|
return function(...)
|
|
|
|
local args = { ... }
|
|
|
|
local wrapped = function()
|
|
|
|
fn(unpack(args))
|
|
|
|
end
|
2022-11-24 21:03:00 +00:00
|
|
|
if not running then
|
|
|
|
if first then
|
2022-12-23 09:18:19 +00:00
|
|
|
wrapped()
|
2022-11-24 21:03:00 +00:00
|
|
|
first = false
|
|
|
|
end
|
|
|
|
|
|
|
|
timer:start(ms, 0, function()
|
|
|
|
running = false
|
2022-12-23 09:18:19 +00:00
|
|
|
vim.schedule(wrapped)
|
2022-11-24 21:03:00 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
running = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-24 10:26:53 +00:00
|
|
|
---@param cmd string[]
|
2022-12-30 09:23:12 +00:00
|
|
|
---@param opts? {cwd:string, filetype:string, terminal?:boolean, close_on_exit?:boolean, enter?:boolean, float?:LazyViewOptions}
|
2022-12-24 10:26:53 +00:00
|
|
|
function M.open_cmd(cmd, opts)
|
|
|
|
opts = opts or {}
|
2022-12-30 09:23:12 +00:00
|
|
|
local float = M.float(opts.float)
|
2022-12-24 10:26:53 +00:00
|
|
|
|
|
|
|
if opts.filetype then
|
|
|
|
vim.bo[float.buf].filetype = opts.filetype
|
|
|
|
end
|
|
|
|
if opts.terminal then
|
|
|
|
opts.terminal = nil
|
|
|
|
vim.fn.termopen(cmd, opts)
|
|
|
|
if opts.enter then
|
|
|
|
vim.cmd.startinsert()
|
|
|
|
end
|
|
|
|
if opts.close_on_exit then
|
|
|
|
vim.api.nvim_create_autocmd("TermClose", {
|
|
|
|
once = true,
|
|
|
|
buffer = float.buf,
|
|
|
|
callback = function()
|
|
|
|
float:close()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local Process = require("lazy.manage.process")
|
|
|
|
local lines = Process.exec(cmd, { cwd = opts.cwd })
|
|
|
|
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
|
|
|
|
vim.bo[float.buf].modifiable = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-24 21:03:00 +00:00
|
|
|
---@return string?
|
|
|
|
function M.head(file)
|
|
|
|
local f = io.open(file)
|
|
|
|
if f then
|
|
|
|
local ret = f:read()
|
|
|
|
f:close()
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@return {branch: string, hash:string}?
|
|
|
|
function M.git_info(dir)
|
|
|
|
local line = M.head(dir .. "/.git/HEAD")
|
|
|
|
if line then
|
|
|
|
---@type string, string
|
|
|
|
local ref, branch = line:match("ref: (refs/heads/(.*))")
|
|
|
|
|
|
|
|
if ref then
|
|
|
|
return {
|
|
|
|
branch = branch,
|
|
|
|
hash = M.head(dir .. "/.git/" .. ref),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param msg string|string[]
|
|
|
|
---@param opts? table
|
|
|
|
function M.markdown(msg, opts)
|
|
|
|
if type(msg) == "table" then
|
|
|
|
msg = table.concat(msg, "\n") or msg
|
|
|
|
end
|
|
|
|
|
|
|
|
vim.notify(
|
|
|
|
msg,
|
|
|
|
vim.log.levels.INFO,
|
|
|
|
vim.tbl_deep_extend("force", {
|
|
|
|
title = "lazy.nvim",
|
|
|
|
on_open = function(win)
|
|
|
|
vim.wo[win].conceallevel = 3
|
|
|
|
vim.wo[win].concealcursor = "n"
|
|
|
|
vim.wo[win].spell = false
|
|
|
|
|
|
|
|
vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown")
|
|
|
|
end,
|
|
|
|
}, opts or {})
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M._dump(value, result)
|
|
|
|
local t = type(value)
|
|
|
|
if t == "number" or t == "boolean" then
|
|
|
|
table.insert(result, tostring(value))
|
|
|
|
elseif t == "string" then
|
|
|
|
table.insert(result, ("%q"):format(value))
|
|
|
|
elseif t == "table" then
|
|
|
|
table.insert(result, "{")
|
|
|
|
local i = 1
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for k, v in pairs(value) do
|
|
|
|
if k == i then
|
|
|
|
elseif type(k) == "string" then
|
|
|
|
table.insert(result, ("[%q]="):format(k))
|
|
|
|
else
|
|
|
|
table.insert(result, k .. "=")
|
|
|
|
end
|
|
|
|
M._dump(v, result)
|
|
|
|
table.insert(result, ",")
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
table.insert(result, "}")
|
|
|
|
else
|
|
|
|
error("Unsupported type " .. t)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.dump(value)
|
|
|
|
local result = {}
|
|
|
|
M._dump(value, result)
|
|
|
|
return table.concat(result, "")
|
|
|
|
end
|
|
|
|
|
2022-12-05 13:46:11 +00:00
|
|
|
---@generic V
|
|
|
|
---@param t table<string, V>
|
|
|
|
---@param fn fun(key:string, value:V)
|
|
|
|
function M.foreach(t, fn)
|
|
|
|
---@type string[]
|
|
|
|
local keys = vim.tbl_keys(t)
|
2022-12-22 09:32:21 +00:00
|
|
|
pcall(table.sort, keys)
|
2022-12-05 13:46:11 +00:00
|
|
|
for _, key in ipairs(keys) do
|
|
|
|
fn(key, t[key])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-24 21:03:00 +00:00
|
|
|
return M
|