From 872632670c31f682f3db01707468dbc12f154ab4 Mon Sep 17 00:00:00 2001 From: kongjun Date: Tue, 29 Apr 2025 06:51:01 +0000 Subject: feat: :X decrypts or encrypts files --- lua/encrypt/helpers.lua | 14 +++++++++++++- lua/encrypt/init.lua | 12 ++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'lua') 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 -- cgit v1.2.3