diff --git a/README.md b/README.md index ce3db54..0334bcf 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ The plugin comes with a number of commands: - `:SessionStart` - Start recording a session. Useful if `autostart = false` - `:SessionStop` - Stop recording a 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`) - `:SessionLoadLast` - Load the most recent session - `:SessionLoadFromFile` - Load a session from a given path diff --git a/doc/persisted.nvim.txt b/doc/persisted.nvim.txt index 06a1618..a35b584 100644 --- a/doc/persisted.nvim.txt +++ b/doc/persisted.nvim.txt @@ -108,6 +108,7 @@ The plugin comes with a number of commands: - `:SessionStart` - Start recording a session. Useful if `autostart = false` - `:SessionStop` - Stop recording a 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`) - `:SessionLoadLast` - Load the most recent session - `:SessionLoadFromFile` - Load a session from a given path diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 0c57549..f3aab9c 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -138,6 +138,42 @@ function M.branch() end end +---Select a session to load +function M.select() + ---@type { session: string, dir: string, branch?: string }[] + local items = {} + local found = {} ---@type table + 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 function M.toggle() M.fire("Toggle") diff --git a/plugin/persisted.lua b/plugin/persisted.lua index c00fcfe..d7dabb2 100644 --- a/plugin/persisted.lua +++ b/plugin/persisted.lua @@ -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! SessionDelete :lua require("persisted").delete()]]) vim.cmd([[command! SessionToggle :lua require("persisted").toggle()]]) +vim.cmd([[command! SessionSelect :lua require("persisted").select()]]) local persisted = require("persisted")