Files
community-plugins/pass/service.luau
T

94 lines
2.2 KiB
Luau

--!nonstrict
local entries = {}
local function publish()
noctalia.state.set("entries", entries)
end
local function passwordStoreDir()
local dir = noctalia.getenv("PASSWORD_STORE_DIR")
if dir and dir ~= "" then
return noctalia.expandPath(dir)
end
return noctalia.expandPath("~/.password-store")
end
local function scan(dir, prefix)
prefix = prefix or ""
local names, err = noctalia.listDir(dir)
if not names then
noctalia.log("Failed to list " .. dir .. ": " .. tostring(err))
return
end
for _, name in ipairs(names) do
-- Ignore hidden files/directories (.git, .extensions, .gpg-id, ...)
if name:sub(1, 1) ~= "." then
-- listDir already proved the name exists, so an entry needs no stat.
-- Only non-entry names get one, to decide whether to recurse.
if name:sub(-4) == ".gpg" then
local path = prefix .. name:sub(1, -5)
local title = path
local subtitle = ""
local lastSlash = path:match("^.*()/")
if lastSlash then
subtitle = path:sub(1, lastSlash - 1)
title = path:sub(lastSlash + 1)
end
table.insert(entries, {
id = path,
path = path,
title = title,
subtitle = subtitle,
})
else
local full = dir .. "/" .. name
local info = noctalia.fileInfo(full)
if info and info.isDir then
scan(full, prefix .. name .. "/")
end
end
end
end
end
local function rebuild()
entries = {}
local root = passwordStoreDir()
if not noctalia.fileExists(root) then
noctalia.notifyError("Pass", noctalia.tr("notification.store_not_found"))
return
end
scan(root)
table.sort(entries, function(a, b)
return a.title:lower() < b.title:lower()
end)
publish()
end
function update()
rebuild()
end
local refresh = noctalia.getConfig("refresh_interval") or 30
noctalia.setUpdateInterval(refresh * 1000)
rebuild()