Skip to content

Commit 999b691

Browse files
committed
feat: add SessionSelect command
For those who don't want to use Telescope to load and select sessions, this offers an alternative
1 parent 3b07ef9 commit 999b691

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

Diff for: README.md

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

Diff for: doc/persisted.nvim.txt

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ The plugin comes with a number of commands:
108108
- `:SessionStart` - Start recording a session. Useful if `autostart = false`
109109
- `:SessionStop` - Stop recording a session
110110
- `:SessionSave` - Save the current session
111+
- `:SessionSelect` - Load a session from the list (useful if you don’t wish to use the Telescope extension)
111112
- `:SessionLoad` - Load the session for the current directory and current branch (if `git_use_branch = true`)
112113
- `:SessionLoadLast` - Load the most recent session
113114
- `:SessionLoadFromFile` - Load a session from a given path

Diff for: lua/persisted/init.lua

+36
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ function M.branch()
138138
end
139139
end
140140

141+
---Select a session to load
142+
function M.select()
143+
---@type { session: string, dir: string, branch?: string }[]
144+
local items = {}
145+
local found = {} ---@type table<string, boolean>
146+
for _, session in ipairs(M.list()) do
147+
if uv.fs_stat(session) then
148+
local file = session:sub(#M.config.save_dir + 1, -5)
149+
local dir, branch = unpack(vim.split(file, "@@", { plain = true }))
150+
dir = dir:gsub("%%", "/")
151+
if jit.os:find("Windows") then
152+
dir = dir:gsub("^(%w)/", "%1:/")
153+
end
154+
if not found[dir .. (branch or "")] then
155+
found[dir .. (branch or "")] = true
156+
items[#items + 1] = { session = session, dir = dir, branch = branch }
157+
end
158+
end
159+
end
160+
vim.ui.select(items, {
161+
prompt = "Select a session: ",
162+
format_item = function(item)
163+
local name = vim.fn.fnamemodify(item.dir, ":p:~")
164+
if item.branch then
165+
name = name .. " (" .. item.branch .. ")"
166+
end
167+
return name
168+
end,
169+
}, function(item)
170+
if item then
171+
vim.fn.chdir(item.dir)
172+
M.load()
173+
end
174+
end)
175+
end
176+
141177
---Determines whether to load, start or stop a session
142178
function M.toggle()
143179
M.fire("Toggle")

Diff for: plugin/persisted.lua

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ vim.cmd([[command! SessionLoad :lua require("persisted").load()]])
99
vim.cmd([[command! SessionLoadLast :lua require("persisted").load({ last = true })]])
1010
vim.cmd([[command! SessionDelete :lua require("persisted").delete()]])
1111
vim.cmd([[command! SessionToggle :lua require("persisted").toggle()]])
12+
vim.cmd([[command! SessionSelect :lua require("persisted").select()]])
1213

1314
local persisted = require("persisted")
1415

0 commit comments

Comments
 (0)