aboutsummaryrefslogtreecommitdiff
path: root/lua/encrypt/helpers.lua
diff options
context:
space:
mode:
authorDavid Elentok <3david@gmail.com>2025-05-06 22:04:23 +0300
committerGitHub <noreply@github.com>2025-05-06 22:04:23 +0300
commit363e1d924c6235ae4e9170b1e2ebab50a2e26c07 (patch)
treebf21093fa70c8ed8014e70f4841714d622ec651e /lua/encrypt/helpers.lua
parent09d7f6ee9ee1d728449131dff4e750e5339f7968 (diff)
parenta9e7d700eb546800e3d341bc53667964ac821a38 (diff)
downloadencrypt.nvim-363e1d924c6235ae4e9170b1e2ebab50a2e26c07.tar.gz
Merge pull request #2 from kongjun18/main
Some Improvement
Diffstat (limited to 'lua/encrypt/helpers.lua')
-rw-r--r--lua/encrypt/helpers.lua42
1 files changed, 34 insertions, 8 deletions
diff --git a/lua/encrypt/helpers.lua b/lua/encrypt/helpers.lua
index 37a96b3..c88b448 100644
--- a/lua/encrypt/helpers.lua
+++ b/lua/encrypt/helpers.lua
@@ -1,13 +1,39 @@
----@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: ")
+ if password == "" then
+ vim.notify("Password is cancelled", vim.log.levels.WARN)
+ end
+ bufferPasswordMap[key] = password
+ end
+ return bufferPasswordMap[key]
end
- return password
end
+local getPassword = getPasswordFactory()
local ENCRYPTED_PREFIX = "# <<<encrypted>>>"
-return { getPassword = getPassword, ENCRYPTED_PREFIX = ENCRYPTED_PREFIX }
+---@enum buftype
+local BUFTYPE = {
+ encrypted = 1,
+ plaintext = 2,
+}
+
+local function bufferEncrypted()
+ local first_line = vim.api.nvim_buf_get_lines(0, 0, 1, false)[1]
+ if first_line == ENCRYPTED_PREFIX then
+ return true
+ end
+end
+
+return {
+ getPassword = getPassword,
+ ENCRYPTED_PREFIX = ENCRYPTED_PREFIX,
+ BUFTYPE = BUFTYPE,
+ bufferEncrypted = bufferEncrypted,
+}