From 8ae8b120e145618000dd0ecd11cdd7abadeb6013 Mon Sep 17 00:00:00 2001 From: secret Date: Thu, 26 Jun 2025 00:02:10 +0800 Subject: [PATCH 1/2] 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() From baa786fa80678cfab1598d33432aee76f4d2f699 Mon Sep 17 00:00:00 2001 From: secret Date: Sat, 17 Jan 2026 00:35:19 +0800 Subject: [PATCH 2/2] :recycle: refactor(load): add file existence check before session handling --- lua/persistence/init.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua index 30a08e4..85963ea 100644 --- a/lua/persistence/init.lua +++ b/lua/persistence/init.lua @@ -7,6 +7,12 @@ M._active = false local e = vim.fn.fnameescape +---@param file_path string +---@return boolean +local file_exists = function(file_path) + return uv.fs_stat(file_path) and true or false +end + ---@param opts? {branch?: boolean} function M.current(opts) opts = opts or {} @@ -116,7 +122,7 @@ function M.handle_selected(opts) if jit.os:find("Windows") then dir = dir:gsub("^(%w)/", "%1:/") end - if not have[dir] then + if (not have[dir]) and file_exists(dir) then have[dir] = true items[#items + 1] = { session = session, dir = dir, branch = branch } end