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