From 5346b5346a2dd1732ae84f05251ecb704f35df87 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sat, 6 Jul 2024 21:24:22 +0200 Subject: [PATCH] feat: added `require("persistence").select()` to select a session to load --- README.md | 28 ++++++++++++++++++---------- lua/persistence/init.lua | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bd6d57f..4e56655 100644 --- a/README.md +++ b/README.md @@ -35,27 +35,35 @@ Persistence comes with the following defaults: ```lua { 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 - 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 **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. ```lua --- restore the session for the current directory -vim.api.nvim_set_keymap("n", "qs", [[lua require("persistence").load()]], {}) +-- load the session for the current directory +vim.keymap.set("n", "qs", function() require("persistence").load() end) --- restore the last session -vim.api.nvim_set_keymap("n", "ql", [[lua require("persistence").load({ last = true })]], {}) +-- select a session to load +vim.keymap.set("n", "qS", function() require("persistence").select() end) + +-- load the last session +vim.keymap.set("n", "ql", function() require("persistence").load({ last = true }) end) -- stop Persistence => session won't be saved on exit -vim.api.nvim_set_keymap("n", "qd", [[lua require("persistence").stop()]], {}) +vim.keymap.set("n", "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 diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua index ae75c35..34e7c81 100644 --- a/lua/persistence/init.lua +++ b/lua/persistence/init.lua @@ -84,4 +84,25 @@ function M.last() return M.list()[1] 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