diff --git a/README.md b/README.md index 15e5b2a..7df242f 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,8 @@ The plugin comes with a number of commands: The Telescope extension may be opened via `:Telescope persisted`. Once opened, the available keymaps are: -* `` - Source the selected session file +* `` - Source the session file +* `` - Delete the session file ### Lazy loading diff --git a/lua/telescope/_extensions/actions.lua b/lua/telescope/_extensions/actions.lua index d7f319a..f8783da 100644 --- a/lua/telescope/_extensions/actions.lua +++ b/lua/telescope/_extensions/actions.lua @@ -1,4 +1,5 @@ local actions_state = require("telescope.actions.state") +local transform_mod = require("telescope.actions.mt").transform_mod local M = {} @@ -11,7 +12,15 @@ end ---Delete the selected session from disk --@return string M.delete_session = function() - print(get_selected_session().file_path) + -- local session = get_selected_session().file_path + local session = get_selected_session() + local path = session.file_path + + local confirm = vim.fn.input("Delete " .. session.name .. "? ", "yes"):lower() + if confirm == "yes" or confirm == "y" then + os.remove(path) + print("Session deleted: " .. path) + end end -return M +return transform_mod(M) diff --git a/lua/telescope/_extensions/finders.lua b/lua/telescope/_extensions/finders.lua new file mode 100644 index 0000000..c67efbd --- /dev/null +++ b/lua/telescope/_extensions/finders.lua @@ -0,0 +1,56 @@ +local finders = require("telescope.finders") +local conf = require("telescope.config").values +local entry_display = require("telescope.pickers.entry_display") + +local M = {} + +M.session_finder = function(sessions) + -- Layout borrowed from: + ---https://github.com/LinArcX/telescope-env.nvim/blob/master/lua/telescope/_extensions/env.lua + local cols = vim.o.columns + local telescope_width = conf.width + or conf.layout_config.width + or conf.layout_config[conf.layout_strategy].width + or cols + + if type(telescope_width) == "function" then + telescope_width = telescope_width(_, cols, _) + end + + if telescope_width < 1 then + telescope_width = math.floor(cols * telescope_width) + end + + local branch_width = 30 + local name_width = math.floor(cols * 0.05) + + local displayer = entry_display.create({ + separator = " │ ", + items = { + { width = telescope_width - branch_width - name_width }, + { width = branch_width }, + { remaining = true }, + }, + }) + local make_display = function(session) + return displayer({ + session.name, + session.branch, + }) + end + + return finders.new_table({ + results = sessions, + entry_maker = function(session) + session.ordinal = session.name + session.display = make_display + session.name = session.name + session.branch = session.branch + session.file_path = session.file_path + + return session + end, + }) +end + +return M diff --git a/lua/telescope/_extensions/persisted.lua b/lua/telescope/_extensions/persisted.lua index 9bd5354..e24b309 100644 --- a/lua/telescope/_extensions/persisted.lua +++ b/lua/telescope/_extensions/persisted.lua @@ -1,74 +1,27 @@ local actions = require("telescope.actions") -local finders = require("telescope.finders") local pickers = require("telescope.pickers") local conf = require("telescope.config").values -local entry_display = require("telescope.pickers.entry_display") local action_state = require("telescope.actions.state") local _actions = require("telescope._extensions.actions") +local _finders = require("telescope._extensions.finders") local function search_sessions(opts) local config = require("persisted.config").options - -- Layout borrowed from: - ---https://github.com/LinArcX/telescope-env.nvim/blob/master/lua/telescope/_extensions/env.lua - local cols = vim.o.columns - local telescope_width = conf.width - or conf.layout_config.width - or conf.layout_config[conf.layout_strategy].width - or cols - - if type(telescope_width) == "function" then - telescope_width = telescope_width(_, cols, _) - end - - if telescope_width < 1 then - telescope_width = math.floor(cols * telescope_width) - end - - local branch_width = 30 - local name_width = math.floor(cols * 0.05) - - local displayer = entry_display.create({ - separator = " │ ", - items = { - { width = telescope_width - branch_width - name_width }, - { width = branch_width }, - { remaining = true }, - }, - }) - local make_display = function(entry) - return displayer({ - entry.name, - entry.branch, - }) - end - - local make_entry = function(item) - return { - ordinal = item.name, - display = make_display, - - name = item.name, - branch = item.branch, - file_path = item.file_path, - } - end - pickers.new(opts, { prompt_title = "Sessions", sorter = conf.generic_sorter(opts), - finder = finders.new_table({ - results = require("persisted").list(), - entry_maker = make_entry, - }), + finder = _finders.session_finder(require("persisted").list()), attach_mappings = function(prompt_bufnr, map) + local refresh_sessions = function() local picker = action_state.get_current_picker(prompt_bufnr) - local finder = require("persisted").list() - picker:refresh(finder, { reset_prompt = true }) + picker:refresh(_finders.session_finder(require("persisted").list()), { reset_prompt = true }) end + _actions.delete_session:enhance({ post = refresh_sessions }) + map("i", "", _actions.delete_session) actions.select_default:replace(function()