mirror of
https://github.com/NotAShelf/direnv.nvim.git
synced 2025-10-02 23:13:34 +00:00
initial commit
This commit is contained in:
parent
8d4d32a526
commit
27abc8e721
3 changed files with 129 additions and 0 deletions
87
lua/direnv.lua
Normal file
87
lua/direnv.lua
Normal file
|
@ -0,0 +1,87 @@
|
|||
local M = {}
|
||||
|
||||
local function check_executable(executable_name)
|
||||
if vim.fn.executable(executable_name) ~= 1 then
|
||||
vim.api.nvim_err_writeln(
|
||||
string.format("Executable '%s' not found", executable_name)
|
||||
)
|
||||
return false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function setup_keymaps(keymaps, mode, opts)
|
||||
for _, map in ipairs(keymaps) do
|
||||
local options =
|
||||
vim.tbl_extend("force", { noremap = true, silent = true }, opts or {})
|
||||
vkms(mode, map[1], map[2], options)
|
||||
end
|
||||
end
|
||||
|
||||
M.setup = function(user_config)
|
||||
local config = vim.tbl_deep_extend("force", {
|
||||
bin = "direnv",
|
||||
autoload_direnv = false,
|
||||
keybindings = {
|
||||
allow = "<Leader>da",
|
||||
deny = "<Leader>dd",
|
||||
reload = "<Leader>dr",
|
||||
},
|
||||
}, user_config or {})
|
||||
|
||||
if not check_executable(config.bin) then
|
||||
return
|
||||
end
|
||||
|
||||
setup_keymaps({
|
||||
{
|
||||
config.keybindings.allow,
|
||||
function()
|
||||
M.allow_direnv()
|
||||
end,
|
||||
desc = "Allow direnv",
|
||||
},
|
||||
{
|
||||
config.keybindings.deny,
|
||||
function()
|
||||
M.deny_direnv()
|
||||
end,
|
||||
desc = "Deny direnv",
|
||||
},
|
||||
{
|
||||
config.keybindings.reload,
|
||||
function()
|
||||
M.check_direnv()
|
||||
end,
|
||||
desc = "Reload direnv",
|
||||
},
|
||||
}, "n")
|
||||
|
||||
if config.autoload_direnv then
|
||||
local group_id = vim.api.nvim_create_augroup("DirenvNvim", {})
|
||||
vim.api.nvim_create_autocmd({ "BufEnter" }, {
|
||||
pattern = "*",
|
||||
group = group_id,
|
||||
callback = function()
|
||||
M.check_direnv()
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
M.allow_direnv = function()
|
||||
print("Allowing direnv...")
|
||||
os.execute("direnv allow")
|
||||
end
|
||||
|
||||
M.deny_direnv = function()
|
||||
print("Denying direnv...")
|
||||
os.execute("direnv deny")
|
||||
end
|
||||
|
||||
M.check_direnv = function()
|
||||
print("Checking direnv status...")
|
||||
os.execute("direnv reload")
|
||||
end
|
||||
|
||||
return M
|
Loading…
Add table
Add a link
Reference in a new issue