mirror of
https://github.com/jdhao/nvim-config.git
synced 2025-06-08 14:14:33 +02:00
54 lines
1.1 KiB
Lua
54 lines
1.1 KiB
Lua
local fn = vim.fn
|
|
|
|
local M = {}
|
|
|
|
function M.executable(name)
|
|
if fn.executable(name) > 0 then
|
|
return true
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
--- check whether a feature exists in Nvim
|
|
--- @param feat string the feature name, like `nvim-0.7` or `unix`.
|
|
--- @return boolean
|
|
M.has = function(feat)
|
|
if fn.has(feat) == 1 then
|
|
return true
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
--- Create a dir if it does not exist
|
|
function M.may_create_dir(dir)
|
|
local res = fn.isdirectory(dir)
|
|
|
|
if res == 0 then
|
|
fn.mkdir(dir, "p")
|
|
end
|
|
end
|
|
|
|
--- Generate random integers in the range [Low, High], inclusive,
|
|
--- adapted from https://stackoverflow.com/a/12739441/6064933
|
|
--- @param low integer the lower value for this range
|
|
--- @param high integer the upper value for this range
|
|
--- @return integer
|
|
function M.rand_int(low, high)
|
|
-- Use lua to generate random int, see also: https://stackoverflow.com/a/20157671/6064933
|
|
math.randomseed(os.time())
|
|
|
|
return math.random(low, high)
|
|
end
|
|
|
|
--- Select a random element from a sequence/list.
|
|
--- @param seq any[] the sequence to choose an element
|
|
function M.rand_element(seq)
|
|
local idx = M.rand_int(1, #seq)
|
|
|
|
return seq[idx]
|
|
end
|
|
|
|
return M
|