From 6a88d734bdb4660945923c3f4a38c14e45847c2c Mon Sep 17 00:00:00 2001 From: kongjun Date: Tue, 29 Apr 2025 03:49:28 +0000 Subject: feat: store passwords in a local scope instead of as a buffer variable --- README.md | 5 ----- lua/encrypt/helpers.lua | 19 ++++++++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ece988b..c140814 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,3 @@ tail -n +2 encrypted.txt \ ``` Or use the provided [decrypt.sh](decrypt.sh) - -## TODO - -- [ ] Store the password in a local scope in the plugin instead of as a buffer - variable. diff --git a/lua/encrypt/helpers.lua b/lua/encrypt/helpers.lua index 37a96b3..ff8baef 100644 --- a/lua/encrypt/helpers.lua +++ b/lua/encrypt/helpers.lua @@ -1,13 +1,18 @@ ----@return string -local function getPassword() - local password = vim.b["password"] - if password == nil then - password = vim.fn.inputsecret("Enter password: ") - vim.b["password"] = password +---@return function string +local getPasswordFactory = function() + local bufferPasswordMap = {} + + return function() + local key = string.format("%s", vim.fn.bufnr()) + if bufferPasswordMap[key] == nil then + password = vim.fn.inputsecret("Enter password: ") + bufferPasswordMap[key] = password + end + return bufferPasswordMap[key] end - return password end +local getPassword = getPasswordFactory() local ENCRYPTED_PREFIX = "# <<>>" return { getPassword = getPassword, ENCRYPTED_PREFIX = ENCRYPTED_PREFIX } -- cgit v1.2.3