feat: added require("persistence").select() to select a session to load

This commit is contained in:
Folke Lemaitre 2024-07-06 21:24:22 +02:00
parent f58a838282
commit 5346b5346a
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 39 additions and 10 deletions

View File

@ -35,27 +35,35 @@ Persistence comes with the following defaults:
```lua ```lua
{ {
dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved dir = vim.fn.stdpath("state") .. "/sessions/", -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
pre_save = nil, -- a function to call before saving the session
post_save = nil, -- a function to call after saving the session
save_empty = false, -- don't save if there are no open file buffers save_empty = false, -- don't save if there are no open file buffers
pre_load = nil, -- a function to call before loading the session
post_load = nil, -- a function to call after loading the session
} }
``` ```
> [!TIP]
> To configure what should be saved in your session, check [:h 'sessionoptions'](https://neovim.io/doc/user/options.html#'sessionoptions')
## 🚀 Usage ## 🚀 Usage
**Persistence** works well with plugins like `startify` or `dashboard`. It will never restore a session automatically, **Persistence** works well with plugins like `startify` or `dashboard`. It will never restore a session automatically,
but you can of course write an autocmd that does exactly that if you want. but you can of course write an autocmd that does exactly that if you want.
```lua ```lua
-- restore the session for the current directory -- load the session for the current directory
vim.api.nvim_set_keymap("n", "<leader>qs", [[<cmd>lua require("persistence").load()<cr>]], {}) vim.keymap.set("n", "<leader>qs", function() require("persistence").load() end)
-- restore the last session -- select a session to load
vim.api.nvim_set_keymap("n", "<leader>ql", [[<cmd>lua require("persistence").load({ last = true })<cr>]], {}) vim.keymap.set("n", "<leader>qS", function() require("persistence").select() end)
-- load the last session
vim.keymap.set("n", "<leader>ql", function() require("persistence").load({ last = true }) end)
-- stop Persistence => session won't be saved on exit -- stop Persistence => session won't be saved on exit
vim.api.nvim_set_keymap("n", "<leader>qd", [[<cmd>lua require("persistence").stop()<cr>]], {}) vim.keymap.set("n", "<leader>qd", function() require("persistence").stop() end)
``` ```
## 📅 Events
- **PersistenceLoadPre**: before loading a session
- **PersistenceLoadPost**: after loading a session
- **PersistenceSavePre**: before saving a session
- **PersistenceSavePost**: after saving a session

View File

@ -84,4 +84,25 @@ function M.last()
return M.list()[1] return M.list()[1]
end end
function M.select()
---@type { session: string, dir: string }[]
local items = {}
for _, session in ipairs(M.list()) do
if uv.fs_stat(session) then
local dir = session:sub(#Config.options.dir + 1, -5):gsub("%%", "/")
items[#items + 1] = { session = session, dir = dir }
end
end
vim.ui.select(items, {
prompt = "Select a session: ",
format_item = function(item)
return vim.fn.fnamemodify(item.dir, ":p:~")
end,
}, function(item)
if item then
M.load({ file = item.session })
end
end)
end
return M return M