From 27abc8e721ffa65b0c927236e3ae47c623ff71fe Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 3 Jul 2024 17:33:07 +0300 Subject: [PATCH] initial commit --- README.md | 39 ++++++++++++++++++++++ lua/direnv.lua | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ stylua.toml | 3 ++ 3 files changed, 129 insertions(+) create mode 100644 README.md create mode 100644 lua/direnv.lua create mode 100644 stylua.toml diff --git a/README.md b/README.md new file mode 100644 index 0000000..56741d1 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# direnv.nvim + +Dead simple Neovim plugin to add automatic Direnv loading, inspired by +`direnv.vim` and written in Lua. + +## 📦 Installation + +Install `direnv.nvim` with your favorite plugin manager, or clone it manually. +You will need to call the setup function to load the plugin. + +## 🚀 Usage + +direnv.nvim will automatically call `direnv allow` in your current directory if +`direnv` is available in your PATH, and you have auto-loading enabled. + +## 🔧 Configuration + +You can pass your config table into the `setup()` function or `opts` if you use +`lazy.nvim`. + +### Options + +- `bin` (optional, type: string): the path to the Direnv binary. May be an + absolute path, or just `direnv` if it's available in your PATH. - Default: + `direnv` +- `autoload_direnv` (optional, type: boolean): whether to call `direnv allow` + when you enter a directory that contains an `.envrc`. - Default: `false` +- `keybindings` (optional, type: table of strings): the table of keybindings to + use. + - Default: + `{allow = "da", deny = "dd", reload = "dr"}` + +#### Example: + +```lua +require("direnv").setup({ + autoload_direnv = true, +}) +``` diff --git a/lua/direnv.lua b/lua/direnv.lua new file mode 100644 index 0000000..f68b7ab --- /dev/null +++ b/lua/direnv.lua @@ -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 = "da", + deny = "dd", + reload = "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 diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..457eb4f --- /dev/null +++ b/stylua.toml @@ -0,0 +1,3 @@ +indent_type = "Spaces" +indent_width = 3 +column_width = 80