From 8ae8b120e145618000dd0ecd11cdd7abadeb6013 Mon Sep 17 00:00:00 2001 From: secret Date: Thu, 26 Jun 2025 00:02:10 +0800 Subject: [PATCH] feat: add delete session feature --- lua/persistence/init.lua | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua index 760f6b6..30a08e4 100644 --- a/lua/persistence/init.lua +++ b/lua/persistence/init.lua @@ -103,7 +103,8 @@ function M.last() return M.list()[1] end -function M.select() +---@param opts { prompt: string, handler: function} +function M.handle_selected(opts) ---@type { session: string, dir: string, branch?: string }[] local items = {} local have = {} ---@type table @@ -122,18 +123,39 @@ function M.select() end end vim.ui.select(items, { - prompt = "Select a session: ", + prompt = opts.prompt, format_item = function(item) return vim.fn.fnamemodify(item.dir, ":p:~") end, }, function(item) if item then - vim.fn.chdir(item.dir) - M.load() + opts.handler(item) end end) end +-- select a session to load +function M.select() + M.handle_selected({ + prompt = "Select a session: ", + handler = function(item) + vim.fn.chdir(item.dir) + M.load() + end, + }) +end + +-- select a session to delete +function M.delete() + M.handle_selected({ + prompt = "Delete a session: ", + handler = function(item) + os.remove(item.session) + print("Deleted " .. item.session) + end, + }) +end + --- get current branch name ---@return string? function M.branch()