feat: add `SessionSelect` command

For those who don't want to use Telescope to load and select sessions,
this offers an alternative
main
olimorris 2024-08-22 18:37:13 +01:00
parent 3b07ef9657
commit 999b6918b4
4 changed files with 39 additions and 0 deletions

View File

@ -113,6 +113,7 @@ The plugin comes with a number of commands:
- `:SessionStart` - Start recording a session. Useful if `autostart = false` - `:SessionStart` - Start recording a session. Useful if `autostart = false`
- `:SessionStop` - Stop recording a session - `:SessionStop` - Stop recording a session
- `:SessionSave` - Save the current session - `:SessionSave` - Save the current session
- `:SessionSelect` - Load a session from the list (useful if you don't wish to use the Telescope extension)
- `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`) - `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`)
- `:SessionLoadLast` - Load the most recent session - `:SessionLoadLast` - Load the most recent session
- `:SessionLoadFromFile` - Load a session from a given path - `:SessionLoadFromFile` - Load a session from a given path

View File

@ -108,6 +108,7 @@ The plugin comes with a number of commands:
- `:SessionStart` - Start recording a session. Useful if `autostart = false` - `:SessionStart` - Start recording a session. Useful if `autostart = false`
- `:SessionStop` - Stop recording a session - `:SessionStop` - Stop recording a session
- `:SessionSave` - Save the current session - `:SessionSave` - Save the current session
- `:SessionSelect` - Load a session from the list (useful if you dont wish to use the Telescope extension)
- `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`) - `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`)
- `:SessionLoadLast` - Load the most recent session - `:SessionLoadLast` - Load the most recent session
- `:SessionLoadFromFile` - Load a session from a given path - `:SessionLoadFromFile` - Load a session from a given path

View File

@ -138,6 +138,42 @@ function M.branch()
end end
end end
---Select a session to load
function M.select()
---@type { session: string, dir: string, branch?: string }[]
local items = {}
local found = {} ---@type table<string, boolean>
for _, session in ipairs(M.list()) do
if uv.fs_stat(session) then
local file = session:sub(#M.config.save_dir + 1, -5)
local dir, branch = unpack(vim.split(file, "@@", { plain = true }))
dir = dir:gsub("%%", "/")
if jit.os:find("Windows") then
dir = dir:gsub("^(%w)/", "%1:/")
end
if not found[dir .. (branch or "")] then
found[dir .. (branch or "")] = true
items[#items + 1] = { session = session, dir = dir, branch = branch }
end
end
end
vim.ui.select(items, {
prompt = "Select a session: ",
format_item = function(item)
local name = vim.fn.fnamemodify(item.dir, ":p:~")
if item.branch then
name = name .. " (" .. item.branch .. ")"
end
return name
end,
}, function(item)
if item then
vim.fn.chdir(item.dir)
M.load()
end
end)
end
---Determines whether to load, start or stop a session ---Determines whether to load, start or stop a session
function M.toggle() function M.toggle()
M.fire("Toggle") M.fire("Toggle")

View File

@ -9,6 +9,7 @@ vim.cmd([[command! SessionLoad :lua require("persisted").load()]])
vim.cmd([[command! SessionLoadLast :lua require("persisted").load({ last = true })]]) vim.cmd([[command! SessionLoadLast :lua require("persisted").load({ last = true })]])
vim.cmd([[command! SessionDelete :lua require("persisted").delete()]]) vim.cmd([[command! SessionDelete :lua require("persisted").delete()]])
vim.cmd([[command! SessionToggle :lua require("persisted").toggle()]]) vim.cmd([[command! SessionToggle :lua require("persisted").toggle()]])
vim.cmd([[command! SessionSelect :lua require("persisted").select()]])
local persisted = require("persisted") local persisted = require("persisted")