Skip to content

Commit

Permalink
feat: add SessionSelect command
Browse files Browse the repository at this point in the history
For those who don't want to use Telescope to load and select sessions,
this offers an alternative
  • Loading branch information
olimorris committed Aug 22, 2024
1 parent 3b07ef9 commit 999b691
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions doc/persisted.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions lua/persisted/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
function M.toggle()
M.fire("Toggle")
Expand Down
1 change: 1 addition & 0 deletions plugin/persisted.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 999b691

Please sign in to comment.