Files

179 lines
5.6 KiB
Lua

-- Open projects with the `dev` command: "p toolshed" does what `dev ~/git/toolshed` does,
-- a Kitty tab backed by a tmux session. Lists the repos under the folders below plus every
-- project dev remembers; remembered ones rank first and running sessions are marked live.
local home = os.getenv("HOME")
local roots = { home .. "/git" }
local registry = (os.getenv("XDG_STATE_HOME") or (home .. "/.local/state")) .. "/dev/projects"
-- Scanning folders and asking tmux on every keystroke would be wasteful.
local cache_seconds = 10
local cached, cached_at = nil, 0
local function lines(text)
local out = {}
for line in text:gmatch("[^\n]+") do table.insert(out, line) end
return out
end
local function read_registry()
local f = io.open(registry, "r")
if not f then return {} end
local text = f:read("*a")
f:close()
return lines(text)
end
-- Same rule as dev's name_of: tmux session names can't hold '.' or ':'.
local function session_name(path)
return (path:match("([^/]+)/*$") or path):gsub("[.:]", "_")
end
local function live_sessions()
local ok, out = pcall(toolshed.exec, { "tmux", "list-sessions", "-F", "#{session_name}" })
local live = {}
if ok then
for _, name in ipairs(lines(out)) do live[name] = true end
end
return live
end
local function projects()
if cached and os.time() - cached_at < cache_seconds then return cached end
local list, seen = {}, {}
local function add(path, remembered)
if seen[path] then
if remembered then seen[path].remembered = true end
return
end
local p = { path = path, name = path:match("([^/]+)$"), remembered = remembered }
seen[path] = p
table.insert(list, p)
end
for _, path in ipairs(read_registry()) do add(path, true) end
for _, root in ipairs(roots) do
local ok, out = pcall(toolshed.exec, { "find", root, "-mindepth", "2", "-maxdepth", "2", "-name", ".git" })
if ok then
for _, git in ipairs(lines(out)) do add((git:gsub("/%.git$", "")), false) end
end
end
local live = live_sessions()
for _, p in ipairs(list) do p.live = live[session_name(p.path)] == true end
cached, cached_at = list, os.time()
return list
end
-- git@host:owner/repo.git and https://host/owner/repo.git both become https://host/owner/repo.
local function web_url(remote)
remote = remote:gsub("%s+$", ""):gsub("%.git$", "")
local host, path = remote:match("^[%w._-]+@([^:]+):(.+)$")
if host then return "https://" .. host .. "/" .. path end
host, path = remote:match("^ssh://[^@]+@([^/:]+)[:%d]*/(.+)$")
if host then return "https://" .. host .. "/" .. path end
if remote:match("^https?://") then return remote end
return nil
end
-- A tab already showing the project is focused wherever it is; place only decides where a
-- new one opens. Without a place, the open_in setting decides.
local function open_action(key, title, place)
return {
key = key,
title = title,
run = function(ctx)
toolshed.exec({ "dev", "--in", place or ctx.settings.open_in, ctx.data.path })
cached = nil -- a session now runs, and dev remembers the project
end,
}
end
return toolshed.toolbox {
key = "projects",
title = "i18n:toolbox.title",
description = "i18n:toolbox.description",
icon = "folder-git-2",
tint = "orange",
requires = { { command = "dev" }, { command = "tmux" } },
settings = {
{
key = "open_in",
title = "i18n:setting.open_in.title",
description = "i18n:setting.open_in.description",
type = "choice",
default = "dropdown",
options = {
{ value = "desktop", title = "i18n:setting.open_in.desktop" },
{ value = "dropdown", title = "i18n:setting.open_in.dropdown" },
},
},
},
tools = { {
key = "search",
title = "i18n:tool.search.title",
words = { "p" },
keywords = { "prosjekter", "repo" },
items = true,
search = function(rest)
local matches = {}
for _, p in ipairs(projects()) do
local score = toolshed.match(rest, p.name)
if score > 0 then
if p.live then score = score + 0.1 elseif p.remembered then score = score + 0.05 end
table.insert(matches, { score = score, project = p })
end
end
table.sort(matches, function(a, b)
if a.score ~= b.score then return a.score > b.score end
return a.project.name:lower() < b.project.name:lower()
end)
local items = {}
for i = 1, math.min(#matches, 30) do
local p = matches[i].project
table.insert(items, {
title = p.name,
subtitle = (p.live and (toolshed.t("live") .. " · ") or "") .. p.path:gsub("^" .. home:gsub("%p", "%%%0"), "~"),
data = { path = p.path },
})
end
return items
end,
actions = {
open_action("open", "i18n:action.search.open"),
open_action("open-desktop", "i18n:action.search.open-desktop", "desktop"),
open_action("open-dropdown", "i18n:action.search.open-dropdown", "dropdown"),
{
key = "finder",
title = "i18n:action.search.finder",
run = function(ctx) toolshed.open(ctx.data.path) end,
},
{
key = "copy-path",
title = "i18n:action.search.copy-path",
run = function(ctx)
toolshed.clipboard.copy(ctx.data.path)
return toolshed.t("path-copied")
end,
},
{
key = "web",
title = "i18n:action.search.web",
run = function(ctx)
local ok, remote = pcall(toolshed.exec, { "git", "-C", ctx.data.path, "remote", "get-url", "origin" })
local url = ok and web_url(remote)
if not url then error(toolshed.t("no-web-address")) end
toolshed.open(url)
end,
},
},
} },
}