146 lines
5.0 KiB
Lua
146 lines
5.0 KiB
Lua
-- Icons for self-hosted apps from dashboardicons.com: "di plex" shows them as tiles. Enter copies
|
|
-- the icon's address, its SVG when it has one, for a dashboard like Homarr or Homepage; ⌘K copies
|
|
-- the SVG or PNG address, its name or the picture, or opens it on the site.
|
|
|
|
local cdn = "https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/"
|
|
local list_file = toolshed.cache_dir() .. "/metadata.json"
|
|
-- New icons arrive every few days; a week-old list still finds nearly all of them.
|
|
local refresh_seconds = 7 * 24 * 3600
|
|
|
|
local modified = toolshed.file.modified
|
|
|
|
local function download(max_seconds)
|
|
local tmp = list_file .. ".download"
|
|
toolshed.exec({ "curl", "-sSfL", "--max-time", tostring(max_seconds), "-o", tmp, cdn .. "metadata.json" })
|
|
assert(os.rename(tmp, list_file))
|
|
end
|
|
|
|
-- refresh downloads a stale list again in the background, quietly, so no search waits for it:
|
|
-- once a run of Toolshed, at its first search, as the list read then is kept.
|
|
local refreshed = false
|
|
local function refresh()
|
|
if refreshed then return end
|
|
refreshed = true
|
|
local changed = modified(list_file)
|
|
if changed and os.time() - changed > refresh_seconds then
|
|
toolshed.activities.set {
|
|
key = "refresh", title = toolshed.t("jobs.refresh.title"), description = toolshed.t("jobs.refresh.description"), quiet = true, timeout = 120,
|
|
run = { "sh", "-c", 'curl -sSfL --max-time 60 -o "$0.download" "$1" && mv "$0.download" "$0"', list_file, cdn .. "metadata.json" },
|
|
}
|
|
end
|
|
end
|
|
|
|
local icons
|
|
local function load()
|
|
refresh()
|
|
if icons then return icons end
|
|
if not modified(list_file) then download(1.5) end -- the first search ever has to wait
|
|
local f = assert(io.open(list_file, "r"))
|
|
local metadata = toolshed.json.decode(f:read("*a"))
|
|
f:close()
|
|
icons = {}
|
|
for name, meta in pairs(metadata) do
|
|
table.insert(icons, {
|
|
name = name,
|
|
base = meta.base,
|
|
aliases = table.concat(meta.aliases or {}, " "),
|
|
categories = table.concat(meta.categories or {}, ", "),
|
|
})
|
|
end
|
|
table.sort(icons, function(a, b) return a.name < b.name end)
|
|
return icons
|
|
end
|
|
|
|
local function address(ctx, format)
|
|
return cdn .. format .. "/" .. ctx.data.name .. "." .. format
|
|
end
|
|
|
|
return toolshed.toolbox {
|
|
key = "dashboard-icons",
|
|
title = "Dashboard Icons",
|
|
description = "i18n:toolbox.description",
|
|
icon = "image",
|
|
tint = "blue",
|
|
grid = true,
|
|
|
|
tools = { {
|
|
key = "search",
|
|
title = "Dashboard Icons",
|
|
words = { "di" },
|
|
examples = { "di plex", "di home assistant" },
|
|
keywords = { "ikoner" },
|
|
items = true,
|
|
|
|
search = function(rest)
|
|
local found = {}
|
|
for _, icon in ipairs(load()) do
|
|
local score = math.max(
|
|
toolshed.match(rest, icon.name),
|
|
toolshed.match(rest, icon.aliases),
|
|
toolshed.match(rest, icon.categories) * 0.8)
|
|
if score > 0 then table.insert(found, { icon = icon, score = score }) end
|
|
end
|
|
table.sort(found, function(a, b)
|
|
if a.score ~= b.score then return a.score > b.score end
|
|
return a.icon.name < b.icon.name
|
|
end)
|
|
local items = {}
|
|
for i = 1, math.min(#found, 50) do
|
|
local icon = found[i].icon
|
|
table.insert(items, {
|
|
title = icon.name,
|
|
subtitle = icon.categories,
|
|
-- The original: an SVG is mostly a tenth of the PNG's size, and some PNGs made from SVGs
|
|
-- came out blank, like jellyglance's.
|
|
image = cdn .. icon.base .. "/" .. icon.name .. "." .. icon.base,
|
|
data = { name = icon.name, base = icon.base },
|
|
})
|
|
end
|
|
return items
|
|
end,
|
|
|
|
actions = {
|
|
{
|
|
key = "copy-address", title = "i18n:action.search.copy-address",
|
|
run = function(ctx)
|
|
toolshed.clipboard.copy(address(ctx, ctx.data.base))
|
|
return toolshed.t(ctx.data.base == "svg" and "svg-copied" or "png-copied-no-svg")
|
|
end,
|
|
},
|
|
{
|
|
key = "copy-svg-address", title = "i18n:action.search.copy-svg-address",
|
|
run = function(ctx)
|
|
if ctx.data.base ~= "svg" then error(toolshed.t("no-svg")) end
|
|
toolshed.clipboard.copy(address(ctx, "svg"))
|
|
return toolshed.t("svg-copied")
|
|
end,
|
|
},
|
|
{
|
|
key = "copy-png-address", title = "i18n:action.search.copy-png-address",
|
|
run = function(ctx)
|
|
toolshed.clipboard.copy(address(ctx, "png"))
|
|
return toolshed.t("png-copied")
|
|
end,
|
|
},
|
|
{
|
|
key = "copy-image", title = "i18n:action.search.copy-image",
|
|
run = function(ctx)
|
|
toolshed.clipboard.copy_image(address(ctx, "png"))
|
|
return toolshed.t("image-copied")
|
|
end,
|
|
},
|
|
{
|
|
key = "copy-name", title = "i18n:action.search.copy-name",
|
|
run = function(ctx)
|
|
toolshed.clipboard.copy(ctx.data.name)
|
|
return toolshed.t("name-copied")
|
|
end,
|
|
},
|
|
{
|
|
key = "open", title = "i18n:action.search.open",
|
|
run = function(ctx) toolshed.open("https://dashboardicons.com/icons/" .. ctx.data.name) end,
|
|
},
|
|
},
|
|
} },
|
|
}
|