diff options
| author | kongjun <kongjun18@outlook.com> | 2025-04-29 06:51:01 +0000 |
|---|---|---|
| committer | kongjun <kongjun18@outlook.com> | 2025-04-29 06:59:38 +0000 |
| commit | 872632670c31f682f3db01707468dbc12f154ab4 (patch) | |
| tree | f8e75de6fa3dddc291a0a3c239e2dc616d256a8f /lua | |
| parent | e6057b3c13f004477f4963f3cdf054e5ca1bf2f4 (diff) | |
| download | encrypt.nvim-872632670c31f682f3db01707468dbc12f154ab4.tar.gz | |
feat: :X decrypts or encrypts files
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/encrypt/helpers.lua | 14 | ||||
| -rw-r--r-- | lua/encrypt/init.lua | 12 |
2 files changed, 21 insertions, 5 deletions
diff --git a/lua/encrypt/helpers.lua b/lua/encrypt/helpers.lua index a2ccc51..b740f28 100644 --- a/lua/encrypt/helpers.lua +++ b/lua/encrypt/helpers.lua @@ -21,4 +21,16 @@ local BUFTYPE = { plaintext = 2, } -return { getPassword = getPassword, ENCRYPTED_PREFIX = ENCRYPTED_PREFIX, BUFTYPE = BUFTYPE} +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, +} diff --git a/lua/encrypt/init.lua b/lua/encrypt/init.lua index cfa1aa6..ea5368f 100644 --- a/lua/encrypt/init.lua +++ b/lua/encrypt/init.lua @@ -5,14 +5,18 @@ local helpers = require("encrypt.helpers") local function setup() vim.api.nvim_create_user_command("X", function() - setupBuffer(helpers.BUFTYPE.decrypted) - encrypt() + if helpers.bufferEncrypted() then + setupBuffer(helpers.BUFTYPE.encrypted) + decrypt() + else + setupBuffer(helpers.BUFTYPE.plaintext) + encrypt() + end end, {}) vim.api.nvim_create_autocmd({ "BufReadPost" }, { callback = function() - local first_line = vim.api.nvim_buf_get_lines(0, 0, 1, false)[1] - if first_line == helpers.ENCRYPTED_PREFIX then + if helpers.bufferEncrypted() then setupBuffer(helpers.BUFTYPE.encrypted) decrypt() end |
