Check for encrypted and format to space only

This commit is contained in:
2025-11-03 14:39:33 +01:00
parent 1011be3d1f
commit fe565fec63

View File

@@ -1,48 +1,53 @@
--- @since 25.2.7 --- @since 25.2.7
local function info(content) local function info(content)
return ya.notify { return ya.notify {
title = "Gpg information", title = "Gpg information",
content = content, content = content,
timeout = 5, timeout = 5,
} }
end end
local hovered_url = ya.sync(function() local hovered_url = ya.sync(function()
local h = cx.active.current.hovered local h = cx.active.current.hovered
return h and h.url return h and h.url
end) end)
local get_state_attr = ya.sync(function(state, attr) local get_state_attr = ya.sync(function(state, attr)
return state[attr] return state[attr]
end) end)
return { return {
setup = function(state, options) setup = function(state, options)
state.delete_after = options.delete_after or true state.delete_after = options.delete_after or true
state.default_recipient = options.default_recipient state.default_recipient = options.default_recipient
end, end,
entry = function() entry = function()
local delete_after, default_recipient = get_state_attr("delete_after"), get_state_attr("default_recipient") local delete_after, default_recipient = get_state_attr("delete_after"), get_state_attr("default_recipient")
local hovered = tostring(hovered_url()) local hovered = tostring(hovered_url())
if not hovered then if not hovered then
return info("No file selected") return info("No file selected")
end end
if not default_recipient then if hovered:match("%.gpg$") then
return info("No recipient set") return info("This file is already encrypted")
end end
local _, err = Command("gpg"):arg("--yes"):arg("--recipient"):arg(default_recipient):arg("--output"):arg(hovered .. ".gpg"):arg("--encrypt"):arg(hovered):output() if not default_recipient then
if err then return info("No recipient set")
return info("Failed to gpg with error: " .. err) end
end
-- Delete the plain file local _, err = Command("gpg"):arg("--yes"):arg("--recipient"):arg(default_recipient):arg("--output"):arg(hovered ..
if delete_after == true then ".gpg"):arg("--encrypt"):arg(hovered):output()
os.remove(hovered) if err then
end return info("Failed to gpg with error: " .. err)
end
info("Done encrypting the file! The plain one is deleted now.") -- Delete the plain file
end, if delete_after == true then
os.remove(hovered)
end
info("Done encrypting the file! The plain one is deleted now.")
end,
} }