aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md8
-rw-r--r--lua/encrypt/helpers.lua14
-rw-r--r--lua/encrypt/init.lua12
3 files changed, 29 insertions, 5 deletions
diff --git a/README.md b/README.md
index c140814..cac1b86 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,7 @@ in an encrypted format on the disk.
## Usage
+Encryption:
1. Open a file
2. Run `:X`
3. Enter a password
@@ -15,6 +16,13 @@ in an encrypted format on the disk.
Now the file is encrypted and every time you open it you'll be asked for the
password.
+Decryption:
+1. Open an encrypted file
+2. Run `:X`
+3. Enter a password
+
+Now the encrypted file is decrypted in the buffer.
+
## Installation
Add the following to your package manager:
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